Debian Codegraph @

docs/src/l10n.md

raw
1 # ๐ŸŒ Localization (L10n) in uutils coreutils
2
3 This guide explains how localization (L10n) is implemented in the **Rust-based coreutils project**, detailing the use of [Fluent](https://projectfluent.org/) files, runtime behavior, and developer integration.
4
5 ## ๐Ÿ—๏ธ Architecture Overview
6
7 **English (US) locale files (`en-US.ftl`) are embedded directly in the binary**, ensuring that English always works regardless of how the software is installed. Other language locale files are loaded from the filesystem at runtime.
8
9 ### Source Repository Structure
10
11 - **Main repository**: Contains English (`en-US.ftl`) locale files embedded in binaries
12 - **Translation repository**: [uutils/coreutils-l10n](https://github.com/uutils/coreutils-l10n) contains all other language translations
13 - **Online Translation**: [weblate/rust-coreutils](https://hosted.weblate.org/projects/rust-coreutils/) to translate the strings.
14
15
16 ---
17
18 ## ๐Ÿ“ Fluent File Layout
19
20 Each utility has its own set of translation files under:
21
22 ```
23 src/uu/<utility>/locales/<locale>.ftl
24 ```
25
26 Examples:
27
28 ```
29 src/uu/ls/locales/en-US.ftl # Embedded in binary
30 src/uu/ls/locales/fr-FR.ftl # Loaded from filesystem
31 ```
32
33 These files follow Fluent syntax and contain localized message patterns.
34
35 The French translation is the only locale with English to be part of the tree. The goal is to be able to run tests with
36 a different locale to verify that they work.
37
38 ---
39
40 ## โš™๏ธ Initialization
41
42 Localization must be explicitly initialized at runtime using:
43
44 ```
45 setup_localization(path)
46 ```
47
48 This is typically done:
49 - In `src/bin/coreutils.rs` for **multi-call binaries**
50 - In `src/uucore/src/lib.rs` for **single-call utilities**
51
52 The string parameter determines the lookup path for Fluent files. **English always works** because it's embedded, but other languages need their `.ftl` files to be available at runtime.
53
54 ---
55
56 ## ๐ŸŒ Locale Detection
57
58 Locale selection is automatic and performed via:
59
60 ```
61 fn detect_system_locale() -> Result<LanguageIdentifier, LocalizationError>
62 ```
63
64 It reads the `LANG` environment variable (e.g., `fr-FR.UTF-8`), strips encoding, and parses the identifier.
65
66 If parsing fails or `LANG` is not set, it falls back to:
67
68 ```
69 const DEFAULT_LOCALE: &str = "en-US";
70 ```
71
72 You can override the locale at runtime by running:
73
74 ```
75 LANG=ja-JP ./target/debug/ls
76 ```
77
78 ---
79
80 ## ๐Ÿ“ฅ Retrieving Messages
81
82 We have a single macro to handle translations.
83 It can be used in two ways:
84
85 ### `translate!(id: &str) -> String`
86
87 Returns the message from the current locale bundle.
88
89 ```
90 let msg = translate!("id-greeting");
91 ```
92
93 If not found, falls back to `en-US`. If still missing, returns the ID itself.
94
95 ---
96
97 ### `translate!(id: &str, args: key-value pairs) -> String`
98
99 Supports variable interpolation and pluralization.
100
101 ```
102 let msg = translate!(
103 "error-io",
104 "error" => std::io::Error::last_os_error()
105 );
106 ```
107
108 Fluent message example:
109
110 ```
111 error-io = I/O error occurred: { $error }
112 ```
113
114 Variables must match the Fluent placeholder keys (`$error`, `$name`, `$count`, etc.).
115
116 ---
117
118 ## ๐Ÿ“ฆ Fluent Syntax Example
119
120 ```
121 id-greeting = Hello, world!
122 welcome = Welcome, { $name }!
123 count-files = You have { $count ->
124 [one] { $count } file
125 *[other] { $count } files
126 }
127 ```
128
129 Use plural rules and inline variables to adapt messages dynamically.
130
131 ---
132
133 ## ๐Ÿงช Testing Localization
134
135 Run all localization-related unit tests with:
136
137 ```
138 cargo test --lib -p uucore
139 ```
140
141 Tests include:
142 - Loading bundles
143 - Plural logic
144 - Locale fallback
145 - Fluent parse errors
146 - Thread-local behavior
147 - ...
148
149 ---
150
151 ## ๐Ÿงต Thread-local Storage
152
153 Localization is stored per thread using a `OnceLock`.
154 Each thread must call `setup_localization()` individually.
155 Initialization is **one-time-only** per thread โ€” re-initialization results in an error.
156
157 ---
158
159 ## ๐Ÿงช Development vs Release Mode
160
161 During development (`cfg(debug_assertions)`), paths are resolved relative to the crate source:
162
163 ```
164 $CARGO_MANIFEST_DIR/../uu/<utility>/locales/
165 ```
166
167 In release mode, **paths are resolved relative to the executable**:
168
169 ```
170 <executable_dir>/locales/<utility>/
171 <prefix>/share/locales/<utility>/
172 ~/.local/share/coreutils/locales/<utility>/
173 ~/.cargo/share/coreutils/locales/<utility>/
174 /usr/share/coreutils/locales/<utility>/
175 ```
176
177 If external locale files aren't found, the system falls back to embedded English locales.
178
179 ---
180
181 ## ๐Ÿ”ค Unicode Isolation Handling
182
183 By default, the Fluent system wraps variables with Unicode directional isolate characters (`U+2068`, `U+2069`) to protect against visual reordering issues in bidirectional text (e.g., mixing Arabic and English).
184
185 In this implementation, isolation is **disabled** via:
186
187 ```
188 bundle.set_use_isolating(false);
189 ```
190
191 This improves readability in CLI environments by preventing extraneous characters around interpolated values:
192
193 Correct (as rendered):
194
195 ```
196 "Welcome, Alice!"
197 ```
198
199 Fluent default (disabled here):
200
201 ```
202 "\u{2068}Alice\u{2069}"
203 ```
204
205 ---
206
207 ## ๐Ÿ”ง Embedded English Locales
208
209 English locale files are always embedded directly in the binary during the build process. This ensures that:
210
211 - **English always works** regardless of installation method (e.g., `cargo install`)
212 - **No runtime dependency** on external `.ftl` files for English
213 - **Fallback behavior** when other language files are missing
214
215 The embedded English locales are generated at build time and included in the binary, providing a reliable fallback while still supporting full localization for other languages when their `.ftl` files are available.