lingo_lib

Crates.iolingo_lib
lib.rslingo_lib
version0.2.2
sourcesrc
created_at2022-12-20 02:33:31.007181
updated_at2023-08-09 21:19:03.56796
descriptionInternationalise a Rust program and translate strings quickly and simply. Make any software open to multiple languages
homepage
repositoryhttps://github.com/antoninhrlt/lingo
max_upload_size
id741899
size36,870
Anтo (antoninhrlt)

documentation

README

lingo

Internationalise a Rust program and translate strings quickly and simply. Make any software open to multiple languages

Installation

In your "Cargo.toml" file :

[dependencies]
lingo_lib = "*"

Check the current version on crates.io.

Unfortunately, the crate "lingo" already exists in crates.io. Waiting to get the name, be careful to not import "lingo" but "lingo_lib".

How to use

let mut lingo = lingo![
    (
        "<identifier>", 
        strings![
            s!("<locale>", "<translation>"),
            s!("<locale>", "<translation>")
            // ...
        ]
    )
    // ...
];

// If not set, it's the operating system locale that will be used.
lingo.set_context_locale(/* app locale */);
// If not set, it's `locale!("en")`.
lingo.set_default_locale(/* locale */);

println!("{}", lingo.string("<identifier>").unwrap());
  • Locale

    locale!("en"); // English (no country code)
    locale!("en_US"); // English (United States)
    
    Locale::from_string("en_US", '_'); // English (United States)
    Locale::from_string("en-US", '-'); // English (United States)
    
    // English (no country code)
    Locale(Language::new(LanguageCode::en), CountryCode::None);
    // English (United States) 
    Locale(Language::new(LanguageCode::en), CountryCode::US);
    

Example

A French application using lingo.

struct MyFrenchApp {
    lingo: Lingo,
}

impl MyFrenchApp {
    pub fn new() -> Self {
        Self { 
            lingo: Self::init_lingo()
        }
    }

    pub fn run(&self) {
        println!("{}", self.lingo.string("welcome").unwrap());
    } 
}

impl LingoApp for MyFrenchApp {
    fn init_lingo() -> Lingo {
        let mut lingo = lingo![
            (
                "welcome", 
                strings![
                    s!("fr", "bienvenue sur l'app !"),
                    s!("en", "welcome to the app!")
                    // ...
                ]
            )
            // ...
        ];

        lingo.set_context_locale(locale!("fr_FR"));
        lingo
    }

    fn lingo(&self) -> &Lingo {
        &self.lingo
    }
}

fn main() {
    let app = MyFrenchApp::new();
    
    println!("{}", app.lingo().string("welcome").unwrap());
    
    app.run();
}
bienvenue sur l'app !
bienvenue sur l'app !
Imports for the example
use lingo_lib::{ lingo, locale, strings, s };
use lingo_lib::{ Lingo, LingoApp };
use lingo_lib::locales::Locale;
Commit count: 32

cargo fmt