2019-11-18 05:58:33 +01:00
---
2015-06-18 07:50:29 +02:00
title: Implement Internationalisation
summary: Implement SilverStripe's internationalisation system in your own modules.
2019-11-18 05:58:33 +01:00
icon: globe
---
2012-04-27 17:32:54 +02:00
2015-06-18 07:50:29 +02:00
# Implementing Internationalisation
2012-04-27 17:32:54 +02:00
2015-06-18 07:50:29 +02:00
To find out about how to assist with translating SilverStripe from a user's point of view, see the
2015-02-28 01:09:15 +01:00
[Contributing Translations page ](/contributing/translations ).
2012-05-04 10:19:48 +02:00
2015-06-18 07:50:29 +02:00
## Set up your own module for localisation
2012-04-27 17:32:54 +02:00
### Collecting translatable text
2014-09-26 09:49:44 +02:00
As a first step, you can automatically collect all translatable text in your module through the `i18nTextCollector`
2015-02-28 01:09:15 +01:00
task. See [i18n ](../developer_guides/i18n#collecting-text ) for more details.
2012-04-27 17:32:54 +02:00
### Import master files
2020-11-26 00:06:37 +01:00
If you don't have an account on transifex.com yet, [create a free account now ](https://www.transifex.com/signup/ ). After
2014-09-26 09:49:44 +02:00
creating a new project, you have to upload the `en.yml` master file as a new "Resource". While you can do this through
the web interface, there's a convenient
2020-11-26 00:06:37 +01:00
[commandline client ](https://docs.transifex.com/client/introduction ) for this
2014-09-26 09:49:44 +02:00
purpose. In order to use it, set up a new `.tx/config` file in your module folder:
2017-10-27 04:38:27 +02:00
2014-09-26 09:49:44 +02:00
```yaml
[main]
host = https://www.transifex.com
2012-04-27 17:32:54 +02:00
2013-08-03 20:18:06 +02:00
2014-09-26 09:49:44 +02:00
[my-project.master]
file_filter = lang/< lang > .yml
source_file = lang/en.yml
source_lang = en
type = YML
```
2012-04-27 17:32:54 +02:00
2015-06-18 07:50:29 +02:00
If you don't have existing translations to import, your project is ready to go - simply point translators to the URL, have them
2014-09-26 09:49:44 +02:00
sign up, and they can create languages and translations as required.
2012-04-27 17:32:54 +02:00
### Import existing translations
2014-09-26 09:49:44 +02:00
In case you have existing translations in YML format, there's a "New language" option in the web interface.
2020-11-26 00:06:37 +01:00
Alternatively, use the [commandline client ](https://docs.transifex.com/client/introduction ).
2012-04-27 17:32:54 +02:00
### Export existing translations
2014-09-26 09:49:44 +02:00
You can download new translations in YML format through the web interface, but that can get quite tedious for more than
2020-11-26 00:06:37 +01:00
a handful of translations. Again, the [commandline client ](https://docs.transifex.com/client/introduction )
2013-08-03 20:18:06 +02:00
provides a more convenient interface here with the `tx pull` command, downloading all translations as a batch.
2012-04-27 17:32:54 +02:00
2012-07-13 14:25:44 +02:00
### Merge back existing translations
2014-09-26 09:49:44 +02:00
If you want to backport translations onto release branches, simply run the `tx pull` command on multiple branches. This
assumes you're adhering to the following guidelines:
2012-07-13 14:25:44 +02:00
2013-08-03 20:18:06 +02:00
- For significantly changed content of an entity, create a new entity key
- For added/removed placeholders, create a new entity
- Run the `i18nTextCollectorTask` with the `merge=true` option to avoid deleting unused entities
(which might still be relevant in older release branches)
2012-07-13 14:25:44 +02:00
2015-06-18 07:50:29 +02:00
### Converting your language files from 2.4 PHP format to YML
2012-04-27 17:32:54 +02:00
2014-09-26 09:49:44 +02:00
The conversion from PHP format to YML is taken care of by a module called
[i18n_yml_converter ](https://github.com/chillu/i18n_yml_converter ).
2012-04-27 17:32:54 +02:00
2013-08-03 20:18:06 +02:00
## Download Translations from Transifex.com
2012-04-27 17:51:38 +02:00
2020-11-26 00:06:37 +01:00
We are managing our translations through a tool called [transifex.com ](https://www.transifex.com ). Most modules are handled
2014-09-26 09:49:44 +02:00
under the "silverstripe" user, see
2020-11-26 00:06:37 +01:00
[list of translatable modules ](https://www.transifex.com/user/profile/silverstripe/ ).
2012-04-27 17:51:38 +02:00
2014-09-26 09:49:44 +02:00
Translations need to be reviewed before being committed, which is a process that happens roughly once per month. We're
merging back translations into all supported release branches as well as the `master` branch. The following script
should be applied to the oldest release branch, and then merged forward into newer branches:
2017-10-27 04:38:27 +02:00
```bash
tx pull
# Manually review changes through git diff, then commit
git add lang/*
git commit -m "Updated translations"
2017-08-03 02:51:32 +02:00
```
2012-04-27 17:32:54 +02:00
2019-11-18 05:58:33 +01:00
[notice]
2014-09-26 09:49:44 +02:00
You can download your work right from Transifex in order to speed up the process for your desired language.
2019-11-18 05:58:33 +01:00
[/notice]
2013-12-19 18:00:54 +01:00
## JavaScript Translations
2014-09-26 09:49:44 +02:00
SilverStripe also supports translating strings in JavaScript (see [i18n ](/developer_guides/i18n )), but there's a
conversion step involved in order to get those translations syncing with Transifex. Our translation files stored in
`mymodule/javascript/lang/*.js` call `ss.i18n.addDictionary()` to add files.
2017-08-03 02:51:32 +02:00
2017-10-27 04:38:27 +02:00
```js
ss.i18n.addDictionary('de', {'MyNamespace.MyKey': 'My Translation'});
2017-08-03 02:51:32 +02:00
```
2013-12-19 18:00:54 +01:00
But Transifex only accepts structured formats like JSON.
2014-09-26 09:49:44 +02:00
```
{'MyNamespace.MyKey': 'My Translation'}
```
2013-12-19 18:00:54 +01:00
First of all, you need to create those source files in JSON, and store them in `mymodule/javascript/lang/src/*.js` . In your `.tx/config` you can configure this path as a separate master location.
2017-08-03 02:51:32 +02:00
2017-10-27 04:38:27 +02:00
```ruby
[main]
host = https://www.transifex.com
2013-12-19 18:00:54 +01:00
2017-10-27 04:38:27 +02:00
[silverstripe-mymodule.master]
file_filter = lang/< lang > .yml
source_file = lang/en.yml
source_lang = en
type = YML
2013-12-19 18:00:54 +01:00
2017-10-27 04:38:27 +02:00
[silverstripe-mymodule.master-js]
file_filter = javascript/lang/src/< lang > .js
source_file = javascript/lang/src/en.js
source_lang = en
type = KEYVALUEJSON
2017-08-03 02:51:32 +02:00
```
2013-12-19 18:00:54 +01:00
2015-06-18 07:50:29 +02:00
Then you can upload the source files via a normal `tx push` . Once translations come in, you need to convert the source
2014-09-26 09:49:44 +02:00
files back into the JS files SilverStripe can actually read. This requires an installation of our
2020-11-26 00:06:37 +01:00
[buildtools ](https://github.com/silverstripe-archive/silverstripe-buildtools ).
2017-10-27 04:38:27 +02:00
2017-08-03 02:51:32 +02:00
```
2017-10-27 04:38:27 +02:00
tx pull
(cd .. & & phing -Dmodule=mymodule translation-generate-javascript-for-module)
git add javascript/lang/*
git commit -m "Updated javascript translations"
2017-08-03 02:51:32 +02:00
```
2017-10-27 04:38:27 +02:00
2012-10-09 03:44:30 +02:00
# Related
2012-04-27 17:51:38 +02:00
2015-02-28 01:09:15 +01:00
* [i18n ](/developer_guides/i18n/ ): Developer-level documentation of Silverstripe's i18n capabilities
* [Contributing Translations ](/contributing/translations ): Information for translators looking to contribute translations of the SilverStripe UI.
2012-04-27 17:51:38 +02:00
* [translatable ](https://github.com/silverstripe/silverstripe-translatable ): DataObject-interface powering the website-content translations
2020-11-26 00:06:37 +01:00
* ["Translatable ModelAdmin" module ](https://github.com/silverstripe-archive/silverstripe-translatablemodeladmin ): An extension which allows translations of DataObjects inside ModelAdmin