Add new macro importing translation to the final binary #12

Merged
kiwii merged 2 commits from plume/rocket_i18n:master into master 4 years ago
  1. 30
      src/lib.rs

30
src/lib.rs

@ -124,6 +124,36 @@ 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),*] ) => {
elegaanz commented 4 years ago (Migrated from github.com)
Review

Is a string literal a single tt?

Is a string literal a single `tt`?
elegaanz commented 4 years ago (Migrated from github.com)
Review

I just tested it, and it works. 👌

I just tested it, and it works. :ok_hand:
Review

to be honest, I didn't know the type I needed to use, so I put the first one I thought of, and I tested if the macro worked as intended in Plume (which confirm Plume only needs that to become a standalone binary). It's totally possible provided with some bad arguments this macro fail in some strange way

to be honest, I didn't know the type I needed to use, so I put the first one I thought of, and I tested if the macro worked as intended in Plume (which confirm Plume only needs that to become a standalone binary). It's totally possible provided with some bad arguments this macro fail in some strange way
{
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};

Loading…
Cancel
Save