Skip to content

Add i18n fallback #815

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ lazy_static = "1.2.0"
fluent = "0.5"
fluent-bundle = "0.6.0"
fluent-syntax = "0.9.0"
fluent-locale = "0.4.1"
rand = "0.6"
regex = "1"
rocket = "0.4.1"
Expand Down
52 changes: 42 additions & 10 deletions src/i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,48 @@ use std::io::prelude::*;
use std::path::Path;

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

lazy_static! {
static ref RESOURCES: HashMap<String, Vec<FluentResource>> = build_resources();
static ref BUNDLES: HashMap<String, FluentBundle<'static>> = build_bundles();
static ref LOCALES: Vec<String> = build_locale_list();
}

pub struct Fallback<'res> {
default: Option<&'res str>,
availables: &'res Vec<String>,
}

impl<'res> Fallback<'res> {
pub fn new(available_locales: &'res Vec<String>) -> Self {
Self {
default: Some("en-US"),
availables: available_locales,
}
}

pub fn order<'t, T: AsRef<str>>(&'t self, targets: &'t [T]) -> Vec<&'t str> {
negotiate_languages(
targets,
&self.availables,
self.default,
&fluent_locale::NegotiationStrategy::Filtering,
)
}
}

pub struct I18NHelper {
bundles: &'static HashMap<String, FluentBundle<'static>>,
fallback: Fallback<'static>,
}

impl I18NHelper {
pub fn new() -> Self {
Self { bundles: &*BUNDLES }
Self {
bundles: &*BUNDLES,
fallback: Fallback::new(&*LOCALES),
}
}

pub fn lookup(
Expand Down Expand Up @@ -58,17 +87,12 @@ impl I18NHelper {
text_id: &str,
args: Option<&HashMap<&str, FluentValue>>,
) -> String {
if let Some(val) = self.lookup(lang, text_id, args) {
val
} else if lang != "en-US" {
if let Some(val) = self.lookup("en-US", text_id, args) {
val
} else {
format!("Unknown localization {}", text_id)
for l in self.fallback.order(&[lang]) {
if let Some(val) = self.lookup(l, text_id, args) {
return val;
}
} else {
format!("Unknown localization {}", text_id)
}
format!("Unknown localization {}", text_id)
}
}

Expand Down Expand Up @@ -284,6 +308,14 @@ pub fn create_bundle(lang: &str, resources: &'static Vec<FluentResource>) -> Flu
bundle
}

fn build_locale_list() -> Vec<String> {
let mut locale_list = Vec::new();
for key in RESOURCES.keys() {
locale_list.push(key.clone());
}
locale_list
}

fn build_resources() -> HashMap<String, Vec<FluentResource>> {
let mut all_resources = HashMap::new();
let entries = read_dir("./templates/fluent-resource").unwrap();
Expand Down