summaryrefslogtreecommitdiff
path: root/doc/development/i18n_guide.md
blob: 44eca68aacaad5d654ff8dbd4deb14107d0f1e9c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# Internationalization for GitLab

> [Introduced](https://gitlab.com/gitlab-org/gitlab-ce/merge_requests/10669) in GitLab 9.2.

For working with internationalization (i18n) we use
[GNU gettext](https://www.gnu.org/software/gettext/) given it's the most used
tool for this task and we have a lot of applications that will help us to work
with it.

## Tools

We use a couple of gems:

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:
    - `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.

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:
    - `rake gettext:po_to_json`: Reads the contents from the PO files and
      generates JSON files containing all the available translations.

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.
1. JavaScript files: we mostly need to work with VUE JS templates.

### 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!")
```

### 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

In JavaScript we added the `__()` (double underscore parenthesis) function
for translations.

### Updating the PO files with the new content

Now that the new content is marked for translation, we need to update the PO
files with the following command:

```sh
bundle exec rake gettext:find
```

This command will update the `locale/**/gitlab.edit.po` file with the
new content that the parser has found.

New translations will be added with their default content and will be marked
fuzzy. To use the translation, look for the `#, fuzzy` mention in `gitlab.edit.po`
and remove it.

Translations that aren't used in the source code anymore will be marked with
`~#`; these can be removed to keep our translation files clutter-free.

## Working with special content

### Interpolation

- In Ruby/HAML:

    ```ruby
    _("Hello %{name}") % { name: 'Joe' }
    ```

- In JavaScript: Not supported at this moment.

### Plurals

- In Ruby/HAML:

    ```ruby
    n_('Apple', 'Apples', 3) => 'Apples'
    ```

    Using interpolation:
    ```ruby
    n_("There is a mouse.", "There are %d mice.", size) % size
    ```

- In JavaScript:

    ```js
    n__('Apple', 'Apples', 3) => 'Apples'
    ```

    Using interpolation:

    ```js
    n__('Last day', 'Last %d days', 30) => 'Last 30 days'
    ```

### 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).

- In Ruby/HAML:

    ```ruby
    s_('OpenedNDaysAgo|Opened')
    ```

    In case the translation is not found it will return `Opened`.

- In JavaScript:

    ```js
    s__('OpenedNDaysAgo|Opened')
    ```

### Just marking content for parsing

Sometimes there are some dynamic translations that can't be found by the
parser when running `bundle exec 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).

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).

## 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`:

    ```ruby
    ...
    AVAILABLE_LANGUAGES = {
      ...,
      'fr' => 'Français'
    }.freeze
    ...
    ```

1. Next, you need to add the language:

    ```sh
    bundle exec rake gettext:add_language[fr]
    ```

    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:

    ```sh
    bundle exec rake gettext:add_language[en_gb]
    ```

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:

    ```sh
    bundle exec rake gettext:pack
    bundle exec rake gettext:po_to_json
    ```

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:

    ```sh
    git add locale/fr/ app/assets/javascripts/locale/fr/
    git commit -m "Add French translations for Cycle Analytics page"
    ```