i18n_mrg is the helper tool that scans your project's codebase for translated strings and synchronizes translations with a popular translation tool named Weblate and back.
The software you need to install the translation merge tool:
- PHP >= 7.0
- Composer package manager
- GNU Gettext package (already installed on macOS and most Linux distributions)
- NodeJS >= 6.0 (for JS projects)
- i18next converter (if you use i18next for your js project)
You require php>=8.1 to run the software. Also, you require to have a PHP packet manager (composer), which you may get here.
Run the following command to install the tool:
composer global require lehadnk/translation-merge-tool
Run the following command under your project root:
i18n_mrg
First, you need to prepare your codebase to work with i18n_mrg:
- Configure your project to work with translation files. We'd suggest you use GNU gettext format, but i18n_mrg could also compile JSON files as well if you prefer, which works better for web applications. You need to set up a directory in your project and store your translation files in it, naming each subfolder with the language code you're planning to use. Example file tree structure:
/i18n/en_GB/messages.po
/i18n/de_DE/messages.po
/i18n/zh_CN/messages.po
- Use the wrapper function named
__()
to mark your translation strings. Implement the function to return the corresponding string from your storage, e.g.:
public class i18nService {
private static <LocaleEnum, HashMap<String, String>> translationStrings;
public static String __(string text, HashMap<String, String> placeholders)
{
var translatedString = i18nService.translationStrings.get("de_DE").get(text);
StrSubstitutor sub = new StrSubstitutor(placeholders, "%", "%");
return sub.replace(translatedString);
}
}
import static i18n.I18nFacade.__;
public class ScoreHandler {
public UserResponse getScore()
{
var placeholders = new HashMap<String, String>();
placeholders.put("score", 16);
var response = new UserResponse();
response.message = __("You have %score% points", placeholders); // Sie haben 16 Punkte
return response;
}
}
- Add
.translate-config.json
to your project root. The example contents of the file:
{
"configVersion": "1.3.0",
"components": [
{
"name": "default",
"includePaths": [
"app/",
"resources/"
],
"excludePaths": [],
"translationFileName": "resources/lang/i18n/{localeName}/LC_MESSAGES/default.po",
"weblateProjectSlug": "crm",
"weblateComponentSlug": "main"
}
],
"vcs": "bitbucket",
"bitbucketUsername": "bitbucket@user.com",
"bitbucketPassword": "password",
"vcsRepository": "company/crm-project",
"vcsAuthToken": "token",
"translationBranchName": "translation",
"weblateServiceUrl": "http://weblate.service.com",
"weblateAuthToken": "token"
}
- Next, define the translation branch in your repository. Usually, you want it to be managed by translation tools (i18n_mrg and Weblate) only, and never touch it:
git checkout -b translations && git push --set-upstream origin translations
- Set up the Weblate platform and define the component for your project.
- Now run
i18n_mrg
to scan your project for translation strings and initially upload them to Weblate. i18n_mrg will parse your codebase for every string wrapped in __() decorator and add it to translation files, the pull updates from Weblate automatically.
For developers working with i18n_mrg
on your project, I'd suggest the following workflow:
- When you're about to be done with your working branch, which contains translation strings, run
i18n_mrg
and push new translation strings to Weblate. - Notify the person in your team responsible for handling translation that new strings are added to Weblate.
- Once translations are done, run
i18n_mrg
again to pull translated strings from Weblate and add updated translated files to your branch.
In big teams, you may also want translations files compilation to be a part of your CI cycle.
composer global update lehadnk/translation-merge-tool
To use the tool with various VCS providers, you must set up authorization.
- Create the key using your profile settings > Access Tokens.
- Your key must have the next scopes: api, read_repository, write_repository
- Export your token to I18N_MRG_GITLAB_AUTH_TOKEN env variable:
export I18N_MRG_GITLAB_AUTH_TOKEN=<token>
- Goto Settings > Developer Settings > Personal access tokens
- Create a token with scope: repo
- Export your token to I18N_MRG_GITHUB_AUTH_TOKEN env variable:
export I18N_MRG_GITHUB_AUTH_TOKEN=<token>
- Go to repository settings
- Issue the repository access token with read/write permissions in the "Access Tokens" section
- Place your token in I18N_MRG_BITBUCKET_AUTH_TOKEN env variable:
export I18N_MRG_BITBUCKET_AUTH_TOKEN=<token>
First, you need to create a.translate-config.json
configuration file under the project's root. Example contents of the config file:
Some Mac users reported that they had issues with the gettext tool installed through Brew. Unlinking and linking it again resolves the issue:
brew unlink gettext && brew link gettext