1
0
mirror of https://github.com/lise-henry/crowbook synced 2024-09-20 07:12:10 +02:00

Add support for localisation in generated files

This commit is contained in:
Elisabeth Henry 2016-04-02 13:58:36 +02:00
parent e6ab171fdb
commit 00b355c1c9
7 changed files with 41 additions and 5 deletions

4
lang/en.yaml Normal file
View File

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

4
lang/fr.yaml Normal file
View File

@ -0,0 +1,4 @@
menu: Menu
display_all: Afficher tous les chapitres
display_one: "N'afficher qu'un chapitre"

View File

@ -24,6 +24,7 @@ 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};
@ -206,6 +207,10 @@ 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);
@ -223,6 +228,10 @@ 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!();

18
src/lib/lang.rs Normal file
View File

@ -0,0 +1,18 @@
use yaml_rust::{YamlLoader, Yaml};
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 {
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())
}

View File

@ -136,6 +136,7 @@ mod number;
mod resource_handler;
mod logger;
mod bookoptions;
mod lang;
mod escape;
mod toc;

View File

@ -47,15 +47,15 @@ function switchAll() {
}
displayAllSwitcher = document.getElementById("book-button");
displayAllSwitcher.src="{{{pages_svg}}}";
displayAllSwitcher.alt="Display one chapter";
displayAllSwitcher.title="Display one chapter";
displayAllSwitcher.alt="{{{loc_display_one}}}";
displayAllSwitcher.title="{{{loc_display_one}}}";
}else{
displayAll = false;
showChapter(0);
displayAllSwitcher = document.getElementById("book-button");
displayAllSwitcher.src="{{{book_svg}}}";
displayAllSwitcher.alt="Display all chapters";
displayAllSwitcher.title="Display all chapters";
displayAllSwitcher.alt="{{{loc_display_all}}}";
displayAllSwitcher.title="{{{loc_display_all}}}";
}
}

View File

@ -22,7 +22,7 @@
<nav id = "nav">
<div id="toolbar">
<img id="book-button" alt="Display mode"
title = "Display all chapters"
title = "{{{loc_display_all}}}"
src="{{{book_svg}}}"
onclick="javascript:switchAll()">
</div>