|
248 | 248 | //! ``` |
249 | 249 | //! # #[cfg(all(feature = "fluent-system", feature = "desktop-requester"))] |
250 | 250 | //! # { |
251 | | -//! use std::sync::Arc; |
| 251 | +//! use std::sync::{Arc, OnceLock}; |
252 | 252 | //! use i18n_embed::{ |
253 | 253 | //! DesktopLanguageRequester, LanguageRequester, |
254 | 254 | //! DefaultLocalizer, Localizer, fluent::FluentLanguageLoader |
255 | 255 | //! }; |
256 | | -//! use rust_embed::RustEmbed; use lazy_static::lazy_static; |
| 256 | +//! use rust_embed::RustEmbed; |
257 | 257 | //! use unic_langid::LanguageIdentifier; |
258 | 258 | //! |
259 | 259 | //! #[derive(RustEmbed)] |
260 | 260 | //! #[folder = "i18n/ftl"] // path to localization resources |
261 | 261 | //! struct Localizations; |
262 | 262 | //! |
263 | | -//! lazy_static! { |
264 | | -//! static ref LANGUAGE_LOADER: FluentLanguageLoader = { |
| 263 | +//! pub fn language_loader() -> &'static FluentLanguageLoader { |
| 264 | +//! static LANGUAGE_LOADER: OnceLock<FluentLanguageLoader> = OnceLock::new(); |
| 265 | +//! |
| 266 | +//! LANGUAGE_LOADER.get_or_init(|| { |
265 | 267 | //! // Usually you could use the fluent_language_loader!() macro |
266 | 268 | //! // to pull values from i18n.toml configuration and current |
267 | 269 | //! // module here at compile time, but instantiating the loader |
268 | 270 | //! // manually here instead so the example compiles. |
269 | 271 | //! let fallback: LanguageIdentifier = "en-US".parse().unwrap(); |
270 | 272 | //! FluentLanguageLoader::new("test", fallback) |
271 | | -//! }; |
| 273 | +//! }) |
272 | 274 | //! } |
273 | 275 | //! |
274 | 276 | //! # #[allow(dead_code)] |
275 | 277 | //! fn main() { |
276 | | -//! let localizer = DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations); |
| 278 | +//! let localizer = DefaultLocalizer::new(&*language_loader(), &Localizations); |
277 | 279 | //! |
278 | 280 | //! let localizer_arc: Arc<dyn Localizer> = Arc::new(localizer); |
279 | 281 | //! |
|
297 | 299 | //! [DefaultLocalizer](DefaultLocalizer), but you can also implement |
298 | 300 | //! the [Localizer](Localizer) trait yourself for a custom solution. |
299 | 301 | //! It also makes use of |
300 | | -//! [lazy_static](https://crates.io/crates/lazy_static) to allow the |
| 302 | +//! [OnceLock](https://doc.rust-lang.org/beta/std/sync/struct.OnceLock.html) to allow the |
301 | 303 | //! [LanguageLoader](LanguageLoader) implementation to be stored |
302 | 304 | //! statically, because its constructor is not `const`. |
303 | 305 | //! |
|
309 | 311 | //! ``` |
310 | 312 | //! # #[cfg(feature = "fluent-system")] |
311 | 313 | //! # { |
312 | | -//! use std::sync::Arc; |
| 314 | +//! use std::sync::{Arc, OnceLock}; |
313 | 315 | //! use i18n_embed::{ |
314 | 316 | //! DefaultLocalizer, Localizer, LanguageLoader, |
315 | 317 | //! fluent::{ |
316 | 318 | //! fluent_language_loader, FluentLanguageLoader |
317 | 319 | //! }}; |
318 | | -//! use rust_embed::RustEmbed; use lazy_static::lazy_static; |
| 320 | +//! use rust_embed::RustEmbed; |
319 | 321 | //! |
320 | 322 | //! #[derive(RustEmbed)] |
321 | 323 | //! #[folder = "i18n/mo"] // path to the compiled localization resources |
322 | 324 | //! struct Localizations; |
323 | 325 | //! |
324 | | -//! lazy_static! { |
325 | | -//! static ref LANGUAGE_LOADER: FluentLanguageLoader = { |
326 | | -//! let loader = fluent_language_loader!(); |
| 326 | +//! fn language_loader() -> &'static FluentLanguageLoader { |
| 327 | +//! static LANGUAGE_LOADER: OnceLock<FluentLanguageLoader> = OnceLock::new(); |
| 328 | +//! |
| 329 | +//! LANGUAGE_LOADER.get_or_init(|| { |
| 330 | +//! let loader = fluent_language_loader!(); |
327 | 331 | //! |
328 | 332 | //! // Load the fallback langauge by default so that users of the |
329 | 333 | //! // library don't need to if they don't care about localization. |
|
332 | 336 | //! .expect("Error while loading fallback language"); |
333 | 337 | //! |
334 | 338 | //! loader |
335 | | -//! }; |
| 339 | +//! }) |
336 | 340 | //! } |
337 | 341 | //! |
338 | 342 | //! // Get the `Localizer` to be used for localizing this library. |
339 | 343 | //! # #[allow(unused)] |
340 | 344 | //! pub fn localizer() -> Arc<dyn Localizer> { |
341 | | -//! Arc::new(DefaultLocalizer::new(&*LANGUAGE_LOADER, &Localizations)) |
| 345 | +//! Arc::new(DefaultLocalizer::new(&*language_loader(), &Localizations)) |
342 | 346 | //! } |
343 | 347 | //! # } |
344 | 348 | //! ``` |
|
355 | 359 | //! for the library: |
356 | 360 | //! |
357 | 361 | //! ``` |
358 | | -//! # #[cfg(feature = "gettext-system")] |
| 362 | +//! #[cfg(feature = "gettext-system")] |
359 | 363 | //! # { |
360 | | -//! use std::sync::Arc; |
| 364 | +//! use std::sync::{Arc, OnceLock}; |
361 | 365 | //! use i18n_embed::{ |
362 | 366 | //! DefaultLocalizer, Localizer, gettext::{ |
363 | 367 | //! gettext_language_loader, GettextLanguageLoader |
364 | 368 | //! }}; |
365 | 369 | //! use i18n_embed::I18nAssets; |
366 | | -//! use lazy_static::lazy_static; |
367 | 370 | //! |
368 | | -//! lazy_static! { |
369 | | -//! static ref LANGUAGE_LOADER: GettextLanguageLoader = |
370 | | -//! gettext_language_loader!(); |
| 371 | +//! fn language_loader() -> &'static GettextLanguageLoader { |
| 372 | +//! static LANGUAGE_LOADER: OnceLock<GettextLanguageLoader> = OnceLock::new(); |
| 373 | +//! |
| 374 | +//! LANGUAGE_LOADER.get_or_init(|| gettext_language_loader!()) |
371 | 375 | //! } |
372 | 376 | //! |
373 | 377 | //! /// Get the `Localizer` to be used for localizing this library, |
374 | 378 | //! /// using the provided embedded source of language files `embed`. |
375 | 379 | //! # #[allow(unused)] |
376 | 380 | //! pub fn localizer<'a>(embed: &'a (dyn I18nAssets + Send + Sync + 'static)) -> Arc<dyn Localizer + 'a> { |
377 | 381 | //! Arc::new(DefaultLocalizer::new( |
378 | | -//! &*LANGUAGE_LOADER, |
| 382 | +//! &*language_loader(), |
379 | 383 | //! embed |
380 | 384 | //! )) |
381 | 385 | //! } |
|
0 commit comments