2017-05-16 18:26:12 +00:00
# Internationalization for GitLab
2020-02-06 15:09:11 +00:00
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/10669) in GitLab 9.2.
2017-05-16 18:26:12 +00:00
2017-10-09 09:36:07 +00:00
For working with internationalization (i18n),
[GNU gettext ](https://www.gnu.org/software/gettext/ ) is used given it's the most
used tool for this task and there are a lot of applications that will help us to
work with it.
2017-05-16 18:26:12 +00:00
2017-05-25 19:21:53 +00:00
## Setting up GitLab Development Kit (GDK)
2019-09-18 14:02:45 +00:00
In order to be able to work on the [GitLab Community Edition ](https://gitlab.com/gitlab-org/gitlab-foss )
2017-10-09 09:36:07 +00:00
project you must download and configure it through [GDK ](https://gitlab.com/gitlab-org/gitlab-development-kit/blob/master/doc/set-up-gdk.md ).
2017-05-25 19:21:53 +00:00
2017-10-09 09:36:07 +00:00
Once you have the GitLab project ready, you can start working on the translation.
2017-05-25 19:21:53 +00:00
2017-05-16 18:26:12 +00:00
## Tools
2017-10-09 09:36:07 +00:00
The following tools are used:
2017-05-16 18:26:12 +00:00
1. [`gettext_i18n_rails` ](https://github.com/grosser/gettext_i18n_rails ): this
gem allow us to translate content from models, views and controllers. Also
it gives us access to the following raketasks:
2019-07-31 08:28:51 +00:00
- `rake gettext:find` : Parses almost all the files from the
Rails application looking for content that has been marked for
translation. Finally, it updates the PO files with the new content that
it has found.
- `rake gettext:pack` : Processes the PO files and generates the
MO files that are binary and are finally used by the application.
2017-05-16 18:26:12 +00:00
1. [`gettext_i18n_rails_js` ](https://github.com/webhippie/gettext_i18n_rails_js ):
this gem is useful to make the translations available in JavaScript. It
provides the following raketask:
2019-07-31 08:28:51 +00:00
- `rake gettext:po_to_json` : Reads the contents from the PO files and
generates JSON files containing all the available translations.
2017-05-16 18:26:12 +00:00
1. PO editor: there are multiple applications that can help us to work with PO
files, a good option is [Poedit ](https://poedit.net/download ) which is
available for macOS, GNU/Linux and Windows.
## Preparing a page for translation
We basically have 4 types of files:
1. Ruby files: basically Models and Controllers.
1. HAML files: these are the view files.
1. ERB files: used for email templates.
2018-03-08 16:02:17 +00:00
1. JavaScript files: we mostly need to work with Vue templates.
2017-05-16 18:26:12 +00:00
### Ruby files
If there is a method or variable that works with a raw string, for instance:
```ruby
def hello
"Hello world!"
end
```
Or:
```ruby
hello = "Hello world!"
```
You can easily mark that content for translation with:
```ruby
def hello
_("Hello world!")
end
```
Or:
```ruby
hello = _("Hello world!")
```
2020-01-24 00:08:51 +00:00
Be careful when translating strings at the class or module level since these would only be
evaluated once at class load time.
For example:
```ruby
validates :group_id, uniqueness: { scope: [:project_id], message: _("already shared with this group") }
```
This would be translated when the class is loaded and result in the error message
always being in the default locale.
Active Record's `:message` option accepts a `Proc` , so we can do this instead:
```ruby
validates :group_id, uniqueness: { scope: [:project_id], message: -> (object, data) { _("already shared with this group") } }
```
2019-06-19 06:29:03 +00:00
NOTE: **Note:** Messages in the API (`lib/api/` or `app/graphql` ) do
not need to be externalised.
2017-05-16 18:26:12 +00:00
### HAML files
Given the following content in HAML:
```haml
%h1 Hello world!
```
You can mark that content for translation with:
```haml
%h1= _("Hello world!")
```
### ERB files
Given the following content in ERB:
```erb
< h1 > Hello world!< / h1 >
```
You can mark that content for translation with:
```erb
< h1 > < %= _("Hello world!") %>< / h1 >
```
### JavaScript files
2018-02-15 17:17:13 +00:00
In JavaScript we added the `__()` (double underscore parenthesis) function that
you can import from the `~/locale` file. For instance:
2017-05-16 18:26:12 +00:00
2018-02-15 17:17:13 +00:00
```js
import { __ } from '~/locale';
const label = __ ('Subscribe');
2017-07-21 06:55:45 +00:00
```
2018-02-15 17:17:13 +00:00
In order to test JavaScript translations you have to change the GitLab
localization to other language than English and you have to generate JSON files
using `bin/rake gettext:po_to_json` or `bin/rake gettext:compile` .
2017-10-24 04:16:57 +00:00
2018-02-15 17:17:13 +00:00
### Dynamic translations
2017-10-24 04:16:57 +00:00
Sometimes there are some dynamic translations that can't be found by the
2018-02-15 17:17:13 +00:00
parser when running `bin/rake gettext:find` . For these scenarios you can
use the [`N_` method ](https://github.com/grosser/gettext_i18n_rails/blob/c09e38d481e0899ca7d3fc01786834fa8e7aab97/Readme.md#unfound-translations-with-rake-gettextfind ).
2017-10-24 04:16:57 +00:00
There is also and alternative method to [translate messages from validation errors ](https://github.com/grosser/gettext_i18n_rails/blob/c09e38d481e0899ca7d3fc01786834fa8e7aab97/Readme.md#option-a ).
2018-02-15 17:17:13 +00:00
## Working with special content
2017-05-16 18:26:12 +00:00
### Interpolation
2018-01-25 13:36:04 +00:00
Placeholders in translated text should match the code style of the respective source file.
2019-06-28 02:54:22 +00:00
For example use `%{created_at}` in Ruby but `%{createdAt}` in JavaScript. Make sure to [avoid splitting sentences when adding links ](#avoid-splitting-sentences-when-adding-links ).
2018-01-25 13:36:04 +00:00
2017-05-16 18:26:12 +00:00
- In Ruby/HAML:
2019-07-31 08:28:51 +00:00
```ruby
_("Hello %{name}") % { name: 'Joe' } => 'Hello Joe'
```
2017-05-16 18:26:12 +00:00
2020-01-28 09:09:06 +00:00
- In Vue:
See the section on [Vue component interpolation ](#vue-components-interpolation ).
- In JavaScript (when Vue cannot be used):
2017-10-24 04:16:57 +00:00
2019-07-31 08:28:51 +00:00
```js
import { __ , sprintf } from '~/locale';
2018-01-25 13:36:04 +00:00
2019-07-31 08:28:51 +00:00
sprintf(__('Hello %{username}'), { username: 'Joe' }); // => 'Hello Joe'
```
2017-05-16 18:26:12 +00:00
2020-01-28 09:09:06 +00:00
If you want to use markup within the translation and are using Vue, you
**must** use the [`gl-sprintf` ](#vue-components-interpolation ) component. If
for some reason you cannot use Vue, use `sprintf` and stop it from escaping
placeholder values by passing `false` as its third argument. You **must**
escape any interpolated dynamic values yourself, for instance using
`escape` from `lodash` .
2018-01-25 13:36:04 +00:00
2019-07-31 08:28:51 +00:00
```js
2020-01-28 09:09:06 +00:00
import { escape } from 'lodash';
2019-07-31 08:28:51 +00:00
import { __ , sprintf } from '~/locale';
2018-01-25 13:36:04 +00:00
2020-01-28 09:09:06 +00:00
let someDynamicValue = '< script > alert ( "evil" ) < / script > ';
// Dangerous:
sprintf(__('This is %{value}'), { value: `<strong>${someDynamicValue}</strong>` , false);
// => 'This is < strong > < script > alert ( 'evil' ) < / script > < / strong > '
// Incorrect:
sprintf(__('This is %{value}'), { value: `<strong>${someDynamicValue}</strong>` });
// => 'This is < strong> < script> alert(' evil' )< /script> < /strong> '
// OK:
sprintf(__('This is %{value}'), { value: `<strong>${escape(someDynamicValue)}</strong>` , false);
// => 'This is < strong > < script> alert(' evil' )< /script> < / strong > '
2019-07-31 08:28:51 +00:00
```
2017-12-11 20:24:05 +00:00
2017-05-16 18:26:12 +00:00
### Plurals
- In Ruby/HAML:
2019-07-31 08:28:51 +00:00
```ruby
n_('Apple', 'Apples', 3)
# => 'Apples'
```
2017-05-16 18:26:12 +00:00
2019-07-31 08:28:51 +00:00
Using interpolation:
2017-05-16 18:26:12 +00:00
2019-07-31 08:28:51 +00:00
```ruby
n_("There is a mouse.", "There are %d mice.", size) % size
# => When size == 1: 'There is a mouse.'
# => When size == 2: 'There are 2 mice.'
```
Avoid using `%d` or count variables in singular strings. This allows more natural translation in some languages.
2018-06-30 12:20:29 +00:00
2017-05-16 18:26:12 +00:00
- In JavaScript:
2019-07-31 08:28:51 +00:00
```js
n__('Apple', 'Apples', 3)
// => 'Apples'
```
2017-05-16 18:26:12 +00:00
2019-07-31 08:28:51 +00:00
Using interpolation:
2017-05-16 18:26:12 +00:00
2019-07-31 08:28:51 +00:00
```js
n__('Last day', 'Last %d days', x)
// => When x == 1: 'Last day'
// => When x == 2: 'Last 2 days'
```
2017-05-16 18:26:12 +00:00
2020-01-22 06:08:33 +00:00
The `n_` method should only be used to fetch pluralized translations of the same
string, not to control the logic of showing different strings for different
quantities. Some languages have different quantities of target plural forms -
Chinese (simplified), for example, has only one target plural form in our
translation tool. This means the translator would have to choose to translate
only one of the strings and the translation would not behave as intended in the
other case.
For example, prefer to use:
```ruby
if selected_projects.one?
selected_projects.first.name
else
n__("Project selected", "%d projects selected", selected_projects.count)
end
```
rather than:
```ruby
# incorrect usage example
n_("%{project_name}", "%d projects selected", count) % { project_name: 'GitLab' }
```
2017-05-16 18:26:12 +00:00
### Namespaces
Sometimes you need to add some context to the text that you want to translate
(if the word occurs in a sentence and/or the word is ambiguous).
2019-05-23 08:11:28 +00:00
Namespaces should be PascalCase.
2017-05-16 18:26:12 +00:00
- In Ruby/HAML:
2019-07-31 08:28:51 +00:00
```ruby
s_('OpenedNDaysAgo|Opened')
```
2017-05-16 18:26:12 +00:00
2019-07-31 08:28:51 +00:00
In case the translation is not found it will return `Opened` .
2017-05-16 18:26:12 +00:00
- In JavaScript:
2019-07-31 08:28:51 +00:00
```js
s__('OpenedNDaysAgo|Opened')
```
2017-05-16 18:26:12 +00:00
2018-02-15 17:17:13 +00:00
Note: The namespace should be removed from the translation. See the [translation
2019-06-05 10:10:28 +00:00
guidelines for more details](translation.md#namespaced-strings).
2018-02-15 17:17:13 +00:00
2017-12-11 12:25:49 +00:00
### Dates / times
- In JavaScript:
```js
2018-02-15 17:17:13 +00:00
import { createDateTimeFormat } from '~/locale';
2017-12-11 12:25:49 +00:00
const dateFormat = createDateTimeFormat({ year: 'numeric', month: 'long', day: 'numeric' });
console.log(dateFormat.format(new Date('2063-04-05'))) // April 5, 2063
```
2020-04-03 12:09:52 +00:00
This makes use of [`Intl.DateTimeFormat` ](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/DateTimeFormat ).
2017-12-11 12:25:49 +00:00
2019-01-22 23:59:01 +00:00
- In Ruby/HAML, we have two ways of adding format to dates and times:
1. **Through the `l` helper** , i.e. `l(active_session.created_at, format: :short)` . We have some predefined formats for
2019-09-18 18:06:14 +00:00
[dates ](https://gitlab.com/gitlab-org/gitlab/blob/4ab54c2233e91f60a80e5b6fa2181e6899fdcc3e/config/locales/en.yml#L54 ) and [times ](https://gitlab.com/gitlab-org/gitlab/blob/4ab54c2233e91f60a80e5b6fa2181e6899fdcc3e/config/locales/en.yml#L262 ).
2019-07-31 08:28:51 +00:00
If you need to add a new format, because other parts of the code could benefit from it,
2019-09-18 18:06:14 +00:00
you'll need to add it to [en.yml ](https://gitlab.com/gitlab-org/gitlab/blob/master/config/locales/en.yml ) file.
2019-01-31 02:45:38 +00:00
1. **Through `strftime`** , i.e. `milestone.start_date.strftime('%b %-d')` . We use `strftime` in case none of the formats
2019-09-18 18:06:14 +00:00
defined on [en.yml ](https://gitlab.com/gitlab-org/gitlab/blob/master/config/locales/en.yml ) matches the date/time
2019-07-31 08:28:51 +00:00
specifications we need, and if there is no need to add it as a new format because is very particular (i.e. it's only used in a single view).
2019-01-22 23:59:01 +00:00
2018-02-15 17:17:13 +00:00
## Best practices
### Splitting sentences
Please never split a sentence as that would assume the sentence grammar and
structure is the same in all languages.
2018-06-12 12:29:52 +00:00
For instance, the following:
2018-02-15 17:17:13 +00:00
```js
{{ s__("mrWidget|Set by") }}
2018-02-28 17:47:02 +00:00
{{ author.name }}
2018-02-15 17:17:13 +00:00
{{ s__("mrWidget|to be merged automatically when the pipeline succeeds") }}
```
should be externalized as follows:
```js
2018-02-28 17:47:02 +00:00
{{ sprintf(s__("mrWidget|Set by %{author} to be merged automatically when the pipeline succeeds"), { author: author.name }) }}
2018-02-15 17:17:13 +00:00
```
2018-06-12 12:29:52 +00:00
#### Avoid splitting sentences when adding links
This also applies when using links in between translated sentences, otherwise these texts are not translatable in certain languages.
2019-06-28 02:54:22 +00:00
- In Ruby/HAML, instead of:
2019-07-31 08:28:51 +00:00
```haml
- zones_link = link_to(s_('ClusterIntegration|zones'), 'https://cloud.google.com/compute/docs/regions-zones/regions-zones', target: '_blank', rel: 'noopener noreferrer')
= s_('ClusterIntegration|Learn more about %{zones_link}').html_safe % { zones_link: zones_link }
```
Set the link starting and ending HTML fragments as variables like so:
```haml
- zones_link_url = 'https://cloud.google.com/compute/docs/regions-zones/regions-zones'
- zones_link_start = '< a href = "%{url}" target = "_blank" rel = "noopener noreferrer" > '.html_safe % { url: zones_link_url }
= s_('ClusterIntegration|Learn more about %{zones_link_start}zones%{zones_link_end}').html_safe % { zones_link_start: zones_link_start, zones_link_end: '< / a > '.html_safe }
```
2018-06-12 12:29:52 +00:00
2020-01-28 09:09:06 +00:00
- In Vue, instead of:
```html
< template >
< div >
< gl-sprintf :message = "s__('ClusterIntegration|Learn more about %{link}')" >
< template #link >
< gl-link
href="https://cloud.google.com/compute/docs/regions-zones/regions-zones"
target="_blank"
>zones< / gl-link >
< / template >
< / gl-sprintf >
< / div >
< / template >
```
Set the link starting and ending HTML fragments as placeholders like so:
```html
< template >
< div >
< gl-sprintf :message = "s__('ClusterIntegration|Learn more about %{linkStart}zones%{linkEnd}')" >
< template #link ="{ content }" >
< gl-link
href="https://cloud.google.com/compute/docs/regions-zones/regions-zones"
target="_blank"
>{{ content }}< / gl-link >
< / template >
< / gl-sprintf >
< / div >
< / template >
```
- In JavaScript (when Vue cannot be used), instead of:
2018-06-12 12:29:52 +00:00
2019-07-31 08:28:51 +00:00
```js
{{
sprintf(s__("ClusterIntegration|Learn more about %{link}"), {
link: '< a href = "https://cloud.google.com/compute/docs/regions-zones/regions-zones" target = "_blank" rel = "noopener noreferrer" > zones< / a > '
})
}}
```
2020-01-28 09:09:06 +00:00
Set the link starting and ending HTML fragments as placeholders like so:
2019-07-31 08:28:51 +00:00
```js
{{
sprintf(s__("ClusterIntegration|Learn more about %{linkStart}zones%{linkEnd}"), {
2020-01-24 09:08:32 +00:00
linkStart: '< a href = "https://cloud.google.com/compute/docs/regions-zones/regions-zones" target = "_blank" rel = "noopener noreferrer" > ',
2019-07-31 08:28:51 +00:00
linkEnd: '< / a > ',
})
}}
```
2018-06-12 12:29:52 +00:00
The reasoning behind this is that in some languages words change depending on context. For example in Japanese は is added to the subject of a sentence and を to the object. This is impossible to translate correctly if we extract individual words from the sentence.
2018-02-15 17:17:13 +00:00
When in doubt, try to follow the best practices described in this [Mozilla
2020-04-03 12:09:52 +00:00
Developer documentation](https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_content_best_practices#Splitting).
2018-02-15 17:17:13 +00:00
2019-11-26 03:06:24 +00:00
##### Vue components interpolation
When translating UI text in Vue components, you might want to include child components inside
the translation string.
You could not use a JavaScript-only solution to render the translation,
because Vue would not be aware of the child components and would render them as plain text.
For this use case, you should use the `gl-sprintf` component which is maintained
in **GitLab UI** .
The `gl-sprintf` component accepts a `message` property, which is the translatable string,
and it exposes a named slot for every placeholder in the string, which lets you include Vue
components easily.
Assume you want to print the translatable string
`Pipeline %{pipelineId} triggered %{timeago} by %{author}` . To replace the `%{timeago}` and
`%{author}` placeholders with Vue components, here's how you would do that with `gl-sprintf` :
```html
< template >
< div >
< gl-sprintf :message = "__('Pipeline %{pipelineId} triggered %{timeago} by %{author}')" >
< template #pipelineId > {{ pipeline.id }}</ template >
< template #timeago >
< timeago :time = "pipeline.triggerTime" / >
< / template >
< template #author >
< gl-avatar-labeled
:src="pipeline.triggeredBy.avatarPath"
:label="pipeline.triggeredBy.name"
/>
< / template >
< / gl-sprintf >
< / div >
< / template >
```
For more information, see the [`gl-sprintf` ](https://gitlab-org.gitlab.io/gitlab-ui/?path=/story/base-sprintf--default ) documentation.
2018-02-15 17:17:13 +00:00
## Updating the PO files with the new content
2019-09-25 03:06:21 +00:00
Now that the new content is marked for translation, we need to update
`locale/gitlab.pot` files with the following command:
2018-02-15 17:17:13 +00:00
2020-01-30 15:09:15 +00:00
```shell
2018-07-06 12:40:23 +00:00
bin/rake gettext:regenerate
2018-02-15 17:17:13 +00:00
```
2019-09-25 03:06:21 +00:00
This command will update `locale/gitlab.pot` file with the newly externalized
2018-02-15 17:17:13 +00:00
strings and remove any strings that aren't used anymore. You should check this
file in. Once the changes are on master, they will be picked up by
2019-09-27 12:06:07 +00:00
[Crowdin ](https://translate.gitlab.com ) and be presented for
2019-09-25 03:06:21 +00:00
translation.
2019-10-25 03:06:02 +00:00
We don't need to check in any changes to the `locale/[language]/gitlab.po` files.
They are updated automatically when [translations from Crowdin are merged ](merging_translations.md ).
2018-02-15 17:17:13 +00:00
If there are merge conflicts in the `gitlab.pot` file, you can delete the file
2019-04-25 12:27:35 +00:00
and regenerate it using the same command.
2018-02-15 17:17:13 +00:00
### Validating PO files
To make sure we keep our translation files up to date, there's a linter that is
running on CI as part of the `static-analysis` job.
To lint the adjustments in PO files locally you can run `rake gettext:lint` .
The linter will take the following into account:
- Valid PO-file syntax
- Variable usage
- Only one unnamed (`%d`) variable, since the order of variables might change
in different languages
- All variables used in the message-id are used in the translation
- There should be no variables used in a translation that aren't in the
message-id
- Errors during translation.
The errors are grouped per file, and per message ID:
2020-03-25 06:07:58 +00:00
```plaintext
2018-02-15 17:17:13 +00:00
Errors in `locale/zh_HK/gitlab.po` :
PO-syntax errors
SimplePoParser::ParserErrorSyntax error in lines
Syntax error in msgctxt
Syntax error in msgid
Syntax error in msgstr
Syntax error in message_line
There should be only whitespace until the end of line after the double quote character of a message text.
2019-06-06 16:45:21 +00:00
Parsing result before error: '{:msgid=>["", "You are going to remove %{project_name_with_namespace}.\\n", "Removed project CANNOT be restored!\\n", "Are you ABSOLUTELY sure?"]}'
2018-02-15 17:17:13 +00:00
SimplePoParser filtered backtrace: SimplePoParser::ParserError
Errors in `locale/zh_TW/gitlab.po` :
1 pipeline
< %d 條流水線> is using unknown variables: [%d]
Failure translating to zh_TW with []: too few arguments
```
In this output the `locale/zh_HK/gitlab.po` has syntax errors.
The `locale/zh_TW/gitlab.po` has variables that are used in the translation that
aren't in the message with id `1 pipeline` .
2017-05-16 18:26:12 +00:00
## Adding a new language
Let's suppose you want to add translations for a new language, let's say French.
1. The first step is to register the new language in `lib/gitlab/i18n.rb` :
2019-07-31 08:28:51 +00:00
```ruby
...
AVAILABLE_LANGUAGES = {
...,
'fr' => 'Français'
}.freeze
...
```
2017-05-16 18:26:12 +00:00
1. Next, you need to add the language:
2020-01-30 15:09:15 +00:00
```shell
2019-07-31 08:28:51 +00:00
bin/rake gettext:add_language[fr]
```
2017-05-16 18:26:12 +00:00
2019-07-31 08:28:51 +00:00
If you want to add a new language for a specific region, the command is similar,
you just need to separate the region with an underscore (`_`). For example:
2017-05-16 18:26:12 +00:00
2020-01-30 15:09:15 +00:00
```shell
2019-07-31 08:28:51 +00:00
bin/rake gettext:add_language[en_GB]
```
2017-05-16 18:26:12 +00:00
2019-07-31 08:28:51 +00:00
Please note that you need to specify the region part in capitals.
2017-05-25 19:21:53 +00:00
2017-05-16 18:26:12 +00:00
1. Now that the language is added, a new directory has been created under the
path: `locale/fr/` . You can now start using your PO editor to edit the PO file
located in: `locale/fr/gitlab.edit.po` .
1. After you're done updating the translations, you need to process the PO files
in order to generate the binary MO files and finally update the JSON files
containing the translations:
2020-01-30 15:09:15 +00:00
```shell
2019-07-31 08:28:51 +00:00
bin/rake gettext:compile
```
2017-05-16 18:26:12 +00:00
1. In order to see the translated content we need to change our preferred language
which can be found under the user's **Settings** (`/profile`).
1. After checking that the changes are ok, you can proceed to commit the new files.
For example:
2020-01-30 15:09:15 +00:00
```shell
2019-07-31 08:28:51 +00:00
git add locale/fr/ app/assets/javascripts/locale/fr/
2020-02-07 21:08:39 +00:00
git commit -m "Add French translations for Value Stream Analytics page"
2019-07-31 08:28:51 +00:00
```