Skip to content

Commit dc6dd6a

Browse files
authored
Add instructions for adding and editing translations using i18n (LAION-AI#1120)
update markdown Remove ignore_keys
1 parent c878d12 commit dc6dd6a

File tree

2 files changed

+87
-0
lines changed

2 files changed

+87
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
from glob import glob
2+
from json import load
3+
from os import path
4+
5+
ALL_PATH = "../../website/public/locales/**/*.json"
6+
DIR = path.dirname(__file__)
7+
EN_PATH = "../../website/public/locales/en/*.json"
8+
9+
10+
def get_not_translated(en_json, translation_json, parent_key=None):
11+
not_translated = []
12+
for key in en_json.keys():
13+
if key in translation_json and translation_json[key] == en_json[key]:
14+
not_translated.append(("{0}.{1}".format(parent_key, key) if parent_key else key))
15+
elif isinstance(en_json[key], dict):
16+
not_translated.extend(get_not_translated(en_json[key], translation_json[key], key))
17+
return not_translated
18+
19+
20+
def get_missing(en_json, translation_json):
21+
return [key for key in en_json.keys() if key not in translation_json]
22+
23+
24+
def print_result(missing, not_translated, file):
25+
if len(missing):
26+
print("[{0}] - missing: {1} {2}".format(path.basename(path.dirname(file)), path.basename(file), missing))
27+
if len(not_translated):
28+
print(
29+
"[{0}] - potentially untranslated: {1} {2}".format(
30+
path.basename(path.dirname(file)), path.basename(file), not_translated
31+
)
32+
)
33+
34+
35+
def audit(file, en_file):
36+
en_json = load(open(en_file))
37+
translation_json = load(open(file))
38+
print_result(get_missing(en_json, translation_json), get_not_translated(en_json, translation_json), file)
39+
40+
41+
def main():
42+
for en_file in glob(path.join(DIR, EN_PATH)):
43+
for file in glob(path.join(DIR, ALL_PATH)):
44+
if path.basename(en_file) == path.basename(file) and file != en_file:
45+
audit(file, en_file)
46+
47+
48+
if __name__ == "__main__":
49+
main()

website/docs/add_edit_translations.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
## Adding new locales to i18n
2+
3+
This guide will help you add a new locale to the `i18n` setup.
4+
5+
### Prerequisites
6+
7+
- An up-to-date branch with the `main` branch.
8+
- Familiarity with `i18n`, `react-i18next`, and `next-i18next` libraries is beneficial.
9+
10+
### Adding a new language
11+
12+
1. Determine the language and country codes using `ISO 639-1`. For example, `en` for English.
13+
1. Create a new directory within the `public/locales` directory using the language and country codes as the name, for
14+
example `en`.
15+
1. Copy all the files from the `en` directory into the newly created directory.
16+
1. Edit the copied the text in the copied files with the desired language.
17+
1. Add the new language to the list in `next-i18next.config.js` if it does not already exist.
18+
1. Follow the instructions in [Website README](<[README.md](../../../website/README.md)>) to run and test the new
19+
language by changing the active locale in the application and verifying that all translated keys are properly
20+
displayed.
21+
1. Commit your changes and open a pull request against the `main` branch for review.
22+
23+
### Editing existing translation files
24+
25+
When editing existing translations, follow these rules:
26+
27+
1. English translations are required, and other locales fall back to them.
28+
1. Keep translation keys in alphabetical order.
29+
1. Add all translations for higher-level components (e.g. `Layout.ts`) in `common.json` to prevent hydration issues.
30+
1. Add reused translation keys in `common.json`.
31+
1. Split translation files into separate files by feature or route.
32+
33+
### Finding missing translations
34+
35+
A script can be used to find missing and potentially untranslated locale files. Run the script from the root dir using
36+
`python scripts/frontend-development/find-missing-locales.py`.
37+
38+
If you have any questions or need further assistance, please reach out.

0 commit comments

Comments
 (0)