|
1 |
| -use error_chain::{bail, error_chain}; |
| 1 | +use crate::translator::errors::*; |
2 | 2 | use fluent_bundle::{FluentArgs, FluentBundle, FluentResource};
|
3 | 3 | use std::rc::Rc;
|
4 | 4 | use unic_langid::{LanguageIdentifier, LanguageIdentifierError};
|
5 | 5 |
|
6 |
| -/// Defines app information about i18n, specifically about which locales are supported. |
7 |
| -#[derive(Clone)] |
8 |
| -pub struct Locales { |
9 |
| - /// The default locale, which will be used as a fallback if the user's locale can't be extracted. This will be built for at build-time. |
10 |
| - pub default: String, |
11 |
| - /// All other supported locales, which will all be built at build time. |
12 |
| - pub other: Vec<String>, |
13 |
| -} |
14 |
| -impl Locales { |
15 |
| - /// Gets all the supported locales by combining the default, and other. |
16 |
| - pub fn get_all(&self) -> Vec<&String> { |
17 |
| - let mut vec: Vec<&String> = vec![&self.default]; |
18 |
| - vec.extend(&self.other); |
19 |
| - |
20 |
| - vec |
21 |
| - } |
22 |
| - /// Checks if the given locale is supported. |
23 |
| - pub fn is_supported(&self, locale: &str) -> bool { |
24 |
| - let locales = self.get_all(); |
25 |
| - locales.iter().any(|l| *l == locale) |
26 |
| - } |
27 |
| -} |
28 |
| - |
29 |
| -// This has its own error management because the user might be implementing translators themselves |
30 |
| -error_chain! { |
31 |
| - errors { |
32 |
| - /// For when a translation ID doesn't exist. |
33 |
| - TranslationIdNotFound(id: String, locale: String) { |
34 |
| - description("translation id not found for current locale") |
35 |
| - display("translation id '{}' not found for locale '{}'", id, locale) |
36 |
| - } |
37 |
| - /// For when the given string of translations couldn't be correctly parsed |
38 |
| - TranslationsStrSerFailed(locale: String, err: String) { |
39 |
| - description("given translations string couldn't be parsed") |
40 |
| - display("given translations string for locale '{}' couldn't be parsed: '{}'", locale, err) |
41 |
| - } |
42 |
| - /// For when the given locale was invalid. This takes an error because different i18n systems may have different requirements. |
43 |
| - InvalidLocale(locale: String, err: String) { |
44 |
| - description("given locale was invalid") |
45 |
| - display("given locale '{}' was invalid: '{}'", locale, err) |
46 |
| - } |
47 |
| - /// For when the translation of a message failed for some reason generally. |
48 |
| - TranslationFailed(id: String, locale: String, err: String) { |
49 |
| - description("message translation failed") |
50 |
| - display("translation of message with id '{}' into locale '{}' failed: '{}'", id, locale, err) |
51 |
| - } |
52 |
| - /// For when the we couldn't arrive at a translation for some reason. This might be caused by an invalid variant for a compound |
53 |
| - /// message. |
54 |
| - NoTranslationDerived(id: String, locale: String) { |
55 |
| - description("no translation derived for message") |
56 |
| - display("no translation could be derived for message with id '{}' in locale '{}'", id, locale) |
57 |
| - } |
58 |
| - } |
59 |
| -} |
| 6 | +/// The file extension used by the Fluent translator, which expects FTL files. |
| 7 | +pub const FLUENT_TRANSLATOR_FILE_EXT: &str = "ftl"; |
60 | 8 |
|
61 | 9 | /// Manages translations on the client-side for a single locale using Mozilla's [Fluent](https://projectfluent.org/) syntax. This
|
62 | 10 | /// should generally be placed into an `Rc<T>` and referred to by every template in an app. You do NOT want to be cloning potentially
|
63 | 11 | /// thousands of translations!
|
64 | 12 | ///
|
65 | 13 | /// Fluent supports compound messages, with many variants, which can specified here using the form `[id].[variant]` in a translation ID,
|
66 | 14 | /// as a `.` is not valid in an ID anyway, and so can be used as a delimiter. More than one dot will result in an error.
|
67 |
| -pub struct Translator { |
| 15 | +pub struct FluentTranslator { |
68 | 16 | /// Stores the internal Fluent data for translating. This bundle directly owns its attached resources (translations).
|
69 | 17 | bundle: Rc<FluentBundle<FluentResource>>,
|
70 | 18 | /// The locale for which translations are being managed by this instance.
|
71 | 19 | locale: String,
|
72 | 20 | }
|
73 |
| -impl Translator { |
| 21 | +impl FluentTranslator { |
74 | 22 | /// Creates a new translator for a given locale, passing in translations in FTL syntax form.
|
75 | 23 | pub fn new(locale: String, ftl_string: String) -> Result<Self> {
|
76 | 24 | let resource = FluentResource::try_new(ftl_string)
|
@@ -197,12 +145,3 @@ impl Translator {
|
197 | 145 | Rc::clone(&self.bundle)
|
198 | 146 | }
|
199 | 147 | }
|
200 |
| - |
201 |
| -/// A super-shortcut for translating stuff. Your translator must be named `translator` for this to work. |
202 |
| -// FIXME |
203 |
| -#[macro_export] |
204 |
| -macro_rules! t { |
205 |
| - ($id:literal, $translator:expr) => { |
206 |
| - $translator.translate($id) |
207 |
| - }; |
208 |
| -} |
0 commit comments