alexya-framework / localization
Alexya's Localization utilities
Installs: 33
Dependents: 0
Suggesters: 1
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Type:framework
Requires
- php: >=7.0
This package is not auto-updated.
Last update: 2025-01-04 21:41:40 UTC
README
Alexya's localization components
Contents
Text translation
The class \Alexya\Localization\Translator
offers a way for translating texts.
Bassically you instance a Translator
object with the given text translatation and the you call the
translate
method whenever you want to translate a text.
Instantiating Translator objects
The constructor accepts as parameter an associative array containing the language code and the translations. Optionally you can send a second parameter being the default language where the texts will be translated.
Example:
<?php $translator = new \Alexya\Localization\Translator([ "en" => [ "monday" => "monday", "thursday" => "thursday", "wednesday" => "wednesday", "tuesday" => "tuesday", "friday" => "friday", "saturday" => "saturday", "sunday" => "sunday" ], "es" => [ "monday" => "lunes", "thursday" => "martes", "wednesday" => "miercoles", "tuesday" => "jueves", "friday" => "viernes", "saturday" => "sabado", "sunday" => "domingo" ] ], "en");
Translating texts
Once the object has been instantiated you can use the method translate
to translate a text.
It accepts as parameter a string being the text to translate.
Optionally you can send an array with the variables to parse or a string with the language code to translate the text, or even both.
If the language doesn't exist, the text will be translated to the default language.
If the text couldn't be translated, it will return the first parameter.
Example:
<?php $translator = new \Alexya\Localization\Translator([ "en" => [ "monday" => "monday", "thursday" => "thursday", "wednesday" => "wednesday", "tuesday" => "tuesday", "friday" => "friday", "saturday" => "saturday", "sunday" => "sunday", "Today is {day}" => "Today is {day}" ], "es" => [ "monday" => "lunes", "thursday" => "martes", "wednesday" => "miercoles", "tuesday" => "jueves", "friday" => "viernes", "saturday" => "sabado", "sunday" => "domingo", "Today is {day}" => "Hoy es {day}" ] ]); // Quick translation $translator->translate("Today is {day}"); // Today is {day} // Translation with context $translator->translate("Today is {day}", [ "day" => $translator->translate("monday") ]); // Today is monday // Translation in a specific language $translator->translate("Today is {day}", "es"); // Hoy es {day} // Translation in a specific language with context $translator->translate("Today is {day}", [ "day" => $translator->translate("monday", "es") ], "es"); // Hoy es lunes // Text that can't be translated $translator->translate("some_text"); // some_text
If the language isn't specified it will be translated to the language sent to the method setDefaultLanguage
.
For translating texts of a sub-array use a dot (.
) to link the texts to translate:
<?php $translator = new \Alexya\Localization\Translator([ "en" => [ "days" => [ "monday" => "monday", "thursday" => "thursday", "wednesday" => "wednesday", "tuesday" => "tuesday", "friday" => "friday", "saturday" => "saturday", "sunday" => "sunday" ], "phrases" => [ "today_is" => "Today is {day}" ] ] ]); // Recursive translation $translator->translate("phrases.today_is", [ "day" => $translator->translate("days.monday") ]); // Today is monday
You can also add more translations to an already specified language with the method addTranslations
.
It accepts as parameter the language code and an array containing the translations to add.
If the language code doesn't exist, it will create it.