Skip to content

Commit 46c32ea

Browse files
Merge pull request #828 from opentripplanner/yaml-sort
Perform yaml-sort on i18n pre-commit
2 parents 38ea597 + ec2973f commit 46c32ea

File tree

4 files changed

+375
-353
lines changed

4 files changed

+375
-353
lines changed

Diff for: README.md

+54-2
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,62 @@ env JS_CONFIG=my-custom-js.js CUSTOM_CSS=my-custom-css.css yarn build
3939
OTP-react-redux uses `react-intl` from the [`formatjs`](https://github.com/formatjs/formatjs) library for internationalization.
4040
Both `react-intl` and `formatjs` take advantage of native internationalization features provided by web browsers.
4141

42+
### `i18n` Folder
43+
4244
Language-specific content is located in YML files under the `i18n` folder
4345
(e.g. `en-US.yml` for American English, `fr.yml` for generic French, etc.).
4446

45-
Most textual content can also be customized using the `language` section of `config.yml`,
46-
whether for all languages at once or for each supported individual language.
47+
In each of these files:
48+
- Messages are organized in various categories and sub-categories.
49+
- A component or JS module can use messages from one or more categories.
50+
- In the code, messages are retrieved using an ID that is simply the path to the message.
51+
Use the dot '.' to separate categories and sub-categories in the path.
52+
For instance, for the message defined in YML below:
53+
```yaml
54+
common
55+
modes
56+
subway: Metro
57+
```
58+
then use the snippet below with the corresponding message id:
59+
```jsx
60+
<FormattedMessage id="common.modes.subway" /> // renders "Metro".
61+
```
62+
63+
In these YML files, it is important that message ids in the code be consistent with
64+
the categories in this file. Below are some general guidelines:
65+
- For starters, there are an `actions`, `common`, `components`, and `config`
66+
categories. Additional categories may be added as needed.
67+
- Each sub-category under `components` denotes a React component and
68+
should contain messages that are used only by that component (e.g. button captions).
69+
- In contrast, some strings are common to multiple components,
70+
so it makes sense to group them by theme (e.g. accessModes) under the `common` category.
71+
72+
Note: Do not put comments in the YML files! They will be removed by `yaml-sort`.
73+
Instead, comments for other developers should be placed in the corresponding js/jsx/ts/tsx file.
74+
Comments for translators should be entered into Weblate (see [Contributing Translations](#contributing-translations))
75+
76+
### Internationalizable content in the configuration file
77+
78+
Most textual content from the `i18n` folder can also be customized on a per-configuration basis
79+
using the `language` section of `config.yml`, whether for all languages at once,
80+
or for each supported individual language.
81+
82+
### Using internationalizable content in the code
83+
84+
Use message id **literals** (no variables or other dynamic content) with either
85+
```jsx
86+
<FormattedMessage id="..." />
87+
```
88+
or
89+
```js
90+
intl.formatMessage({ id: ... })
91+
```
92+
93+
The reason for passing **literals** to `FormattedMessage` and `intl.formatMessage` is that we have a checker script `yarn check:i18n` that is based on the `formatJS` CLI and that detects unused messages in the code and exports translation tables.
94+
Passing variables or dynamic content will cause the `formatJS` CLI and the checker to ignore the corresponding messages and
95+
incorrectly claim that a string is unused or missing from a translation file.
96+
97+
One exception to this rule concerns configuration settings where message ids can be constructed dynamically.
4798

4899
### Contributing translations
49100

@@ -62,6 +113,7 @@ OTP-react-redux now uses [Hosted Weblate](https://www.weblate.org) to manage tra
62113
Translations from the community are welcome and very much appreciated,
63114
please see instructions at https://hosted.weblate.org/projects/otp-react-redux/.
64115
Community input from Weblate will appear as pull requests with changes to files in the `i18n` folder for our review.
116+
(Contributions may be edited or rejected to remain in line with long-term project goals.)
65117

66118
If changes to a specific language file is needed but not enabled in Weblate, please open an issue or a pull request with the changes needed.
67119

0 commit comments

Comments
 (0)