| 1 |
# IntlMemoizer |
| 2 |
|
| 3 |
`intl-memoizer` is a crate designed to handle lazy-initialized references |
| 4 |
to intl formatters. |
| 5 |
|
| 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. |
| 9 |
|
| 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. |
| 12 |
|
| 13 |
Usage |
| 14 |
----- |
| 15 |
|
| 16 |
```rust |
| 17 |
use intl_memoizer::{IntlMemoizer, Memoizable}; |
| 18 |
use unic_langid::langid; |
| 19 |
|
| 20 |
use intl_pluralrules::{PluralRules, PluralRuleType, PluralCategory}; |
| 21 |
|
| 22 |
impl Memoizable for PluralRules { |
| 23 |
type Args = (PluralRulesType,); |
| 24 |
fn construct(lang: LanguageIdentifier, args: Self::Args) -> Self { |
| 25 |
Self::new(lang, args.0) |
| 26 |
} |
| 27 |
} |
| 28 |
|
| 29 |
fn main() { |
| 30 |
let lang = langid!("en-US"); |
| 31 |
|
| 32 |
// A single memoizer for all languages |
| 33 |
let mut memoizer = IntlMemoizer::new(); |
| 34 |
|
| 35 |
// A RefCell for a particular language to be used in all `FluentBundle` |
| 36 |
// instances. |
| 37 |
let mut en_us_memoizer = memoizer.get_for_lang(lang.clone()); |
| 38 |
|
| 39 |
// Per-call borrow |
| 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); |
| 43 |
} |
| 44 |
|
| 45 |
``` |
| 46 |
|
| 47 |
Get Involved |
| 48 |
------------ |
| 49 |
|
| 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 |
| 52 |
feedback. |
| 53 |
|
| 54 |
|
| 55 |
Discuss |
| 56 |
------- |
| 57 |
|
| 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 |
| 62 |
IRC channel. |
| 63 |
|
| 64 |
- Discourse: https://discourse.mozilla.org/c/fluent |
| 65 |
- IRC channel: [irc://irc.mozilla.org/l20n](irc://irc.mozilla.org/l20n) |