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
use crate::;
use ;
use LangID;
use OnceCell;
// mods:
/// The `LangResource` struct represents a language resource comprising a `LangID`, a `FluentLoader`, and a `Chain` for alternative language resources. The fallback chain can be lazily initialised and is immutable once set, ensuring effective and reliable handling of language localisation in software applications.
///
/// ## Overview
///
/// | Member Variable | Type | Description |
/// | --------------- | ------------------------- | ----------------------------------------------------------------- |
/// | `id` | `LangID` | Represents the ID of the language resource. |
/// | `loader` | `FluentLoader<'a>` | Represents the loader for Fluent resources. |
/// | `chain` | `OnceCell<Chain>` | Represents the fallback chain for alternative language resources. |
///
/// ### ID
///
/// ID refers to language identifier, such as `en`, `en-001`, and `en-Latn-001` are all valid lang-ids.
///
/// `en` refers to en-Latn-US, not en-GB.
///
/// `001` refers to the world.
///
/// > The related rules come from Unicode CLDR (Unicode Common Locale Data Repository) version 42.0.0.
///
/// If you use `from()` to create a struct, you don't need to worry about the ID field because it will automatically obtain your system's language.
///
/// > For a detailed list of lang-ids, you can refer to [lang-id](https://github.com/2moe/lang-id).
///
/// ### loader
///
/// `FluentLoader` is an enum type that currently supports `StaticLoader` and `ArcLoader`, both of which come from fluent-templates.
///
/// Perhaps more loaders can be added in the future.
///
/// | Variable | Type | Description |
/// | ---------- | ----------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
/// | `Static` | `&'static StaticLoader` | Represents a static Fluent loader that loads Fluent resources at compile time. |
/// | `Arc` | `&'a ArcLoader` | Represents a reference-counted smart pointer-based Fluent loader that allows loading resources at runtime. Both `'static ArcLoader` and `'a ArcLoader` can be used. |
/// | `ArcOwned` | `Arc<ArcLoader>` | Represents a reference-counted smart pointer-based Fluent loader with ownership. The main difference from `&'a ArcLoader` is that it has ownership. |
///
/// The `FluentLoader` enum represents two types of Fluent loaders: the `StaticLoader`, which loads Fluent resources (i18n/l10n resources) at compile time, and the `ArcLoader`, which allows loading resources at runtime using both `'static ArcLoader` and `'a ArcLoader`. The `ArcOwned` variant is another type of `ArcLoader` that has ownership.
///
/// This enum is used to specify the type of Fluent loader when initializing a `LangResource` struct.
///
/// Note: `static_loader` comes from fluent-templates, you can use the same syntax as it.
///
/// ```no_run
/// use glossa::fluent::loader::static_loader;
///
/// static_loader! {
/// static LOADER = {
/// locales: "locales",
/// fallback_language: "en",
/// customise: |bundle| bundle.set_use_isolating(false),
/// };
/// }
/// ```
///
/// ### About chain
///
/// `chain` can be lazily initialised. By default, it won't be initialised automatically when not needed and will only be initialised once when needed.
///
/// For example, if your language ID is "en" and all the localisation resources match, then it won't automatically initialise the fallback chain.
///
/// On the other hand, if your language ID is "zh-Hant-MO" and there are no matches, then it will automatically generate a fallback chain based on the resource list.
///
/// - For languages with the same name, scripts have higher priority than regions.
/// - If the current resource list is `["zh", "zh-Hans", "zh-Hant-HK", "zh-Hans-MO"]`, then `zh-Hant-HK` has higher priority than `zh-Hans-MO`.
///
/// If you need to customise the fallback chain, use `set_chain_once()` before calling `get()` or `get_with_kv()`.
///
/// Once the fallback chain is initialised, you cannot modify its value, but you can replace it with a new `OnceCell` instance.
///
/// ```no_run
/// let mut res = LangResource::from(&LOADER);
/// let chain = res.get_chain_mut();
/// *chain = OnceCell::new();
/// ```
/// Implements the Debug trait for the LangResource struct, allowing it to be printed with dbg!() and other debugging tools.