mirror of
https://github.com/lise-henry/crowbook
synced 2024-09-28 02:59:45 +02:00
Add support for localisation in generated files
This commit is contained in:
parent
e6ab171fdb
commit
00b355c1c9
4
lang/en.yaml
Normal file
4
lang/en.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
menu: Menu
|
||||
|
||||
display_all: Display all chapters
|
||||
display_one: Display one chapter
|
4
lang/fr.yaml
Normal file
4
lang/fr.yaml
Normal file
@ -0,0 +1,4 @@
|
||||
menu: Menu
|
||||
|
||||
display_all: Afficher tous les chapitres
|
||||
display_one: "N'afficher qu'un chapitre"
|
@ -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
18
src/lib/lang.rs
Normal 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())
|
||||
}
|
@ -136,6 +136,7 @@ mod number;
|
||||
mod resource_handler;
|
||||
mod logger;
|
||||
mod bookoptions;
|
||||
mod lang;
|
||||
|
||||
mod escape;
|
||||
mod toc;
|
||||
|
@ -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}}}";
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -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>
|
||||
|
Loading…
Reference in New Issue
Block a user