1
0
Fork 0
mirror of https://github.com/lise-henry/crowbook synced 2024-05-24 18:26:08 +02:00

Complete localization support

This commit is contained in:
Elisabeth Henry 2016-04-02 14:19:36 +02:00
parent 00b355c1c9
commit 05e1f530ee
8 changed files with 27 additions and 24 deletions

View File

@ -1,4 +1,4 @@
menu: Menu
menu: Table of contents
display_all: Display all chapters
display_one: Display one chapter

View File

@ -1,4 +1,4 @@
menu: Menu
menu: Table des matières
display_all: Afficher tous les chapitres
display_one: "N'afficher qu'un chapitre"

View File

@ -13,6 +13,7 @@ use escape;
use number::Number;
use resource_handler::ResourceHandler;
use logger::{Logger, InfoLevel};
use lang;
use std::fs::File;
use std::io::{Write, Read};
@ -524,6 +525,9 @@ impl Book {
/// * "author"
/// * "title"
/// * "lang"
///
/// It also uses the lang/xx.yaml file corresponding to the language and fills
/// `loc_xxx` fiels with it that corresponds to translated versions
pub fn get_mapbuilder(&self, format: &str) -> MapBuilder {
fn clone(x:&str) -> String {
x.to_owned()
@ -534,10 +538,17 @@ impl Book {
"tex" => escape::escape_tex,
_ => panic!("get mapbuilder called with invalid escape format")
};
MapBuilder::new()
let mut mapbuilder = MapBuilder::new()
.insert_str("author", f(self.options.get_str("author").unwrap()))
.insert_str("title", f(&self.options.get_str("title").unwrap()))
.insert_str("lang", self.options.get_str("lang").unwrap().to_owned())
.insert_str("lang", self.options.get_str("lang").unwrap().to_owned());
let hash = lang::get_hash(self.options.get_str("lang").unwrap());
for (key, value) in hash.into_iter() {
let key = format!("loc_{}", key.as_str().unwrap());
let value = value.as_str().unwrap();
mapbuilder = mapbuilder.insert_str(&key, f(value));
}
mapbuilder
}
/// Remove YAML blocks from a string and try to parse them to set options

View File

@ -24,7 +24,6 @@ use toc::Toc;
use resource_handler::ResourceHandler;
use std::borrow::Cow;
use templates::html;
use lang::localize;
use mustache;
use rustc_serialize::base64::{self, ToBase64};
@ -207,10 +206,6 @@ impl<'a> HtmlRenderer<'a> {
.insert_str("book_svg", &book_svg)
.insert_str("pages_svg", &pages_svg)
.insert_bool("display_chapter", self.book.options.get_bool("html.display_chapter").unwrap())
.insert_str("loc_display_one", localize(self.book.options.get_str("lang").unwrap(),
"display_one"))
.insert_str("loc_display_all", localize(self.book.options.get_str("lang").unwrap(),
"display_all"))
.build();
let mut res:Vec<u8> = vec!();
template_js.render_data(&mut res, &data);
@ -228,10 +223,6 @@ impl<'a> HtmlRenderer<'a> {
.insert_str("menu_svg", menu_svg)
.insert_str("book_svg", book_svg)
.insert_str("pages_svg", pages_svg)
.insert_str("loc_display_one", localize(self.book.options.get_str("lang").unwrap(),
"display_one"))
.insert_str("loc_display_all", localize(self.book.options.get_str("lang").unwrap(),
"display_all"))
.build();
let template = mustache::compile_str(try!(self.book.get_template("html.template")).as_ref());
let mut res = vec!();

View File

@ -1,18 +1,19 @@
use yaml_rust::{YamlLoader, Yaml};
use yaml_rust::yaml::Hash;
static EN:&'static str = include_str!("../../lang/en.yaml");
static FR:&'static str = include_str!("../../lang/fr.yaml");
/// Localize a string in a given language
pub fn localize(lang: &str, s: &str) -> String {
/// Get the hashmap for a given language
pub fn get_hash(lang: &str) -> Hash {
let docs = match lang {
"fr" => YamlLoader::load_from_str(FR).unwrap(),
_ => YamlLoader::load_from_str(EN).unwrap(),
};
let hash = docs[0].as_hash().unwrap();
String::from(hash.get(&Yaml::from_str(s))
.unwrap()
.as_str()
.unwrap())
let elem = docs.into_iter().next().unwrap();
if let Yaml::Hash(hash) = elem {
hash
} else {
panic!("Yaml file for language {} didn't contain a hash", lang);
}
}

View File

@ -27,7 +27,7 @@
<header>
<div id = "menu">
<img id = "menu-button" onclick="toggle();"
src="menu.svg" alt = "Menu" title = "Menu" />
src="menu.svg" alt = "{{{loc_menu}}}" title = "{{{loc_menu}}}" />
</div>
</header>
{{{prev_chapter}}}

View File

@ -27,7 +27,7 @@
<header>
<div id = "menu">
<img id = "menu-button" onclick="toggle();"
src="menu.svg" alt = "Menu" title = "Menu" />
src="menu.svg" alt = "{{{loc_menu}}}" title = "{{{loc_menu}}}" />
</div>
<h1 class = "title">{{{title}}}</h1>

View File

@ -36,7 +36,7 @@
<div id = "menu">
<img id = "menu-button" onclick="toggle();"
src="{{{menu_svg}}}"
alt = "Menu" title = "Menu" />
alt = "{{{loc_menu}}}" title = "{{{loc_menu}}}" />
{{#display_chapter}}
{{/display_chapter}}
</div>