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
//! Integration with yew 0.23.
//!
//! phrasebook ships no components and no hooks: the context type is
//! [`Phrases`] itself. An app provides it with yew's own
//! `ContextProvider<Phrases>`, and components read it with yew's own
//! `use_context::<Phrases>()`, or `ctx.link().context::<Phrases>(..)` in a
//! struct component. This module adds what that needs:
//!
//! * [`ImplicitClone`] for [`Phrases`], [`Lang`] and [`Language`], so they
//! pass as props and context like yew's own cheap values;
//! * [`IntoPropValue`] from [`Lang`] into `AttrValue`, for an element's
//! `lang` attribute (the `Rc<str>` from [`Phrases::text`] already converts
//! into `AttrValue` and into children);
//! * [`or_default`] and [`or_catalog`], plain functions over the
//! `Option<Phrases>` that `use_context` returns, for a component rendered
//! where nothing provides a handle.
//!
//! # Providing the handle
//!
//! The app's root owns the handle as app state. On a language change it
//! calls [`Phrases::with_lang`] (and `Phrases::store` with the `browser`
//! feature, to remember the choice) and provides the new handle, which
//! re-renders every component reading it. A component deep in the tree
//! that changes the language, such as a picker, gets a `Callback<Lang>`
//! from the app like for any other app state.
//!
//! ```
//! # extern crate yew023 as yew;
//! use phrasebook::{Catalog, Lang, Phrasebook, Phrases, Source};
//! use phrasebook::yew023::or_default;
//! use yew::prelude::*;
//!
//! const SV_SE: Lang = Lang::new("sv-SE");
//!
//! static APP: Catalog = Catalog::new("app", &[
//! Source::new(Lang::EN_US, "title = Mafia"),
//! Source::new(SV_SE, "title = Maffia"),
//! ]);
//!
//! fn phrasebook() -> Phrasebook {
//! Phrasebook::builder()
//! .language(Lang::EN_US, "English")
//! .language(SV_SE, "Svenska")
//! .catalog(&APP)
//! .build()
//! }
//!
//! enum Msg {
//! SetLang(Lang),
//! }
//!
//! struct App {
//! phrases: Phrases,
//! }
//!
//! impl Component for App {
//! type Message = Msg;
//! type Properties = ();
//!
//! fn create(_: &Context<Self>) -> Self {
//! // With the `browser` feature, start from the remembered or
//! // preferred language: `browser::initial(&book, browser::DEFAULT_KEY)`.
//! let phrases = phrasebook().phrases(Lang::EN_US);
//! Self { phrases }
//! }
//!
//! fn update(&mut self, _: &Context<Self>, msg: Msg) -> bool {
//! match msg {
//! Msg::SetLang(lang) => {
//! self.phrases = self.phrases.with_lang(lang);
//! // With the `browser` feature: `self.phrases.store();`
//! true
//! }
//! }
//! }
//!
//! fn view(&self, ctx: &Context<Self>) -> Html {
//! let on_lang = ctx.link().callback(Msg::SetLang);
//!
//! html! {
//! <ContextProvider<Phrases> context={self.phrases.clone()}>
//! <Title />
//! <Picker {on_lang} />
//! </ContextProvider<Phrases>>
//! }
//! }
//! }
//!
//! #[function_component]
//! fn Title() -> Html {
//! let phrases = or_default(use_context::<Phrases>());
//! html! { <h1 lang={phrases.lang()}>{phrases.text("title")}</h1> }
//! }
//!
//! #[derive(Properties, PartialEq)]
//! struct PickerProps {
//! on_lang: Callback<Lang>,
//! }
//!
//! #[function_component]
//! fn Picker(props: &PickerProps) -> Html {
//! let phrases = or_default(use_context::<Phrases>());
//!
//! phrases
//! .languages()
//! .iter()
//! .map(|language| {
//! let lang = language.lang;
//! let onclick = props.on_lang.reform(move |_| lang);
//! html! { <button {onclick}>{language.name}</button> }
//! })
//! .collect()
//! }
//! ```
//!
//! # In a library
//!
//! A library ships its own [`Catalog`] under its prefix, which an app adds
//! to its phrasebook after its own catalog (so the app can override or
//! translate the library's messages). Each of the library's components
//! reads the handle through [`or_catalog`]: the provided handle with the
//! library's catalog as its backstop, so its messages render even when the
//! app did not add the catalog, or not in this language; or, with nothing
//! provided, the library's catalog alone in its default language.
//!
//! ```
//! # extern crate yew023 as yew;
//! use phrasebook::{Catalog, Lang, Phrases, Source};
//! use phrasebook::yew023::or_catalog;
//! use yew::prelude::*;
//!
//! pub static L10N: Catalog = Catalog::new("watchword", &[
//! Source::new(Lang::EN_US, "watchword-sign-in = Sign in"),
//! ])
//! .with_prefix("watchword-");
//!
//! #[function_component]
//! pub fn SignIn() -> Html {
//! let phrases = or_catalog(use_context::<Phrases>(), &L10N);
//! html! { <button>{phrases.text("watchword-sign-in")}</button> }
//! }
//! ```
//!
//! A struct component subscribes in `create` and keeps the
//! `ContextHandle`, which stays subscribed while it lives; later handles
//! arrive as a message.
//!
//! ```
//! # extern crate yew023 as yew;
//! use phrasebook::{Catalog, Lang, Phrases, Source};
//! use phrasebook::yew023::or_catalog;
//! use yew::context::ContextHandle;
//! use yew::prelude::*;
//!
//! pub static L10N: Catalog = Catalog::new("watchword", &[
//! Source::new(Lang::EN_US, "watchword-sign-out = Sign out"),
//! ])
//! .with_prefix("watchword-");
//!
//! pub enum Msg {
//! Phrases(Phrases),
//! }
//!
//! pub struct SignOut {
//! phrases: Phrases,
//! _handle: Option<ContextHandle<Phrases>>,
//! }
//!
//! impl Component for SignOut {
//! type Message = Msg;
//! type Properties = ();
//!
//! fn create(ctx: &Context<Self>) -> Self {
//! let context = ctx.link().context::<Phrases>(ctx.link().callback(Msg::Phrases));
//! let phrases = or_catalog(context.as_ref().map(|(p, _)| p.clone()), &L10N);
//! let _handle = context.map(|(_, handle)| handle);
//! Self { phrases, _handle }
//! }
//!
//! fn update(&mut self, _: &Context<Self>, msg: Msg) -> bool {
//! match msg {
//! Msg::Phrases(phrases) => {
//! self.phrases = or_catalog(Some(phrases), &L10N);
//! true
//! }
//! }
//! }
//!
//! fn view(&self, _: &Context<Self>) -> Html {
//! html! { <button>{self.phrases.text("watchword-sign-out")}</button> }
//! }
//! }
//! ```
//!
//! [`ImplicitClone`]: yew023::html::ImplicitClone
//! [`IntoPropValue`]: yew023::html::IntoPropValue
use Cell;
use Rc;
use ;
use AttrValue;
use crate::;
/// Cheap to clone: an `Rc` and a few words.
/// `Copy`.
/// `Copy`.
/// The language's tag, such as `sv-SE`, for an element's `lang` attribute.
/// The language's tag, such as `sv-SE`, for an element's `lang` attribute.
thread_local!
/// The provided handle, or [`Phrases::empty`], which renders ids, for a
/// component rendered where nothing provides one (a test, a portal, a
/// component used outside the app's root). The first fallback on each
/// thread is logged with `tracing`.
///
/// ```
/// use phrasebook::Phrases;
/// use phrasebook::yew023::or_default;
///
/// // What `use_context::<Phrases>()` returns outside a provider.
/// let phrases = or_default(None);
/// assert_eq!(&*phrases.text("title"), "title");
/// assert!(phrases == Phrases::empty());
/// ```
/// For a library's components: the provided handle with `catalog` as its
/// backstop ([`Phrases::with_backstop`]), so a message the app's bundles
/// lack (the app did not add the catalog, or not in this language) comes
/// from the catalog; or, with nothing provided, [`Phrases::from_catalog`],
/// the catalog alone in its default language.
///
/// ```
/// use phrasebook::{Catalog, Lang, Phrasebook, Source};
/// use phrasebook::yew023::or_catalog;
///
/// static LIB: Catalog = Catalog::new("lib", &[
/// Source::new(Lang::EN_US, "lib-sign-in = Sign in"),
/// ])
/// .with_prefix("lib-");
///
/// // Nothing provided: the library's own English.
/// let phrases = or_catalog(None, &LIB);
/// assert_eq!(&*phrases.text("lib-sign-in"), "Sign in");
///
/// // Provided by an app that did not add the library's catalog.
/// let book = Phrasebook::builder().language(Lang::EN_US, "English").build();
/// let phrases = or_catalog(Some(book.phrases(Lang::EN_US)), &LIB);
/// assert_eq!(&*phrases.text("lib-sign-in"), "Sign in");
/// ```