pub struct Extractor { /* private fields */ }
Expand description
Struct that extracts all messages from source code and can print them
to a .pot
file.
This file can then be used as a starting point to begin translation.
It should be relatively similar to gettext
generated files.
§Example
use crowbook_intl::Extractor;
let mut extractor = Extractor::new();
extractor.add_messages_from_dir("src/").unwrap();
println!("{}", extractor.generate_pot_file());
§Note
This struct only add messages that are considered as needing localization,
that is, the first argument of calls so lformat!
macro.
Implementations§
Source§impl Extractor
impl Extractor
Sourcepub fn new() -> Extractor
pub fn new() -> Extractor
Create a new, empty extractor
Examples found in repository?
More examples
5fn main() {
6 let str_fr = r#"
7msgid "hello, {}"
8msgstr "bonjour, {}"
9
10msgid "Shit: \"{}\" went wrong;"
11msgstr "Chiotte: \"{}\" est parti en live;"
12
13msgid "kwak!"
14msgstr "coin !"
15"#;
16
17 let str_es = r#"
18msgid "hello, {}"
19msgstr "hola, {}"
20
21msgid "Oi!"
22msgstr "¡Oi!"
23"#;
24 let extractor = Extractor::new();
25 let mut localizer = Localizer::new(&extractor);
26 localizer.add_lang("fr", str_fr).unwrap();
27 localizer.add_lang("es", str_es).unwrap();
28 println!("{}", localizer.generate_macro_file());
29}
Sourcepub fn original_strings<'a>(&'a self) -> &'a HashMap<String, String>
pub fn original_strings<'a>(&'a self) -> &'a HashMap<String, String>
Returns a hashmap mapping the original strings (as used by lformat!
)
to escaped strings. Only contains strings that are different and
must thus be handled.
Sourcepub fn add_messages_from_file<P: AsRef<Path>>(&mut self, file: P) -> Result<()>
pub fn add_messages_from_file<P: AsRef<Path>>(&mut self, file: P) -> Result<()>
Add all the messages contained in a source file
Sourcepub fn add_messages_from_dir<P: AsRef<Path>>(&mut self, dir: P) -> Result<()>
pub fn add_messages_from_dir<P: AsRef<Path>>(&mut self, dir: P) -> Result<()>
Add messages from all .rs
files contained in a directory
(walks through subdirectories)
Sourcepub fn generate_pot_file(&self) -> String
pub fn generate_pot_file(&self) -> String
Generate a pot-like file from the strings extracted from all files (if any)
Sourcepub fn write_pot_file(&mut self, file: &str) -> Result<()>
pub fn write_pot_file(&mut self, file: &str) -> Result<()>
Write a pot-like file to specified location