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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
//! Compile-time internationalization for embedded and `no_std` Rust.
//!
//! Translations are read from locale files at build time by procedural macros
//! and embedded in the binary as `&'static str` literals: no runtime parsing, no
//! allocator, no file system, and a missing translation is a compile error at
//! the call site.
//!
//! # Quick start
//!
//! Put locale files in a `locales/` directory next to `Cargo.toml`:
//!
//! ```json
//! {
//! "greeting": "Bonjour",
//! "files": { "one": "{count} fichier", "other": "{count} fichiers" }
//! }
//! ```
//!
//! Pick the locale at build time with `EMBEDDED_I18N_LOCALE=fr cargo build`, and
//! translate with the macros:
//!
//! ```
//! use core::fmt::Write;
//! use embedded_i18n::{translate, translate_plural, translate_write};
//!
//! embedded_i18n::track_locales!();
//!
//! const GREETING: &str = translate!("greeting");
//! assert_eq!(GREETING, "Hello");
//!
//! assert_eq!(translate_plural!("files", 2), "{count} files");
//!
//! let mut output = String::new();
//! translate_write!(&mut output, "with_arg", 42)?;
//! assert_eq!(output, "Value: 42");
//! # Ok::<(), core::fmt::Error>(())
//! ```
//!
//! # Macros
//!
//! | Macro | Result |
//! | --- | --- |
//! | [`translate!`] | `&'static str` or `&'static CStr` literal |
//! | [`translate_args!`] | [`core::fmt::Arguments`] with the translation as format string |
//! | [`translate_write!`] | Writes the formatted translation into a [`core::fmt::Write`] |
//! | [`translate_plural!`] / [`translate_plural_write!`] | Plural form selected from a count |
//! | [`translate_select!`] / [`translate_select_write!`] | Variant selected from an `AsRef<str>` value |
//! | [`track_locales!`] | Rebuilds the crate when a locale file changes |
//!
//! Every macro accepts a message context before the key: `translate!(context =
//! "menu", "Open")`.
//!
//! # Configuration
//!
//! The macros are configured with environment variables read at compile time,
//! which can be set in `.cargo/config.toml`:
//!
//! | Variable | Default | Description |
//! | --- | --- | --- |
//! | `EMBEDDED_I18N_LOCALE` | `en` | Locale to embed |
//! | `EMBEDDED_I18N_FALLBACK` | `en` | Comma separated fallback locales |
//! | `EMBEDDED_I18N_PATH` | `locales` | Locale files directory, relative to the calling crate |
//! | `EMBEDDED_I18N_LOCALES` | unset | Locales to embed for [runtime selection](set_locale) |
//! | `EMBEDDED_I18N_STRICT` | unset | Forbids falling back for messages missing from the primary locale |
//! | `EMBEDDED_I18N_BIDI_ISOLATION` | unset | Isolates placeholders of right-to-left locales |
//!
//! Locales are normalized like BCP 47 tags and resolved from the most specific
//! to the most generic (`fr-CA`, `fr`, then the fallbacks). The special locale
//! `pseudo` generates a pseudo-localization of the fallback locales.
//!
//! # Locale file formats
//!
//! Each format is enabled by a Cargo feature; all parsing happens in the
//! procedural macro, so formats add nothing to the binary.
//!
//! | Feature | Files |
//! | --- | --- |
//! | `backend-json` (default) | `.json`, nested objects flattened to dotted keys |
//! | `backend-yaml` | `.yaml`, `.yml` |
//! | `backend-toml` | `.toml` |
//! | `backend-po` | GNU gettext `.po`, with `msgctxt`, plural forms and `Plural-Forms` |
//! | `backend-mo` | Compiled GNU gettext `.mo` |
//! | `backend-fluent` | Fluent `.ftl` simple messages and whole-message selects |
//!
//! # Runtime helpers
//!
//! - [`DateTimeFormatter`] and [`write_unix_timestamp`]: localized `strftime`-style
//! formatting without allocation.
//! - [`is_right_to_left`] and [`is_locale_right_to_left`]: text direction.
//! - [`get_locale_ranges`] and [`write_ranges`]: Unicode ranges to subset fonts.
//! - [`set_locale`], [`locale`] and [`LOCALES`]: runtime locale selection when
//! several locales are embedded.
//!
//! # Features
//!
//! - `alloc`: `String` and `Vec` returning variants of the helpers.
//! - `std`: enables `alloc` and the build environment getters.
//!
//! # Guide
//!
//! The [`guide`] has one page per topic: [locale files](guide::locale_files),
//! [translating](guide::translating), [message context](guide::context),
//! [plural forms](guide::plurals), [select variants](guide::select),
//! [formatting](guide::formatting), [rebuild tracking](guide::rebuild_tracking),
//! [configuration](guide::configuration),
//! [runtime locale selection](guide::runtime_locales),
//! [translation coverage](guide::coverage),
//! [right-to-left languages](guide::right_to_left),
//! [pseudo-localization](guide::pseudo_localization),
//! [date and time](guide::date_time), [Unicode ranges](guide::unicode_ranges),
//! [`no_std` support](guide::no_std) and a
//! [comparison with other crates](guide::comparison).
extern crate self as embedded_i18n;
extern crate alloc;
extern crate std;
pub use *;
pub use *;
pub use *;
pub use *;
pub use *;
/// Makes the calling crate depend on every locale file read by the macros.
///
/// Cargo does not know that the macros read locale files, so editing them would
/// not rebuild the crate. Invoke this macro once, at module level, to rebuild
/// whenever one of the loaded files changes. It expands to `include_bytes!` of
/// each file in an unused constant and adds nothing to the binary.
///
/// ```
/// embedded_i18n::track_locales!();
/// ```
pub use track_locales;
/// Replaces a message key with its translation, at compile time.
///
/// The key is a string literal, or a C string literal to get a `&'static CStr`
/// for FFI. An optional `context` disambiguates identical source strings.
///
/// ```
/// use core::ffi::CStr;
/// use embedded_i18n::translate;
///
/// const GREETING: &str = translate!("greeting");
/// assert_eq!(GREETING, "Hello");
///
/// let greeting: &'static CStr = translate!(c"greeting");
/// assert_eq!(greeting.to_bytes(), b"Hello");
///
/// assert_eq!(translate!(context = "menu", "Open"), "Open (menu)");
/// ```
///
/// In single-locale builds the expansion is a literal, usable in `const` items
/// and as the format string of `format!` or `write!`. A key missing from every
/// locale of the chain is a compile error:
///
/// ```compile_fail
/// let _ = embedded_i18n::translate!("no such key");
/// ```
pub use translate;
/// Builds [`core::fmt::Arguments`] from a translated format string.
///
/// The translation is the format string, so placeholders are checked at compile
/// time and can capture variables in scope. Usable wherever `format_args!` is,
/// without allocation.
///
/// ```
/// use embedded_i18n::translate_args;
///
/// let name = "Ada";
/// let text = format!("{}", translate_args!("with_named_args", count = 3));
/// assert_eq!(text, "Hello Ada, you have 3 messages");
/// ```
pub use translate_args;
/// Writes a translated, formatted message into a [`core::fmt::Write`].
///
/// Expands to `write!(writer, translation, arguments...)` and returns
/// [`core::fmt::Result`].
///
/// ```
/// use core::fmt::Write;
/// use embedded_i18n::translate_write;
///
/// let mut output = String::new();
/// translate_write!(&mut output, "with_arg", 42)?;
/// assert_eq!(output, "Value: 42");
/// # Ok::<(), core::fmt::Error>(())
/// ```
pub use translate_write;
/// Selects the plural form of a message matching a count.
///
/// Forms are CLDR categories (`files.one`, `files.other`...) in JSON, YAML, TOML
/// and Fluent files, or `msgstr[N]` with the `Plural-Forms` rule in gettext
/// files. The rule is compiled into an `if` / `match` on the count; the result
/// is a `&'static str`, or a `&'static CStr` with a C string key.
///
/// ```
/// use embedded_i18n::translate_plural;
///
/// assert_eq!(translate_plural!("files", 1), "{count} file");
/// assert_eq!(translate_plural!("files", 5u32), "{count} files");
/// ```
pub use translate_plural;
/// Writes the plural form of a message matching a count into a
/// [`core::fmt::Write`].
///
/// The count is bound to `count`, so forms can use the `{count}` placeholder.
/// Extra format arguments can follow the count.
///
/// ```
/// use core::fmt::Write;
/// use embedded_i18n::translate_plural_write;
///
/// let mut output = String::new();
/// translate_plural_write!(&mut output, "files", 3)?;
/// assert_eq!(output, "3 files");
/// # Ok::<(), core::fmt::Error>(())
/// ```
pub use translate_plural_write;
/// Selects a variant of a message from a value implementing `AsRef<str>`.
///
/// Variants are nested keys (`replied.male`, `replied.other`); the `other`
/// variant is required and used for every value without its own variant.
///
/// ```
/// use embedded_i18n::translate_select;
///
/// assert_eq!(translate_select!("replied", "female"), "She replied");
/// assert_eq!(translate_select!("replied", "unknown"), "They replied");
/// ```
pub use translate_select;
/// Writes the variant of a message selected from a value implementing
/// `AsRef<str>` into a [`core::fmt::Write`].
///
/// ```
/// use core::fmt::Write;
/// use embedded_i18n::translate_select_write;
///
/// let mut output = String::new();
/// translate_select_write!(&mut output, "replied", "male")?;
/// assert_eq!(output, "He replied");
/// # Ok::<(), core::fmt::Error>(())
/// ```
pub use translate_select_write;
const DEFAULT_LOCALE: &str = "en";
const DEFAULT_FALLBACK_LOCALE: &str = "en";
/// Returns the locale selected at build time with `EMBEDDED_I18N_LOCALE`, `en`
/// by default.
///
/// ```
/// assert_eq!(embedded_i18n::get_locale(), "en");
/// ```
pub const
/// Returns the fallback locales selected at build time with
/// `EMBEDDED_I18N_FALLBACK`, as written (comma separated), `en` by default.
pub const
/// Reads `EMBEDDED_I18N_LOCALE` from the environment of the running process, for
/// build scripts. Available with the `std` feature.
/// Reads `EMBEDDED_I18N_FALLBACK` from the environment of the running process,
/// for build scripts. Available with the `std` feature.