mouf / utils.i18n.fine.translation-interface
This package contains two interfaces shared by all translation services. The LanguageDetectionInterface is used to detect the current language for the user while the TranslatorInterface is used to translate strings.
Installs: 17 671
Dependents: 5
Suggesters: 0
Security: 0
Stars: 0
Watchers: 18
Forks: 0
Open Issues: 0
Requires
- php: >=5.4.0
This package is auto-updated.
Last update: 2024-10-15 04:48:00 UTC
README
Fine is a PHP internationalisation package. It will help you develop applications that support several languages. FINE means: Fine is not English :).
By default, translation is performed using PHP mapping files, but other implementation (database storage) can also by used. Fine is a Mouf package. This means you will have a nice graphical interface inside the Mouf framework to write your own translated messages.
In this document, we will describe how to use FINE to support several languages in your application.
TODO SCREENSHOT INTERFACE !!!
Install
Dependencies
Fine comes as a Composer package and requires the "Mouf" framework to run. The first step is therefore to install Mouf.
Once Mouf is installed, you can process to the Fine installation.
Install Fine
Edit your composer.json file, and add a dependency on mouf/utils.i18n.fine.
A typical composer.json file might look like this:
{
"require": {
"mouf/mouf": "~2.0.0",
"mouf/utils.i18n.fine.translation-interface": "~4.0",
"mouf/utils.i18n.fine.language-detection": "~4.0",
"mouf/utils.i18n.fine.file-translator": "~4.0",
"mouf/utils.i18n.fine.manage.bo": "~4.0",
"mouf/utils.i18n.fine.common": "~4.0"
},
"autoload": {
"psr-4": {
"Test\\": "src/"
}
}
}
As you can see, there are a number of packages included:
- mouf/utils.i18n.fine.translation-interface: Fine is a modular package. You can replace any part of it (language detection, translation system, etc...) This package contains the interfaces for language detection and translation system implemented by the other packages.
- mouf/utils.i18n.fine.language-detection: This package contains the classes used to detect the language of the user (based on the browser, or the session, etc...)
- mouf/utils.i18n.fine.file-translation: There is many solution to store the translation. By default, the file storage is enabled. There is other package:
- mouf/utils.i18n.fine.bd-translation: Store translation in database.
- mouf/utils.i18n.fine.manage.bo: To manage translations, this package adds a human interface in Mouf to add, edit or delete a translation.
- mouf/utils.i18n.fine.common: This package contains the installer to help the user to install all the great packages to start and all the interface, function or trait to use fine or implement it
To install the package. After yout composer, you must going to the Mouf interface and select Project -> Installation tasks
You could see the list of fine package that must install to use it.
Click on button Run the x tasks.
The installation create all the instance to use fine:
-
The FineCacadingTranslator class is to cascading all your translator. If the first doesn't have the translation, the second is requested.... The first translator how has the response is returned. So the order of translators is very important.
-
Found the defaultTranslationService instance in Mouf, click on it. There is a table of TranslatorInterface
-
To add a new instance, please click on add an instance, don't forget to configure it!
-
To change the order, just drag and drop the instance before or after another
Your package is ready for the code!
Using interface
Create a translation
This interface can be use to add, edit or delete a translation. You can access to this interface with the menu HTML -> Fine -> Manage Translation.
On this interface there is a button to add a new translation.
A modal appears, in the title input you can set your fine key, in this example it's "test.key". Select you language, for example english and add it ith the "add" button.
Enter a message in the text zone. And click on the "Save all Translations" button.
In this page you can see your translation.
The top right select is to diplay the language at the bottom of the modal. With this translation you can easily translate all message.
Edit translation
To edit a translation click on the key or directly on the translation. You can add a new language with the "add" tab in the modal.
Remove a translation
You can remove a translation with the cross on the language. After you can validate to remove only this language
To remove the full key, click on the bottom button "remove key" and validate it.
In your code
In your code, you can use the functions. There are bind automatically to the defaultTranslationService:
- iMsg function: Return the translation of the key, parameters (in option) and LanguageDetection (in option)
- eMsg function: Do an echo of iMsg
Example:
$string = iMsg('mykey', array('parameter1' => 'value')); eMsg('mykey', array('parameter1' => 'value'));
The messagethe key mykey is: "A test with {parameter1}"
It's possible to add an instance of LanguageDetectionInterface (for exemple to change the language in the middle of your code). Example: In this controller you have bind an instance of LanguageDetetionInterface named $fixEnglishLanguage So you can call the function with
iMsg('myKey', array('parameter1' => 'value'), $this->fixEnglishLanguage);
Interface Implementation
If you want to create your own package, you can see the implementation