From 6acd7c7be668a50d1309136f787065ec7b969213 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Fri, 30 Nov 2018 10:38:29 +0100 Subject: [PATCH 1/2] Add new macro importing translation to the final binary Add a new macro include_i18n!, taking similar arguments to i18n(), and expanding to the same type. It allow to include translations in the final binary, so that once compiled, it can be run from anywhere, without depending on `.mo`s to be present --- src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index c9429e1..7b1bc45 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,6 +124,27 @@ pub fn i18n(domain: &str, lang: Vec<&'static str>) -> Translations { }) } +#[macro_export] +macro_rules! include_i18n { + ( $domain:tt, [$($lang:tt),*] ) => { + { + use $crate::Catalog; + vec![ + $( + ( + $lang, + Catalog::parse( + &include_bytes!( + concat!(env!("CARGO_MANIFEST_DIR"), "/translations/", $lang, "/LC_MESSAGES/", $domain, ".mo") + )[..] + ).expect("Error while loading catalog") + ) + ),* + ] + } + } +} + #[cfg(feature = "build")] pub fn update_po(domain: &str, locales: &[&'static str]) { use std::{path::Path, process::Command}; -- 2.38.5 From 25fbb741db395806a5e05f230f46ff74d96bfe01 Mon Sep 17 00:00:00 2001 From: Trinity Pointard Date: Fri, 30 Nov 2018 10:56:52 +0100 Subject: [PATCH 2/2] Add documentation for include_i18n! --- src/lib.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index 7b1bc45..46e7170 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -124,6 +124,15 @@ pub fn i18n(domain: &str, lang: Vec<&'static str>) -> Translations { }) } +/// Use this macro to staticaly import translations into your final binary. It's use is similar to +/// [`i18n`](../rocket_i18n/fn.i18n.html) +/// ```rust,ignore +/// # //ignore because there is no translation file provided with rocket_i18n +/// # #[macro_use] +/// # extern crate rocket_i18n; +/// # use rocket_i18n::Translations; +/// let tr: Translations = include_i18n!("plume", ["de", "en", "fr"]); +/// ``` #[macro_export] macro_rules! include_i18n { ( $domain:tt, [$($lang:tt),*] ) => { -- 2.38.5