Download the PHP package yak0d3/mirza_yandex_translator without Composer
On this page you can find all versions of the php package yak0d3/mirza_yandex_translator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download yak0d3/mirza_yandex_translator
More information about yak0d3/mirza_yandex_translator
Files in yak0d3/mirza_yandex_translator
Package mirza_yandex_translator
Short Description Mirza Translator For Laravel gives you the ability to easily translate and manipulate text using the Yandex.Translate API.
License MIT
Informations about the package mirza_yandex_translator
Mirza Yandex Translator For Laravel
Mirza Translator gives you the ability to easily translate and manipulate text using the Yandex.Translate API.
Table Of Contents
- Quick Start
- Quick Usage Guide
- Documentation
- Methods
- Blade Directives
- Issues & Suggestions
- License
Quick Start
Let's set this up real quick in just three mere steps!
-
Navigate to your Laravel installation folder via the terminal/cmd and run
composer require yak0d3/Mirza_Yandex_Translator
or add"yak0d3/Mirza_Yandex_Translator": "^1.0.0"
manually to yourcomposer.json
. -
Publish the configuration file using one of the following methods:
- Run
php artisan vendor:publish --provider="yak0d3\Mirza_Yandex_TranslatorT\MirzaServiceProvider"
- Run
php artisan vendor:publish
and type the number behindyak0d3\Mirza_Yandex_TranslatorT\MirzaServiceProvider
then pressEnter
- Run
- Run
- Add environment variable to your
.env
file with the nameYANDEX_API
and set its value to your own Yandex.Translate API Key. (e.g.YANDEX_API=MY_YANDEX_API_KEY
)
Note: You can get your FREE API Key from the Yandex.Translate Developers Dashboard
Quick Usage Guide
The quick usage guide is only meant to explain the basic usage of this package, for the list of methods and its relative information (Parameters, Return Type etc..) jump to the directives sections to view the list of available blade
directives.
-
Detect Language:
Mirza::detectLanguage('Welcome');
Output:en
-
Translate text:
Mirza::translate('Hello','es');
Output:"Hola"
-
Translate to Multiple Languages:
Mirza::translateTo('Hello World!',['es', 'tr', 'fr']')
Output:
Note: You can decode this string by using the
json_decode
function.
Translate an Array of Text:
$textArray = ['Hello','My Dear','Friend'];
Mirza::translateArray($textArray,'fr');
Output:Note: You can decode this string by using the
json_decode
function.
Still not getting it? Take a look at the Documentation below and the confusion will go away!
Documentation (With Examples)
Let's admin it, not everyone in here will find it easy to start using this package, so let's try to understand what's happening together. This section will cover the usage of each and every method provided by Mirza Yandex Translator, here is the table of contents:
- The
translate
method- The
translateArray
method- Using sequential arrays
- Using associative arrays
- The
translateTo
method - The
detectLanguage
method- Return language code
- Return language name
- The
getSupportedLanguages
method - The
translateToAll
method - Blade directives
@translate
directive@yandex_rights
directive@languages_select
directive1. Thetranslate
methodAs you have already expected, for sure there is a
translate
method for a translator package, this method takes two parameters; the text and the ISO code of the target language.
- The
Example:
translateArray
methodNote that all
json
strings needs to be decoded using the PHPjson_decode
function. Tip: To return a PHP array set the second argument ofjson_decode
totrue
(e.g.json_decode($jsonString, true);
).
If you prefer manipulatingjson objects
, leave the second argument empty or set it tofalse
.
Mirza::translateArray(['Hello', 'My Dear', 'Friend'],'fr');
this method translates a given array of text into which is in our case this array ['Hello', 'My Dear', 'Friend']
and translates it to a given language which is French in our example.
This function returns a json encoded
string like the following:
As you can see, the output json string
is in the same order of the input array, now we can access each of these elements by decoding the string like so:
Easy, right? But it could get easier if you set the the $assoc parameter to true so you are able to access your string translations by their index names (that you have set manually). No body is getting confused in here, here is an example:
Note: If you set
$assoc
totrue
and provide a sequential array an exception will be thrown.
translateTo
method:This method is (maybe) the reverse version of the previous function, instead of taking an array
of strings, this method takes one string
and translates it to an array of languages.
Example:
The above example will return json string
with the following structure:
You may have noticed that some of the characters are in Unicode format, no worries if you
echo
it later on it will be displayed correctly.
Now we can easily decode this json string
and access our data like so:
detectLanguage
methodYou sometimes need to detect in which language a text is written, the detectLanguage
method is made just for this matter!
As mentioned in the methods table, this method takes one required parameter and one optional.
The optional parameter (boolean $name
) lets us switch between returning the language ISO code or the language name.
Example:
-
Return language code:
-
Return language name:
getSupportedLanguages
methodThis method takes no parameters (it should, but that will be added in a later version) and if executed it returns the list of all the supported languages.
Example:
I didn't want to include the whole output because it is so long, but if you are still curious about it, i was prepared for this! Here is a screenshot:
Let's decode this json string
and play a little bit!
Let's say we have a language code, but we don't know to what language it refers, this line would help us a lot in such a case:
Now supposing that we have a language name, but we doesn't know the ISO code, EASY PEASY! We can do it with the PHP array_flip
function
translateToAll
methodI don't know what you might use this method for, but i thought it would be nice to include such a feature. As mentioned in the method name, this method translates a given string to all of the supported languages.
Example:
-
@translate
: Allows you to translate a given text to a given language on the goExample:
-
@yandex_rights
: If you have read the Yandex.Translate requirements for the use of translation results you'd know that this directive will be very useful.
You have to specify thecolor
as the first argument and thefont-size
as the second one.Example:
@languages_select
: Generates an HTML<select>
with the list of all supported languages.
Example:
Methods
Everything in Mirza is meant to be easy and readable, just by taking a look at the source code you will understand what's happening in no time. But don't worry, i have saved you the struggle and made a table containing the the list of methods that Mirza Translator provides.
Method | Parameters | Returns | Throws | Description |
---|---|---|---|---|
translate | string $text string $lang Optional: string $format [html\|plain] (Default: "Plain") |
String | Exception: If text couldn't be translated. | Translates a given $text to a given $lang (language) |
translateTo | string $text array $langs |
String (json) | Exception: If one or more languages aren't supported. | Translate a given $text to multiple $langs (languages) |
translateArray | array $textArray string $lang Optional: bool $assoc (Default: false) |
String (json) | Exception: 1. If target language is not supported. 2. If $assoc is set to true and the given array is not associative. |
Translates a $textArray (array of text) to a given $lang (language) Note: If $assoc is set to true , the returned json string will have the same index names |
detectLanguage | string $text Optional: bool $langName |
String | Exception: 1. If language code is not found. 2. If language name is not found |
Detects the language of a given $text and returns the language code Note: If $langName is set to true , the language full name will be returned instead. |
getSupportedLanguages | None | String (json) | Exception: If an unknown error occurs while trying to fetch the list of supported functions | Returns a json string containing the list of all supported languages |
translateToAll | string $text |
String (json) | None | Translates a string ($text ) to all supported languages. Note: This may take a while and cause a PHP max_execution_time TIMEOUT Exception |
yandex_rights | Optional: string $color (Default: #fff) string $fontsize (Default: 14px) |
String | None | Returns the string of the "Powered By Yandex.Translate" link string. Also called via blade directive Yandex Translate: Requirements for the use of translation results to know more about font-size, color and placing requirements. |
languages_select | None | String | None | Returns the string of an HTML <select> tag with the list of all available languages. Also called via blade directive @languages_select |
Blade Directives
Directive | Parameters | Description |
---|---|---|
@yandex_rights |
Optional: string $color (Default: #fff) string $fontsize (Default: 14px) |
Generates an HTML link for the "Powered By Yandex.Translate" text. |
@languages_select |
None | Generates an HTML <select> tag with the list of all available languages. |
@translate |
string $text string $lang |
Translate a given $text string to a given $lang (language) |
Issues & Suggestions
Mirza has been tested by only one person (obviously me 😃), which means that problems might occur with others, if something went wrong with your Mirza installation or you think something is still missing, please let me know by submitting a new issue.