1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
use std::collections::HashMap;
use std::fs::read_dir;
use std::fs::File;
use std::io;
use std::io::prelude::*;
use std::path::Path;

use fluent_bundle::{FluentBundle, FluentResource, FluentValue};
use fluent_locale::negotiate_languages;

pub trait Loader {
    fn lookup(
        &self,
        lang: &str,
        text_id: &str,
        args: Option<&HashMap<&str, FluentValue>>,
    ) -> String;
}

#[macro_export]
macro_rules! simple_loader {
    ($constructor:ident, $location:expr, $fallback:expr) => {
        use $crate::loader::{build_resources, build_bundles, build_fallbacks, SimpleLoader, load_core_resource};
        use std::collections::HashMap;
        use $crate::fluent_bundle::{FluentBundle, FluentResource, FluentValue};
        $crate::lazy_static::lazy_static! {
            static ref RESOURCES: HashMap<String, Vec<FluentResource>> = build_resources($location);
            static ref BUNDLES: HashMap<String, FluentBundle<'static>> = build_bundles(&&RESOURCES, None);
            static ref LOCALES: Vec<&'static str> = RESOURCES.iter().map(|(l, _)| &**l).collect();
            static ref FALLBACKS: HashMap<String, Vec<String>> = build_fallbacks(&LOCALES);
        }

        pub fn $constructor() -> SimpleLoader {
            SimpleLoader::new(&*BUNDLES, &*FALLBACKS, $fallback.into())
        }
    };
    ($constructor:ident, $location:expr, $fallback:expr, core: $core:expr) => {
        use $crate::loader::{build_resources, build_bundles, build_fallbacks, SimpleLoader, load_core_resource};
        use std::collections::HashMap;
        use $crate::fluent_bundle::{FluentBundle, FluentResource, FluentValue};
        $crate::lazy_static::lazy_static! {
            static ref CORE_RESOURCE: FluentResource = load_core_resource($core);
            static ref RESOURCES: HashMap<String, Vec<FluentResource>> = build_resources($location);
            static ref BUNDLES: HashMap<String, FluentBundle<'static>> = build_bundles(&*RESOURCES, Some(&CORE_RESOURCE));
            static ref LOCALES: Vec<&'static str> = RESOURCES.iter().map(|(l, _)| &**l).collect();
            static ref FALLBACKS: HashMap<String, Vec<String>> = build_fallbacks(&LOCALES);
        }

        pub fn $constructor() -> SimpleLoader {
            SimpleLoader::new(&*BUNDLES, &*FALLBACKS, $fallback.into())
        }
    };
}

pub fn build_fallbacks(locales: &[&str]) -> HashMap<String, Vec<String>> {
    locales
        .iter()
        .map(|locale| {
            (
                locale.to_string(),
                negotiate_languages(
                    &[locale],
                    &locales,
                    None,
                    &fluent_locale::NegotiationStrategy::Filtering,
                )
                .into_iter()
                .map(|x| x.to_string())
                .collect(),
            )
        })
        .collect()
}

pub struct SimpleLoader {
    bundles: &'static HashMap<String, FluentBundle<'static>>,
    fallbacks: &'static HashMap<String, Vec<String>>,
    fallback: String,
}

impl SimpleLoader {
    pub fn new(
        bundles: &'static HashMap<String, FluentBundle<'static>>,
        fallbacks: &'static HashMap<String, Vec<String>>,
        fallback: String,
    ) -> Self {
        Self {
            bundles,
            fallbacks,
            fallback,
        }
    }

    pub fn lookup_single_language(
        &self,
        lang: &str,
        text_id: &str,
        args: Option<&HashMap<&str, FluentValue>>,
    ) -> Option<String> {
        if let Some(bundle) = self.bundles.get(lang) {
            if bundle.has_message(text_id) {
                let (value, _errors) = bundle.format(text_id, args).unwrap_or_else(|| {
                    panic!(
                        "Failed to format a message for locale {} and id {}",
                        lang, text_id
                    )
                });
                Some(value)
            } else {
                None
            }
        } else {
            panic!("Unknown language {}", lang)
        }
    }

    // Don't fall back to English
    pub fn lookup_no_english(
        &self,
        lang: &str,
        text_id: &str,
        args: Option<&HashMap<&str, FluentValue>>,
    ) -> Option<String> {
        for l in self.fallbacks.get(lang).expect("language not found") {
            if let Some(val) = self.lookup_single_language(l, text_id, args) {
                return Some(val);
            }
        }

        None
    }
}

impl Loader for SimpleLoader {
    // Traverse the fallback chain,
    fn lookup(
        &self,
        lang: &str,
        text_id: &str,
        args: Option<&HashMap<&str, FluentValue>>,
    ) -> String {
        for l in self.fallbacks.get(lang).expect("language not found") {
            if let Some(val) = self.lookup_single_language(l, text_id, args) {
                return val;
            }
        }
        if lang != self.fallback {
            if let Some(val) = self.lookup_single_language(&self.fallback, text_id, args) {
                return val;
            }
        }
        format!("Unknown localization {}", text_id)
    }
}

fn read_from_file<P: AsRef<Path>>(filename: P) -> io::Result<FluentResource> {
    let mut file = File::open(filename)?;
    let mut string = String::new();

    file.read_to_string(&mut string)?;

    Ok(FluentResource::try_new(string).expect("File did not parse!"))
}

fn read_from_dir<P: AsRef<Path>>(dirname: P) -> io::Result<Vec<FluentResource>> {
    let mut result = Vec::new();
    for dir_entry in read_dir(dirname)? {
        let entry = dir_entry?;
        let resource = read_from_file(entry.path())?;
        result.push(resource);
    }
    Ok(result)
}

pub fn create_bundle(
    lang: &str,
    resources: &'static Vec<FluentResource>,
    core_resource: Option<&'static FluentResource>,
) -> FluentBundle<'static> {
    let mut bundle = FluentBundle::new(&[lang]);
    if let Some(core) = core_resource {
        bundle
            .add_resource(core)
            .expect("Failed to add core resource to bundle");
    }
    for res in resources {
        bundle
            .add_resource(res)
            .expect("Failed to add FTL resources to the bundle.");
    }

    bundle
        .add_function("EMAIL", |values, _named| {
            let email = match *values.get(0)?.as_ref()? {
                FluentValue::String(ref s) => s,
                _ => return None,
            };
            Some(FluentValue::String(format!(
                "<a href='mailto:{0}' lang='en-US'>{0}</a>",
                email
            )))
        })
        .expect("could not add function");

    bundle
        .add_function("ENGLISH", |values, _named| {
            let text = match *values.get(0)?.as_ref()? {
                FluentValue::String(ref s) => s,
                _ => return None,
            };
            Some(FluentValue::String(format!(
                "<span lang='en-US'>{0}</span>",
                text
            )))
        })
        .expect("could not add function");

    bundle
}

pub fn build_resources(dir: &str) -> HashMap<String, Vec<FluentResource>> {
    let mut all_resources = HashMap::new();
    let entries = read_dir(dir).unwrap();
    for entry in entries {
        let entry = entry.unwrap();
        if entry.file_type().unwrap().is_dir() {
            if let Ok(lang) = entry.file_name().into_string() {
                let resources = read_from_dir(entry.path()).unwrap();
                all_resources.insert(lang, resources);
            }
        }
    }
    all_resources
}

pub fn build_bundles(
    resources: &'static HashMap<String, Vec<FluentResource>>,
    core_resource: Option<&'static FluentResource>,
) -> HashMap<String, FluentBundle<'static>> {
    let mut bundles = HashMap::new();
    for (ref k, ref v) in &*resources {
        bundles.insert(k.to_string(), create_bundle(&k, &v, core_resource));
    }
    bundles
}

pub fn load_core_resource(path: &str) -> FluentResource {
    read_from_file(path).expect("cannot find core resource")
}