Struct Extractor

Source
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

Source

pub fn new() -> Extractor

Create a new, empty extractor

Examples found in repository?
examples/extract.rs (line 6)
5fn main() {
6    let mut extractor = Extractor::new();
7    extractor.add_messages_from_dir("src/").unwrap();
8    println!("{}", extractor.generate_pot_file());
9}
More examples
Hide additional examples
examples/macrogen.rs (line 24)
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}
Source

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.

Source

pub fn add_messages_from_file<P: AsRef<Path>>(&mut self, file: P) -> Result<()>

Add all the messages contained in a source file

Source

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)

Examples found in repository?
examples/extract.rs (line 7)
5fn main() {
6    let mut extractor = Extractor::new();
7    extractor.add_messages_from_dir("src/").unwrap();
8    println!("{}", extractor.generate_pot_file());
9}
Source

pub fn generate_pot_file(&self) -> String

Generate a pot-like file from the strings extracted from all files (if any)

Examples found in repository?
examples/extract.rs (line 8)
5fn main() {
6    let mut extractor = Extractor::new();
7    extractor.add_messages_from_dir("src/").unwrap();
8    println!("{}", extractor.generate_pot_file());
9}
Source

pub fn write_pot_file(&mut self, file: &str) -> Result<()>

Write a pot-like file to specified location

Trait Implementations§

Source§

impl Clone for Extractor

Source§

fn clone(&self) -> Extractor

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Extractor

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.