TranslationApi Module
Purpose
Provides content translation to support localisation and export of the app to other countries.
Responsible for
- Tweaks to laravels core translation system (specifically override loading)
- Google translate JS component
Laravel translations
We essentially use laravel core translations, but due to the modular architecture a few tweaks have been made to support modules defining their own strings and then other modules supporting overriding those strings.
Modules defining their own translation files
Translation files are stored in the modules Resources/lang/en directory. If it is another language then change the local accordingly, eg change en to fr. You can then use the __() helper function to get the translated string.
The key that you use with the __() helper is formatted module_name::file_name.key_name. So if this module provided a translation file Resources/lang/en/example.php with the following content:
return [
'my_cool_string' => 'My cool string dude';
];
You would use the __() helper like so:
echo __('translationapi::example.my_cool_string');
Note the module name is the lowercase version.
Overriding translations
If you want to override a translation string from another module, you can do so by creating a new translation file in the Resources/lang/vendor/module_name/en directory of your module. The filename should be the same as the file you want to override and contain the keys you want to override.
So if, for example the Users module wanted to override the example.my_cool_string key from the TranslationApi module, it would create a file Resources/lang/vendor/translationapi/en/example.php with the following content:
return [
'my_cool_string' => 'My cool string from the users module dude';
];
Then __('translationapi::example.my_cool_string') would return My cool string from the users module dude.
Module priorities kick in here, so you may need to tweak, your modules priority value if there is a conflicting override.
How this works
Modules define their own translation files in the Resources/lang/en directory which get auto loaded by the BaseModuleServiceProvider::register() method. Overrides were a bit more tricky, this required extending the core laravel translate FileLoader rebinding to the service container. See TranslationApiServiceProvdier for what is happening.
This module has a high priority
This module has an extremely high module priority as many other modules require translations in their service provider.
Google translate
Enable Google translate component
By default disabled. When enabled via dashboard translation settings, it will add a language selectbox to translate the frontend to another language.
Frontend rendering
Ensure that your frontend layout includes the component with the language list as a prop, e.g:
<translate-select :lang-list="translationLangList()"></translate-select>
Language list
The language list is defined under the config translation_api.google_translate.languages. It is passed to the frontend via a global prop, see TranslationApiServiceProvider.
Exclude items from translation
For something you do not want to translate, add class="notranslate"