From 046de8c998f4f124781a90de6ffe3edf57809932 Mon Sep 17 00:00:00 2001 From: Baptiste Darthenay Date: Sat, 13 Jan 2018 00:10:19 +0100 Subject: [PATCH] Config with i18n --- build.rs | 2 +- components/config/src/lib.rs | 34 +++++++++++++++++-- components/site/src/lib.rs | 1 + components/templates/src/global_fns.rs | 46 +++++++++++++++++++++++++- 4 files changed, 78 insertions(+), 5 deletions(-) diff --git a/build.rs b/build.rs index bc9dc8f5..5e9624fd 100644 --- a/build.rs +++ b/build.rs @@ -1,7 +1,7 @@ #[macro_use] extern crate clap; -use clap::Shell; +// use clap::Shell; include!("src/cli.rs"); diff --git a/components/config/src/lib.rs b/components/config/src/lib.rs index d6722d74..f1a07624 100644 --- a/components/config/src/lib.rs +++ b/components/config/src/lib.rs @@ -38,7 +38,7 @@ pub struct Config { /// Description of the site pub description: Option, /// The language used in the site. Defaults to "en" - pub language_code: Option, + pub default_language: Option, /// Whether to generate RSS. Defaults to false pub generate_rss: Option, /// The number of articles to include in the RSS feed. Defaults to unlimited @@ -50,6 +50,9 @@ pub struct Config { /// Whether to compile the `sass` directory and output the css files into the static folder pub compile_sass: Option, + /// Languages list and translated strings + pub translations: Option>, + /// All user params set in [extra] in the config pub extra: Option>, @@ -74,13 +77,14 @@ impl Config { Err(e) => bail!(e) }; - set_default!(config.language_code, "en".to_string()); + set_default!(config.default_language, "en".to_string()); set_default!(config.highlight_code, false); set_default!(config.generate_rss, false); set_default!(config.rss_limit, 20); set_default!(config.generate_tags_pages, false); set_default!(config.generate_categories_pages, false); set_default!(config.compile_sass, false); + set_default!(config.translations, HashMap::new()); set_default!(config.extra, HashMap::new()); match config.highlight_theme { @@ -166,12 +170,13 @@ impl Default for Config { highlight_code: Some(true), highlight_theme: Some("base16-ocean-dark".to_string()), description: None, - language_code: Some("en".to_string()), + default_language: Some("en".to_string()), generate_rss: Some(false), rss_limit: Some(10_000), generate_tags_pages: Some(true), generate_categories_pages: Some(true), compile_sass: Some(false), + translations: None, extra: None, build_timestamp: Some(1), } @@ -302,4 +307,27 @@ a_value = 10 assert_eq!(extra["hello"].as_str().unwrap(), "world".to_string()); assert_eq!(extra["a_value"].as_integer().unwrap(), 10); } + + #[test] + fn can_use_language_configuration() { + let config = r#" +base_url = "https://remplace-par-ton-url.fr" +default_language = "fr" + +[translations] +[translations.fr] +title = "Un titre" + +[translations.en] +title = "A title" + + "#; + + let config = Config::parse(config); + assert!(config.is_ok()); + let translations = config.unwrap().translations.unwrap(); + assert_eq!(translations["fr"]["title"].as_str().unwrap(), "Un titre"); + assert_eq!(translations["en"]["title"].as_str().unwrap(), "A title"); + } + } diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 3eb011ce..7ac1b7d3 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -259,6 +259,7 @@ impl Site { } pub fn register_tera_global_fns(&mut self) { + self.tera.register_global_function("trans", global_fns::make_trans(self.config.clone())); self.tera.register_global_function("get_page", global_fns::make_get_page(&self.pages)); self.tera.register_global_function("get_section", global_fns::make_get_section(&self.sections)); self.tera.register_global_function( diff --git a/components/templates/src/global_fns.rs b/components/templates/src/global_fns.rs index 44284ff8..a0c76726 100644 --- a/components/templates/src/global_fns.rs +++ b/components/templates/src/global_fns.rs @@ -21,6 +21,21 @@ macro_rules! required_string_arg { }; } + +pub fn make_trans(config: Config) -> GlobalFn { + let translations_config = config.translations.unwrap(); + let default_lang = to_value(config.default_language.unwrap()).unwrap(); + + Box::new(move |args| -> Result { + let key = required_string_arg!(args.get("key"), "`trans` requires a `key` argument."); + let lang_arg = args.get("lang").unwrap_or(&default_lang).clone(); + let lang = from_value::(lang_arg).unwrap(); + let translations = &translations_config[lang.as_str()]; + Ok(to_value(&translations[key.as_str()]).unwrap()) + }) +} + + pub fn make_get_page(all_pages: &HashMap) -> GlobalFn { let mut pages = HashMap::new(); for page in all_pages.values() { @@ -111,7 +126,7 @@ pub fn make_get_taxonomy_url(tags: Option, categories: Option