Since about 2010, I’ve been searching for a translation framework for PHP that may generate very complicated phrases and on the identical time mix choices equivalent to singular/plural and work with genders (male, feminine, unknown) and ideally additionally format numbers in keeping with the requirements of the given nation or area — unsuccessfully. In 2018, Fb launched FBT — an open supply localization framework that gives a extra environment friendly manner of defining content material for versatile and high-quality localization. I didn’t hesitate in any respect. I regularly began to rewrite this JavaScript model into PHP, and even when a couple of small issues weren’t totally in keeping with the unique code (assortment of strings by command) I succeeded. It was my problem that I completed in about 40 days (higher mentioned, evenings 😊).
There are numerous explanation why the frequent i18n libraries are very insufficient. The primary ones are:
- misunderstanding of the native textual content by the translator (doesn’t know the context),
- inadequate/nonsensical code mixtures for translation,
- don’t help options like enums or pronouns,
- and lots of extra…
Luckily, FBT can remedy all of this. Nonetheless, it’s not simple to make use of it for the primary time if you end up not conversant in it. However I will probably be comfortable to advise you on tips on how to use it.
1. Set up the FBT package deal
composer require richarddobron/fbt
2. Set your FBT configuration
<?php
require ("vendor/autoload.php");
fbtFbtConfig::set('writer', 'your identify/group');
fbtFbtConfig::set('mission', 'mission identify');
fbtFbtConfig::set('path', '/path/to/storage/fbt');
3. Language and gender settings
When you simply wish to change the interface language, simply use:
fbtFbtHooks::locale('sk_SK'); // app locale
If in case you have an software during which customers log in, you need to use the interface IntlViewerContextInterface:
After implementation, set viewerContext:
$loggedUserDto = ...;
fbtFbtConfig::set('viewerContext', $loggedUserDto);
4. Put together translations recordsdata
Fb has devised their very own system of labeling languages, you’ll find a listing of them at this link.
From this checklist, select the languages into which you wish to translate your web site or software.
I normally add them to the listing /storage/fbt/
File identify will appear like this: sk_SK.json
5. Add FBT scripts to composer.json
6. Add your texts
Native phrases are at all times anticipated in English, it’d look one thing like this:
7. Translate collected texts
That is what the collected supply strings appear like within the .source_strings.json file after script execution:
Now we’re able to generate a file during which we are going to write the translations for the collected supply strings:
composer run generate-translations
The /storage/fbt/sk_SK.json file now comprises the hash keys of the supply phrases that we are able to now translate. I make translations into Slovak, so I can use the gender of the {identify} token and adapt the variations of the given textual content exactly. This step is a bit tough for some translations with variants, as you must work with the variations key.
It’s also possible to discover a number of sorts of translations at this link.
After finishing the translations, we run the command that creates the translations for the appliance:
composer run translate-fbts
Lastly, this command will generate a manufacturing file with translations utilizing Jenkins hash.
Your app is now translated! Hurray!
Thanks for studying 🙏.