pub struct Config {
pub path: String,
pub language: String,
}
Expand description
Localization config object.
Use Config::new()
to create config objects instead of using this struct.
§Parameters
path
: The directory containing the translation files. The directory is relative to the path the executable was executed from.language
: The language to translate to.
§Returns
A new Config
object with the specified path and language.
§Examples
localizer_rs::Config {
path: "path".to_owned(),
language: "language".to_owned()
};
Fields§
§path: String
The directory containing the translation files. The directory is relative to the path the executable was executed from.
language: String
The language to translate to.
Implementations§
Source§impl Config
impl Config
Sourcepub fn new(path: &str, language: &str) -> Config
pub fn new(path: &str, language: &str) -> Config
Creates a new config object.
§Parameters
path
: The directory containing the translation files. The directory is relative to the path the executable was executed from.language
: The language to translate to.
§Returns
A new Config
object with the specified path and language.
§Panics
Panics if the Path provided is invalid.
§Examples
localizer_rs::Config::new("examples/translations", "language");
§See also
Examples found in repository?
examples/main.rs (line 4)
3fn main() {
4 let config: localizer_rs::Config = localizer_rs::Config::new("translations", "en");
5
6 println!(
7 "{:}",
8 localizer_rs::t!(
9 config,
10 "error",
11 "details" = "Something went wrong when trying to do stuff"
12 )
13 );
14 println!(
15 "{:}",
16 localizer_rs::t!(config, "success", "balance" = "$10", "user" = "John Doe")
17 );
18
19 println!("{:}", localizer_rs::t!(config, "all"));
20}
Sourcepub fn set_path(&mut self, str_path: &str) -> &Config
pub fn set_path(&mut self, str_path: &str) -> &Config
Sets the path for the config object.
§Parameters
self
: The config object. This must be mutable.str_path
: The directory containing the translation files. The directory is relative to the path the executable was executed from.
§Returns
The modified Config
object with the specified path.
§Panics
Panics if the Path provided is invalid.
§Examples
config.set_path("examples");
§See also
Sourcepub fn set_language(&mut self, language: &str) -> &Config
pub fn set_language(&mut self, language: &str) -> &Config
Sourcepub fn translate(&self, key: &str, arguments: Vec<(&str, &str)>) -> String
pub fn translate(&self, key: &str, arguments: Vec<(&str, &str)>) -> String
Translates the specified key in the language specified in the config.
§Parameters
self
: The config object.key
: The key to translate to.arguments
: The arguments to replace.
§Returns
A String
containing the translated value.
§Raises
This method throws an exception and exits if
- The translation file could not be found
- The translation file could not be opened
- The translation file could not be parsed
- The parsed json could not be converted to a json value
- The converted json could not be indexed
§Examples
config.translate("test", vec![]);
§See also
Trait Implementations§
Source§impl Ord for Config
impl Ord for Config
Source§impl PartialOrd for Config
impl PartialOrd for Config
impl Eq for Config
impl StructuralPartialEq for Config
Auto Trait Implementations§
impl Freeze for Config
impl RefUnwindSafe for Config
impl Send for Config
impl Sync for Config
impl Unpin for Config
impl UnwindSafe for Config
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more