Debian Codegraph @

debian/rust-vendor/intl-memoizer/README.md

raw
1 # IntlMemoizer
2
3 [![crates.io](https://img.shields.io/crates/v/intl-memoizer.svg)](https://crates.io/crates/intl-memoizer)
4 [![docs.rs](https://img.shields.io/docsrs/intl-memoizer)](https://docs.rs/intl-memoizer)
5 [![Build](https://github.com/projectfluent/fluent-rs/actions/workflows/test.yaml/badge.svg)](https://github.com/projectfluent/fluent-rs/actions/workflows/test.yaml)
6 [![Coverage Status](https://coveralls.io/repos/github/projectfluent/fluent-rs/badge.svg?branch=main)](https://coveralls.io/github/projectfluent/fluent-rs?branch=main)
7
8 The `fluent-rs` workspace is a collection of Rust crates implementing [Project Fluent][],
9 a localization system designed to unleash the entire expressive power of natural language translations.
10
11 This crate is a memoizer specifically tailored for storing lazy-initialized intl formatters.
12
13 [Project Fluent]: https://projectfluent.org
14
15 Usage
16 -----
17
18 The assumption is that allocating a new formatter instance is costly, and such
19 instance is read-only during its life time, with constructor being expensive, and
20 `format`/`select` calls being cheap.
21
22 In result it pays off to use a singleton to manage memoization of all instances of intl
23 APIs such as `PluralRules`, `DateTimeFormat` etc. between all `FluentBundle` instances.
24
25 The following is a high-level example of how this works, for running examples see
26 the [docs](https://docs.rs/intl-memoizer/)
27
28 ```rust
29 /// Internationalization formatter should implement the Memoizable trait.
30 impl Memoizable for NumberFormat {
31 ...
32 }
33
34 // The main memoizer has weak references to all of the per-language memoizers.
35 let mut memoizer = IntlMemoizer::default();
36
37 // The formatter memoziation happens per-locale.
38 let lang = "en-US".parse().expect("Failed to parse.");
39 let lang_memoizer: Rc<IntlLangMemoizer> = memoizer.get_for_lang(en_us);
40
41 // Run the formatter
42
43 let options: NumberFormatOptions {
44 minimum_fraction_digits: 3,
45 maximum_fraction_digits: 5,
46 };
47
48 // Format pi with the options. This will lazily construct the NumberFormat.
49 let pi = lang_memoizer
50 .with_try_get::<NumberFormat, _, _>((options,), |nf| nf.format(3.141592653))
51 .unwrap()
52
53 // The example formatter constructs a string with diagnostic information about
54 // the configuration.
55 assert_eq!(text, "3.14159");
56
57 // Running it again will use the previous formatter.
58 let two = lang_memoizer
59 .with_try_get::<NumberFormat, _, _>((options,), |nf| nf.format(2.0))
60 .unwrap()
61
62 assert_eq!(text, "2.000");
63 ```
64
65 Get Involved
66 ------------
67
68 `fluent-rs` is open-source, licensed under both the Apache 2.0 and MIT licenses. We
69 encourage everyone to take a look at our code and we'll listen to your
70 feedback.
71
72
73 Discuss
74 -------
75
76 We'd love to hear your thoughts on Project Fluent! Whether you're a localizer
77 looking for a better way to express yourself in your language, or a developer
78 trying to make your app localizable and multilingual, or a hacker looking for
79 a project to contribute to, please do get in touch on the mailing list and the
80 IRC channel.
81
82 - Discourse: https://discourse.mozilla.org/c/fluent
83 - Matrix channel: <a href="https://chat.mozilla.org/#/room/#fluent:mozilla.org">#fluent:mozilla.org</a>