debian-mirror-gitlab/doc/development/i18n/externalization.md

856 lines
25 KiB
Markdown
Raw Normal View History

2021-01-29 00:20:46 +05:30
---
stage: Manage
group: Import
2021-02-22 17:27:13 +05:30
info: To determine the technical writer assigned to the Stage/Group associated with this page, see https://about.gitlab.com/handbook/engineering/ux/technical-writing/#assignments
2021-01-29 00:20:46 +05:30
---
2018-03-17 18:26:18 +05:30
# Internationalization for GitLab
2020-03-13 15:44:24 +05:30
> [Introduced](https://gitlab.com/gitlab-org/gitlab-foss/-/merge_requests/10669) in GitLab 9.2.
2018-03-17 18:26:18 +05:30
For working with internationalization (i18n),
[GNU gettext](https://www.gnu.org/software/gettext/) is used given it's the most
2021-02-22 17:27:13 +05:30
used tool for this task and there are a lot of applications that help us
2018-03-17 18:26:18 +05:30
work with it.
2021-02-22 17:27:13 +05:30
NOTE:
2020-11-24 15:15:51 +05:30
All `rake` commands described on this page must be run on a GitLab instance, usually GDK.
2018-03-17 18:26:18 +05:30
## Setting up GitLab Development Kit (GDK)
2019-12-04 20:38:33 +05:30
In order to be able to work on the [GitLab Community Edition](https://gitlab.com/gitlab-org/gitlab-foss)
2021-06-08 01:23:25 +05:30
project you must download and configure it through [GDK](https://gitlab.com/gitlab-org/gitlab-development-kit/blob/main/doc/set-up-gdk.md).
2018-03-17 18:26:18 +05:30
2021-01-29 00:20:46 +05:30
After you have the GitLab project ready, you can start working on the translation.
2018-03-17 18:26:18 +05:30
## Tools
The following tools are used:
1. [`gettext_i18n_rails`](https://github.com/grosser/gettext_i18n_rails): this
gem allow us to translate content from models, views and controllers. Also
2020-05-24 23:13:21 +05:30
it gives us access to the following Rake tasks:
2019-10-12 21:52:04 +05:30
- `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.
2018-03-17 18:26:18 +05:30
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
2020-05-24 23:13:21 +05:30
provides the following Rake task:
2019-10-12 21:52:04 +05:30
- `rake gettext:po_to_json`: Reads the contents from the PO files and
generates JSON files containing all the available translations.
2018-03-17 18:26:18 +05:30
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-05-09 12:01:36 +05:30
1. JavaScript files: we mostly need to work with Vue templates.
2018-03-17 18:26:18 +05:30
### 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-03-13 15:44:24 +05:30
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") } }
```
2020-07-28 23:09:34 +05:30
Messages in the API (`lib/api/` or `app/graphql`) do
2021-01-29 00:20:46 +05:30
not need to be externalized.
2019-09-30 21:07:59 +05:30
2018-03-17 18:26:18 +05:30
### 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-03-27 19:54:05 +05:30
In JavaScript we added the `__()` (double underscore parenthesis) function that
you can import from the `~/locale` file. For instance:
2018-03-17 18:26:18 +05:30
2020-05-24 23:13:21 +05:30
```javascript
2018-03-27 19:54:05 +05:30
import { __ } from '~/locale';
const label = __('Subscribe');
2018-03-17 18:26:18 +05:30
```
2018-03-27 19:54:05 +05:30
In order to test JavaScript translations you have to change the GitLab
2020-11-24 15:15:51 +05:30
localization to another language than English and you have to generate JSON files
using `bin/rake gettext:po_to_json` or `bin/rake gettext:compile`.
### Vue files
In Vue files we make both the `__()` (double underscore parenthesis) function and the `s__()` (namespaced double underscore parenthesis) function available that you can import from the `~/locale` file. For instance:
```javascript
import { __, s__ } from '~/locale';
const label = __('Subscribe');
const nameSpacedlabel = __('Plan|Subscribe');
```
For the static text strings we suggest two patterns for using these translations in Vue files:
- External constants file:
```javascript
javascripts
└───alert_settings
│ │ constants.js
│ └───components
│ │ alert_settings_form.vue
// constants.js
import { s__ } from '~/locale';
/* Integration constants */
export const I18N_ALERT_SETTINGS_FORM = {
saveBtnLabel: __('Save changes'),
};
// alert_settings_form.vue
import {
I18N_ALERT_SETTINGS_FORM,
} from '../constants';
<script>
export default {
i18n: {
I18N_ALERT_SETTINGS_FORM,
}
}
</script>
<template>
<gl-button
ref="submitBtn"
variant="success"
type="submit"
>
{{ $options.i18n.I18N_ALERT_SETTINGS_FORM }}
</gl-button>
</template>
```
When possible, you should opt for this pattern, as this allows you to import these strings directly into your component specs for re-use during testing.
2021-01-03 14:25:43 +05:30
- Internal component `$options` object:
2020-11-24 15:15:51 +05:30
```javascript
<script>
export default {
i18n: {
buttonLabel: s__('Plan|Button Label')
}
},
</script>
<template>
<gl-button :aria-label="$options.i18n.buttonLabel">
{{ $options.i18n.buttonLabel }}
</gl-button>
</template>
```
In order to visually test the Vue translations you have to change the GitLab
localization to another language than English and you have to generate JSON files
2018-03-27 19:54:05 +05:30
using `bin/rake gettext:po_to_json` or `bin/rake gettext:compile`.
2018-03-17 18:26:18 +05:30
2018-03-27 19:54:05 +05:30
### Dynamic translations
2018-03-17 18:26:18 +05:30
Sometimes there are some dynamic translations that can't be found by the
2018-03-27 19:54:05 +05:30
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).
2018-03-17 18:26:18 +05:30
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-03-27 19:54:05 +05:30
## Working with special content
2018-03-17 18:26:18 +05:30
### Interpolation
2018-10-15 14:42:47 +05:30
Placeholders in translated text should match the code style of the respective source file.
2019-09-30 21:07:59 +05:30
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-10-15 14:42:47 +05:30
2018-03-17 18:26:18 +05:30
- In Ruby/HAML:
2019-10-12 21:52:04 +05:30
```ruby
_("Hello %{name}") % { name: 'Joe' } => 'Hello Joe'
```
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
- In Vue:
2021-04-29 21:17:54 +05:30
Use the [`GlSprintf`](https://gitlab-org.gitlab.io/gitlab-ui/?path=/docs/utilities-sprintf--sentence-with-link) component if:
- you need to include child components in the translation string.
- you need to include HTML in your translation string.
- you are using `sprintf` and need to pass `false` as the third argument to
prevent it from escaping placeholder values.
For example:
```html
<gl-sprintf :message="s__('ClusterIntegration|Learn more about %{linkStart}zones%{linkEnd}')">
<template #link="{ content }">
<gl-link :href="somePath">{{ content }}</gl-link>
</template>
</gl-sprintf>
```
In other cases it may be simpler to use `sprintf`, perhaps in a computed
property. For example:
```html
<script>
import { __, sprintf } from '~/locale';
export default {
...
computed: {
userWelcome() {
sprintf(__('Hello %{username}'), { username: this.user.name });
}
}
...
}
</script>
<template>
<span>{{ userWelcome }}</span>
</template>
```
2020-03-13 15:44:24 +05:30
- In JavaScript (when Vue cannot be used):
2018-03-17 18:26:18 +05:30
2020-05-24 23:13:21 +05:30
```javascript
2019-10-12 21:52:04 +05:30
import { __, sprintf } from '~/locale';
2018-10-15 14:42:47 +05:30
2019-10-12 21:52:04 +05:30
sprintf(__('Hello %{username}'), { username: 'Joe' }); // => 'Hello Joe'
```
2018-03-17 18:26:18 +05:30
2021-04-29 21:17:54 +05:30
If you need to use markup within the translation, 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-10-15 14:42:47 +05:30
2020-05-24 23:13:21 +05:30
```javascript
2020-03-13 15:44:24 +05:30
import { escape } from 'lodash';
2019-10-12 21:52:04 +05:30
import { __, sprintf } from '~/locale';
2018-10-15 14:42:47 +05:30
2020-03-13 15:44:24 +05:30
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 &lt;strong&gt;&lt;script&gt;alert(&#x27;evil&#x27;)&lt;/script&gt;&lt;/strong&gt;'
// OK:
2020-10-24 23:57:45 +05:30
sprintf(__('This is %{value}'), { value: `<strong>${escape(someDynamicValue)}</strong>` }, false);
2020-03-13 15:44:24 +05:30
// => 'This is <strong>&lt;script&gt;alert(&#x27;evil&#x27;)&lt;/script&gt;</strong>'
2019-10-12 21:52:04 +05:30
```
2018-03-17 18:26:18 +05:30
### Plurals
- In Ruby/HAML:
2019-10-12 21:52:04 +05:30
```ruby
n_('Apple', 'Apples', 3)
# => 'Apples'
```
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
Using interpolation:
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
```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-11-08 19:23:39 +05:30
2018-03-17 18:26:18 +05:30
- In JavaScript:
2020-05-24 23:13:21 +05:30
```javascript
2019-10-12 21:52:04 +05:30
n__('Apple', 'Apples', 3)
// => 'Apples'
```
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
Using interpolation:
2018-03-17 18:26:18 +05:30
2020-05-24 23:13:21 +05:30
```javascript
2019-10-12 21:52:04 +05:30
n__('Last day', 'Last %d days', x)
// => When x == 1: 'Last day'
// => When x == 2: 'Last 2 days'
```
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
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' }
```
2018-03-17 18:26:18 +05:30
### Namespaces
2020-06-23 00:09:42 +05:30
A namespace is a way to group translations that belong together. They provide context to our translators by adding a prefix followed by the bar symbol (`|`). For example:
```ruby
'Namespace|Translated string'
```
A namespace provide the following benefits:
- It addresses ambiguity in words, for example: `Promotions|Promote` vs `Epic|Promote`
- It allows translators to focus on translating externalized strings that belong to the same product area rather than arbitrary ones.
- It gives a linguistic context to help the translator.
In some cases, namespaces don't make sense, for example,
for ubiquitous UI words and phrases such as "Cancel" or phrases like "Save changes" a namespace could
be counterproductive.
2019-09-04 21:01:54 +05:30
Namespaces should be PascalCase.
2018-03-17 18:26:18 +05:30
- In Ruby/HAML:
2019-10-12 21:52:04 +05:30
```ruby
s_('OpenedNDaysAgo|Opened')
```
2018-03-17 18:26:18 +05:30
2021-02-22 17:27:13 +05:30
In case the translation is not found it returns `Opened`.
2018-03-17 18:26:18 +05:30
- In JavaScript:
2020-05-24 23:13:21 +05:30
```javascript
2019-10-12 21:52:04 +05:30
s__('OpenedNDaysAgo|Opened')
```
2018-03-17 18:26:18 +05:30
2021-01-29 00:20:46 +05:30
The namespace should be removed from the translation. See the
[translation guidelines for more details](translation.md#namespaced-strings).
2018-03-27 19:54:05 +05:30
2020-10-24 23:57:45 +05:30
### HTML
We no longer include HTML directly in the strings that are submitted for translation. This is for a couple of reasons:
1. It introduces a chance for the translated string to accidentally include invalid HTML.
1. It introduces a security risk where translated strings become an attack vector for XSS, as noted by the
[Open Web Application Security Project (OWASP)](https://owasp.org/www-community/attacks/xss/).
To include formatting in the translated string, we can do the following:
- In Ruby/HAML:
```ruby
html_escape(_('Some %{strongOpen}bold%{strongClose} text.')) % { strongOpen: '<strong>'.html_safe, strongClose: '</strong>'.html_safe }
# => 'Some <strong>bold</strong> text.'
```
- In JavaScript:
```javascript
sprintf(__('Some %{strongOpen}bold%{strongClose} text.'), { strongOpen: '<strong>', strongClose: '</strong>'}, false);
// => 'Some <strong>bold</strong> text.'
```
- In Vue
See the section on [interpolation](#interpolation).
2021-02-22 17:27:13 +05:30
When [this translation helper issue](https://gitlab.com/gitlab-org/gitlab/-/issues/217935) is complete, we plan to update the
2020-10-24 23:57:45 +05:30
process of including formatting in translated strings.
#### Including Angle Brackets
2021-02-22 17:27:13 +05:30
If a string contains angles brackets (`<`/`>`) that are not used for HTML, it is still flagged by the
2020-10-24 23:57:45 +05:30
`rake gettext:lint` linter.
To avoid this error, use the applicable HTML entity code (`&lt;` or `&gt;`) instead:
- In Ruby/HAML:
```ruby
html_escape_once(_('In &lt; 1 hour')).html_safe
# => 'In < 1 hour'
```
- In JavaScript:
```javascript
2021-01-03 14:25:43 +05:30
import { sanitize } from '~/lib/dompurify';
2020-10-24 23:57:45 +05:30
2020-11-24 15:15:51 +05:30
const i18n = { LESS_THAN_ONE_HOUR: sanitize(__('In &lt; 1 hour'), { ALLOWED_TAGS: [] }) };
2020-10-24 23:57:45 +05:30
// ... using the string
element.innerHTML = i18n.LESS_THAN_ONE_HOUR;
// => 'In < 1 hour'
```
- In Vue:
```vue
<gl-sprintf :message="s__('In &lt; 1 hours')"/>
// => 'In < 1 hour'
```
2021-06-08 01:23:25 +05:30
### Numbers
Different locales may use different number formats. To support localization of numbers, we use `formatNumber`,
which leverages [`toLocaleString()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toLocaleString).
`formatNumber` formats numbers as strings using the current user locale by default.
- In JavaScript
```javascript
import { formatNumber } from '~/locale';
// Assuming "User Preferences > Language" is set to "English":
const tenThousand = formatNumber(10000); // "10,000" (uses comma as decimal symbol in English locale)
const fiftyPercent = formatNumber(0.5, { style: 'percent' }) // "50%" (other options are passed to toLocaleString)
```
- In Vue templates
```html
<script>
import { formatNumber } from '~/locale';
export default {
//...
methods: {
// ...
formatNumber,
},
}
</script>
<template>
<div class="my-number">
{{ formatNumber(10000) }} <!-- 10,000 -->
</div>
<div class="my-percent">
{{ formatNumber(0.5, { style: 'percent' }) }} <!-- 50% -->
</div>
</template>
```
2018-03-17 18:26:18 +05:30
### Dates / times
- In JavaScript:
2020-05-24 23:13:21 +05:30
```javascript
2018-03-27 19:54:05 +05:30
import { createDateTimeFormat } from '~/locale';
2018-03-17 18:26:18 +05:30
const dateFormat = createDateTimeFormat({ year: 'numeric', month: 'long', day: 'numeric' });
console.log(dateFormat.format(new Date('2063-04-05'))) // April 5, 2063
```
2020-06-23 00:09:42 +05:30
This makes use of [`Intl.DateTimeFormat`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/DateTimeFormat).
2018-03-17 18:26:18 +05:30
2019-03-02 22:35:43 +05:30
- 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-12-21 20:55:43 +05:30
[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-10-12 21:52:04 +05:30
If you need to add a new format, because other parts of the code could benefit from it,
2021-02-22 17:27:13 +05:30
you can add it to [en.yml](https://gitlab.com/gitlab-org/gitlab/blob/master/config/locales/en.yml) file.
2019-03-02 22:35:43 +05:30
1. **Through `strftime`**, i.e. `milestone.start_date.strftime('%b %-d')`. We use `strftime` in case none of the formats
2019-12-21 20:55:43 +05:30
defined on [en.yml](https://gitlab.com/gitlab-org/gitlab/blob/master/config/locales/en.yml) matches the date/time
2019-10-12 21:52:04 +05:30
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-03-02 22:35:43 +05:30
2018-03-27 19:54:05 +05:30
## Best practices
2021-01-29 00:20:46 +05:30
### Minimize translation updates
Updates can result in the loss of the translations for this string. To minimize risks,
avoid changes to strings, unless they:
- Add value to the user.
- Include extra context for translators.
For example, we should avoid changes like this:
```diff
- _('Number of things: %{count}') % { count: 10 }
+ n_('Number of things: %d', 10)
```
2020-04-22 19:07:51 +05:30
### Keep translations dynamic
There are cases when it makes sense to keep translations together within an array or a hash.
Examples:
- Mappings for a dropdown list
- Error messages
2021-02-22 17:27:13 +05:30
To store these kinds of data, using a constant seems like the best choice, however this doesn't work for translations.
2020-04-22 19:07:51 +05:30
Bad, avoid it:
```ruby
class MyPresenter
MY_LIST = {
key_1: _('item 1'),
key_2: _('item 2'),
key_3: _('item 3')
}
end
```
2021-02-22 17:27:13 +05:30
The translation method (`_`) is called when the class is loaded for the first time and translates the text to the default locale. Regardless of the user's locale, these values are not translated a second time.
2020-04-22 19:07:51 +05:30
Similar thing happens when using class methods with memoization.
Bad, avoid it:
```ruby
class MyModel
def self.list
@list ||= {
key_1: _('item 1'),
key_2: _('item 2'),
key_3: _('item 3')
}
end
end
```
2021-02-22 17:27:13 +05:30
This method memorizes the translations using the locale of the user, who first "called" this method.
2020-04-22 19:07:51 +05:30
To avoid these problems, keep the translations dynamic.
Good:
```ruby
class MyPresenter
def self.my_list
{
key_1: _('item 1'),
key_2: _('item 2'),
key_3: _('item 3')
}.freeze
end
end
```
2018-03-27 19:54:05 +05:30
### Splitting sentences
Please never split a sentence as that would assume the sentence grammar and
structure is the same in all languages.
2018-11-08 19:23:39 +05:30
For instance, the following:
2018-03-27 19:54:05 +05:30
2020-05-24 23:13:21 +05:30
```javascript
2018-03-27 19:54:05 +05:30
{{ s__("mrWidget|Set by") }}
{{ author.name }}
{{ s__("mrWidget|to be merged automatically when the pipeline succeeds") }}
```
should be externalized as follows:
2020-05-24 23:13:21 +05:30
```javascript
2018-03-27 19:54:05 +05:30
{{ sprintf(s__("mrWidget|Set by %{author} to be merged automatically when the pipeline succeeds"), { author: author.name }) }}
```
2018-11-08 19:23:39 +05:30
#### 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-09-30 21:07:59 +05:30
- In Ruby/HAML, instead of:
2019-10-12 21:52:04 +05:30
```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 }
2021-04-29 21:17:54 +05:30
= html_escape(s_('ClusterIntegration|Learn more about %{zones_link_start}zones%{zones_link_end}')) % { zones_link_start: zones_link_start, zones_link_end: '</a>'.html_safe }
2019-10-12 21:52:04 +05:30
```
2019-09-30 21:07:59 +05:30
2020-03-13 15:44:24 +05:30
- 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-11-08 19:23:39 +05:30
2020-05-24 23:13:21 +05:30
```javascript
2019-10-12 21:52:04 +05:30
{{
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>'
2021-04-29 21:17:54 +05:30
}, false)
2019-10-12 21:52:04 +05:30
}}
```
2020-03-13 15:44:24 +05:30
Set the link starting and ending HTML fragments as placeholders like so:
2019-10-12 21:52:04 +05:30
2020-05-24 23:13:21 +05:30
```javascript
2019-10-12 21:52:04 +05:30
{{
sprintf(s__("ClusterIntegration|Learn more about %{linkStart}zones%{linkEnd}"), {
2020-03-13 15:44:24 +05:30
linkStart: '<a href="https://cloud.google.com/compute/docs/regions-zones/regions-zones" target="_blank" rel="noopener noreferrer">',
2019-10-12 21:52:04 +05:30
linkEnd: '</a>',
2021-04-29 21:17:54 +05:30
}, false)
2019-10-12 21:52:04 +05:30
}}
```
2018-11-08 19:23:39 +05:30
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-03-27 19:54:05 +05:30
When in doubt, try to follow the best practices described in this [Mozilla
2020-04-22 19:07:51 +05:30
Developer documentation](https://developer.mozilla.org/en-US/docs/Mozilla/Localization/Localization_content_best_practices#Splitting).
2018-03-27 19:54:05 +05:30
## Updating the PO files with the new content
2019-12-21 20:55:43 +05:30
Now that the new content is marked for translation, we need to update
`locale/gitlab.pot` files with the following command:
2018-03-27 19:54:05 +05:30
2020-03-13 15:44:24 +05:30
```shell
2018-11-18 11:00:15 +05:30
bin/rake gettext:regenerate
2018-03-27 19:54:05 +05:30
```
2021-02-22 17:27:13 +05:30
This command updates `locale/gitlab.pot` file with the newly externalized
2018-03-27 19:54:05 +05:30
strings and remove any strings that aren't used anymore. You should check this
2021-04-29 21:17:54 +05:30
file in. Once the changes are on the default branch, they are picked up by
2020-04-22 19:07:51 +05:30
[CrowdIn](https://translate.gitlab.com) and be presented for
2019-12-21 20:55:43 +05:30
translation.
2019-12-26 22:10:19 +05:30
We don't need to check in any changes to the `locale/[language]/gitlab.po` files.
2020-04-22 19:07:51 +05:30
They are updated automatically when [translations from CrowdIn are merged](merging_translations.md).
2018-03-27 19:54:05 +05:30
If there are merge conflicts in the `gitlab.pot` file, you can delete the file
2019-07-31 22:56:46 +05:30
and regenerate it using the same command.
2018-03-27 19:54:05 +05:30
### 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`.
2021-02-22 17:27:13 +05:30
The linter takes the following into account:
2018-03-27 19:54:05 +05:30
- Valid PO-file syntax
- Variable usage
- Only one unnamed (`%d`) variable, since the order of variables might change
in different languages
2020-05-24 23:13:21 +05:30
- All variables used in the message ID are used in the translation
2018-03-27 19:54:05 +05:30
- There should be no variables used in a translation that aren't in the
2020-05-24 23:13:21 +05:30
message ID
2018-03-27 19:54:05 +05:30
- Errors during translation.
2020-10-24 23:57:45 +05:30
- Presence of angle brackets (`<` or `>`)
2018-03-27 19:54:05 +05:30
The errors are grouped per file, and per message ID:
2020-04-22 19:07:51 +05:30
```plaintext
2018-03-27 19:54:05 +05:30
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.
2020-10-24 23:57:45 +05:30
Parsing result before error: '{:msgid=>["", "You are going to delete %{project_name_with_namespace}.\\n", "Deleted projects CANNOT be restored!\\n", "Are you ABSOLUTELY sure?"]}'
2018-03-27 19:54:05 +05:30
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
2020-05-24 23:13:21 +05:30
aren't in the message with ID `1 pipeline`.
2018-03-27 19:54:05 +05:30
2018-03-17 18:26:18 +05:30
## Adding a new language
2021-06-08 01:23:25 +05:30
A new language should only be added as an option in User Preferences once at least 10% of the
strings have been translated and approved. Even though a larger number of strings may have been
translated, only the approved translations display in the GitLab UI.
2021-02-22 17:27:13 +05:30
NOTE:
2020-10-24 23:57:45 +05:30
[Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/221012) in GitLab 13.3:
2021-02-22 17:27:13 +05:30
Languages with less than 2% of translations are not available in the UI.
2020-10-24 23:57:45 +05:30
2018-03-17 18:26:18 +05:30
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-10-12 21:52:04 +05:30
```ruby
...
AVAILABLE_LANGUAGES = {
...,
'fr' => 'Français'
}.freeze
...
```
2018-03-17 18:26:18 +05:30
1. Next, you need to add the language:
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
bin/rake gettext:add_language[fr]
```
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
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:
2018-03-17 18:26:18 +05:30
2020-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
bin/rake gettext:add_language[en_GB]
```
2018-03-17 18:26:18 +05:30
2019-10-12 21:52:04 +05:30
Please note that you need to specify the region part in capitals.
2018-03-17 18:26:18 +05:30
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-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
bin/rake gettext:compile
```
2018-03-17 18:26:18 +05:30
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-03-13 15:44:24 +05:30
```shell
2019-10-12 21:52:04 +05:30
git add locale/fr/ app/assets/javascripts/locale/fr/
2020-03-13 15:44:24 +05:30
git commit -m "Add French translations for Value Stream Analytics page"
2019-10-12 21:52:04 +05:30
```