3 `intl-memoizer` is a crate designed to handle lazy-initialized references
6 The assumption is that allocating a new formatter instance is costly, and such
7 instance is read-only during its life time, with constructor being expensive, and
8 `format`/`select` calls being cheap.
10 In result it pays off to use a singleton to manage memoization of all instances of intl
11 APIs such as `PluralRules`, DateTimeFormat` etc. between all `FluentBundle` instances.
17 use intl_memoizer::{IntlMemoizer, Memoizable};
18 use unic_langid::langid;
20 use intl_pluralrules::{PluralRules, PluralRuleType, PluralCategory};
22 impl Memoizable for PluralRules {
23 type Args = (PluralRulesType,);
24 fn construct(lang: LanguageIdentifier, args: Self::Args) -> Self {
25 Self::new(lang, args.0)
30 let lang = langid!("en-US");
32 // A single memoizer for all languages
33 let mut memoizer = IntlMemoizer::new();
35 // A RefCell for a particular language to be used in all `FluentBundle`
37 let mut en_us_memoizer = memoizer.get_for_lang(lang.clone());
40 let mut en_us_memoizer_borrow = en_us_memoizer.borrow_mut();
41 let cb = en_us_memoizer_borrow.get::<PluralRules>((PluralRulesType::Cardinal,));
42 assert_eq!(cb.select(1), PluralCategory::One);
50 `fluent-rs` is open-source, licensed under the Apache License, Version 2.0. We
51 encourage everyone to take a look at our code and we'll listen to your
58 We'd love to hear your thoughts on Project Fluent! Whether you're a localizer
59 looking for a better way to express yourself in your language, or a developer
60 trying to make your app localizable and multilingual, or a hacker looking for
61 a project to contribute to, please do get in touch on the mailing list and the
64 - Discourse: https://discourse.mozilla.org/c/fluent
65 - IRC channel: [irc://irc.mozilla.org/l20n](irc://irc.mozilla.org/l20n)