Struct Config

Source
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

Source

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}
Source

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
Source

pub fn set_language(&mut self, language: &str) -> &Config

Sets the language for the config object.

§Parameters
  • self: The config object. This must be mutable.
  • language: The language to translate to.
§Returns

The modified Config object with the specified language.

§Examples
config.set_language("en");
§See also
Source

pub fn t(&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.

§Examples
config.t("test", vec![]);
§See also
Source

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 Clone for Config

Source§

fn clone(&self) -> Config

Returns a duplicate 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 Config

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Ord for Config

Source§

fn cmp(&self, other: &Config) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Config

Source§

fn eq(&self, other: &Config) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Config

Source§

fn partial_cmp(&self, other: &Config) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Eq for Config

Source§

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> 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.