Prepare Y10n for actual use

This commit is contained in:
R Tyler Croy 2021-07-05 09:23:18 -07:00
parent 8eef1b1f1d
commit 877a3c95dd
No known key found for this signature in database
GPG Key ID: E5C92681BEF6CEA2
2 changed files with 39 additions and 3 deletions

View File

@ -2,3 +2,38 @@
Yamlization (`y10n`) is a simple Rust-based localization (`l10n`) library. Yamlization (`y10n`) is a simple Rust-based localization (`l10n`) library.
Strings can be defined in `.yml` files which are then merged together. Strings can be defined in `.yml` files which are then merged together.
Typically `y10n` should be used with multiple translation files written in
Yaml. The yaml files can be merged together to provide base translations (e.g.
the English strings) underneath the users preferred language (such as German).
== Example
.en.yml
[source,yaml]
----
greeting: 'hello world'
----
.de.yml
[source,yaml]
----
greeting: 'hallöchen kleiner Mann'
----
.main.rs
[source,rust]
----
use y10n::*;
fn main() {
let y10n = Y10n::from_glob("l10n/**/*.yml");
// Create Language entities based on an `Accept-Languages` header
let langs = parse_accept_languages("en,de;q=0.5");
let translations = y10n.localize(&langs);
// Translations is a serde_yaml::Value which can easily be brought into
// handlebars or other structures for interpolation
}
----

View File

@ -18,6 +18,7 @@ lazy_static! {
/** /**
* Y10n is a stateful struct that can be loaded with localization files * Y10n is a stateful struct that can be loaded with localization files
*/ */
#[derive(Clone, Debug)]
pub struct Y10n { pub struct Y10n {
translations: HashMap<String, serde_yaml::Value>, translations: HashMap<String, serde_yaml::Value>,
} }
@ -35,7 +36,7 @@ impl Y10n {
* For example `"l10n/**/*.yml"` will load all the yml files in the `l10n` directory using each * For example `"l10n/**/*.yml"` will load all the yml files in the `l10n` directory using each
* file's name (e.g. `en.yml`) to derive it's language key (`en`). * file's name (e.g. `en.yml`) to derive it's language key (`en`).
*/ */
fn from_glob(pattern: &str) -> Self { pub fn from_glob(pattern: &str) -> Self {
let mut this = Self::new(); let mut this = Self::new();
trace!( trace!(
"Attempting to load translations from glob pattern: {:?}", "Attempting to load translations from glob pattern: {:?}",
@ -68,7 +69,7 @@ impl Y10n {
* Return a Vec of all the names of languages that have been loaded * Return a Vec of all the names of languages that have been loaded
* These are conventionally just the file stems of the yml files loaded * These are conventionally just the file stems of the yml files loaded
*/ */
fn languages(&self) -> Vec<&String> { pub fn languages(&self) -> Vec<&String> {
self.translations.keys().collect() self.translations.keys().collect()
} }
@ -80,7 +81,7 @@ impl Y10n {
* `en` file has 10, then this function could be called with a Vec of `Language` instances of * `en` file has 10, then this function could be called with a Vec of `Language` instances of
* `[de, en]` and the result would contain the one German string and 9 English strings. * `[de, en]` and the result would contain the one German string and 9 English strings.
*/ */
fn localize(&self, languages: &[Language]) -> serde_yaml::Value { pub fn localize(&self, languages: &[Language]) -> serde_yaml::Value {
use serde_yaml::{Mapping, Value}; use serde_yaml::{Mapping, Value};
let mut values = vec![]; let mut values = vec![];