Converted tabs to spaces and fixed tests
This commit is contained in:
parent
49a2b13657
commit
66363879c8
8
.github/README.md
vendored
8
.github/README.md
vendored
@ -1,5 +1,5 @@
|
||||
<h1 align="center">
|
||||
localizer-rs
|
||||
localizer-rs
|
||||
</h1>
|
||||
<h3 align="center">
|
||||
Localizer helps localize (translate) your rust applications using json files.
|
||||
@ -67,7 +67,7 @@ With the following `en.json` file.
|
||||
```json
|
||||
|
||||
{
|
||||
"error": "{{color.red}}{{bold}}Error:{{end}} Something went wrong: {{details}}."
|
||||
"error": "{{color.red}}{{bold}}Error:{{end}} Something went wrong: {{details}}."
|
||||
}
|
||||
|
||||
```
|
||||
@ -79,9 +79,9 @@ And the following rust code.
|
||||
use localizer_rs;
|
||||
|
||||
fn main() {
|
||||
let config: localizer_rs::Config = localizer_rs::Config::new("translations", "en");
|
||||
let config: localizer_rs::Config = localizer_rs::Config::new("translations", "en");
|
||||
|
||||
println!("{:}", config.t("error", vec![("details", "Path not found")]));
|
||||
println!("{:}", config.t("error", vec![("details", "Path not found")]));
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
@ -1,10 +1,10 @@
|
||||
use localizer_rs;
|
||||
|
||||
fn main() {
|
||||
let config: localizer_rs::Config = localizer_rs::Config::new("translations", "en");
|
||||
let config: localizer_rs::Config = localizer_rs::Config::new("translations", "en");
|
||||
|
||||
println!("{:}", config.t("error", vec![("details", "Something went wrong when trying to do stuff")]));
|
||||
println!("{:}", config.t("success", vec![("balance", "$10"), ("user", "John Doe")]));
|
||||
println!("{:}", config.t("error", vec![("details", "Something went wrong when trying to do stuff")]));
|
||||
println!("{:}", config.t("success", vec![("balance", "$10"), ("user", "John Doe")]));
|
||||
|
||||
println!("{:}", config.t("all", vec![]));
|
||||
}
|
||||
println!("{:}", config.t("all", vec![]));
|
||||
}
|
||||
|
||||
145
src/lib.rs
145
src/lib.rs
@ -1,3 +1,4 @@
|
||||
#![doc = ".github/README.md"]
|
||||
// localizer-rs
|
||||
// Version: 1.0.0
|
||||
|
||||
@ -61,8 +62,8 @@ use serde_json;
|
||||
/// ```
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
|
||||
pub struct Config {
|
||||
pub path: String,
|
||||
pub language: String
|
||||
pub path: String,
|
||||
pub language: String
|
||||
}
|
||||
|
||||
|
||||
@ -96,16 +97,16 @@ impl Config {
|
||||
/// # See also
|
||||
///
|
||||
/// - [`Config`]
|
||||
pub fn new(path: &str, language: &str) -> Config {
|
||||
let mut config: Config = Config {
|
||||
path: "".to_string(),
|
||||
language: "".to_string()
|
||||
}.to_owned();
|
||||
config = config.set_language(language).to_owned();
|
||||
config = config.set_path(path).to_owned();
|
||||
pub fn new(path: &str, language: &str) -> Config {
|
||||
let mut config: Config = Config {
|
||||
path: "".to_string(),
|
||||
language: "".to_string()
|
||||
}.to_owned();
|
||||
config = config.set_language(language).to_owned();
|
||||
config = config.set_path(path).to_owned();
|
||||
|
||||
return config;
|
||||
}
|
||||
return config;
|
||||
}
|
||||
|
||||
/// Sets the path for the config object.
|
||||
///
|
||||
@ -126,32 +127,32 @@ impl Config {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use localizer_rs;
|
||||
/// # let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "language");
|
||||
/// # let mut config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "language");
|
||||
/// config.set_path("examples");
|
||||
/// ```
|
||||
///
|
||||
/// # See also
|
||||
///
|
||||
/// - [`Config`]
|
||||
pub fn set_path(&mut self, str_path: &str) -> &Config {
|
||||
let path: &Path = Path::new(str_path);
|
||||
pub fn set_path(&mut self, str_path: &str) -> &Config {
|
||||
let path: &Path = Path::new(str_path);
|
||||
|
||||
match path.try_exists() {
|
||||
Ok(value) => {
|
||||
if !value {
|
||||
eprintln!("Translation path {:?} does not exist", str_path);
|
||||
exit(1);
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("Can't open translation path {:?}: {}", str_path, error);
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
match path.try_exists() {
|
||||
Ok(value) => {
|
||||
if !value {
|
||||
eprintln!("Translation path {:?} does not exist", str_path);
|
||||
exit(1);
|
||||
}
|
||||
},
|
||||
Err(error) => {
|
||||
eprintln!("Can't open translation path {:?}: {}", str_path, error);
|
||||
exit(2);
|
||||
}
|
||||
}
|
||||
|
||||
self.path = String::from(path.to_owned().to_str().expect("Expected valid path"));
|
||||
return self;
|
||||
}
|
||||
self.path = String::from(path.to_owned().to_str().expect("Expected valid path"));
|
||||
return self;
|
||||
}
|
||||
|
||||
/// Sets the language for the config object.
|
||||
///
|
||||
@ -168,17 +169,17 @@ impl Config {
|
||||
///
|
||||
/// ```rust
|
||||
/// # use localizer_rs;
|
||||
/// # let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "language");
|
||||
/// # let mut config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "language");
|
||||
/// config.set_language("en");
|
||||
/// ```
|
||||
///
|
||||
/// # See also
|
||||
///
|
||||
/// - [`Config`]
|
||||
pub fn set_language(&mut self, language: &str) -> &Config {
|
||||
self.language = language.to_string();
|
||||
return self;
|
||||
}
|
||||
pub fn set_language(&mut self, language: &str) -> &Config {
|
||||
self.language = language.to_string();
|
||||
return self;
|
||||
}
|
||||
|
||||
/// Translates the specified key in the language specified in the config.
|
||||
///
|
||||
@ -203,51 +204,51 @@ impl Config {
|
||||
/// # See also
|
||||
///
|
||||
/// - [`Config`]
|
||||
pub fn t(&self, key: &str, arguments: Vec<(&str, &str)>) -> String {
|
||||
return self.translate::<serde_json::Value>(key, arguments);
|
||||
}
|
||||
pub fn t(&self, key: &str, arguments: Vec<(&str, &str)>) -> String {
|
||||
return self.translate::<serde_json::Value>(key, arguments);
|
||||
}
|
||||
|
||||
fn translate<T>(&self, key: &str, mut arguments: Vec<(&str, &str)>) -> String
|
||||
where
|
||||
T: serde::Serialize + for<'de> serde::Deserialize<'de>
|
||||
{
|
||||
let mut colors: Vec<(&str, &str)> = vec![
|
||||
// Formatting codes
|
||||
("end", "\x1b[0m"), ("bold", "\x1b[1m"), ("italic", "\x1b[3m"), ("underline", "\x1b[4m"), ("overline", "\x1b[53m"),
|
||||
fn translate<T>(&self, key: &str, mut arguments: Vec<(&str, &str)>) -> String
|
||||
where
|
||||
T: serde::Serialize + for<'de> serde::Deserialize<'de>
|
||||
{
|
||||
let mut colors: Vec<(&str, &str)> = vec![
|
||||
// Formatting codes
|
||||
("end", "\x1b[0m"), ("bold", "\x1b[1m"), ("italic", "\x1b[3m"), ("underline", "\x1b[4m"), ("overline", "\x1b[53m"),
|
||||
|
||||
// Foreground colors
|
||||
("color.black", "\x1b[30m"), ("color.red", "\x1b[31m"), ("color.green", "\x1b[32m"), ("color.yellow", "\x1b[33m"),
|
||||
("color.blue", "\x1b[34m"), ("color.magenta", "\x1b[35m"), ("color.cyan", "\x1b[36m"), ("color.white", "\x1b[37m"),
|
||||
// Foreground colors
|
||||
("color.black", "\x1b[30m"), ("color.red", "\x1b[31m"), ("color.green", "\x1b[32m"), ("color.yellow", "\x1b[33m"),
|
||||
("color.blue", "\x1b[34m"), ("color.magenta", "\x1b[35m"), ("color.cyan", "\x1b[36m"), ("color.white", "\x1b[37m"),
|
||||
|
||||
// Bright foreground colors
|
||||
("color.bright_black", "\x1b[90m"), ("color.bright_red", "\x1b[91m"), ("color.bright_green", "\x1b[92m"),
|
||||
("color.bright_yellow", "\x1b[93m"), ("color.bright_blue", "\x1b[94m"), ("color.bright_magenta", "\x1b[95m"),
|
||||
("color.bright_cyan", "\x1b[96m"), ("color.bright_white", "\x1b[97m"),
|
||||
// Bright foreground colors
|
||||
("color.bright_black", "\x1b[90m"), ("color.bright_red", "\x1b[91m"), ("color.bright_green", "\x1b[92m"),
|
||||
("color.bright_yellow", "\x1b[93m"), ("color.bright_blue", "\x1b[94m"), ("color.bright_magenta", "\x1b[95m"),
|
||||
("color.bright_cyan", "\x1b[96m"), ("color.bright_white", "\x1b[97m"),
|
||||
|
||||
// Background colors
|
||||
("back.black", "\x1b[40m"), ("back.red", "\x1b[41m"), ("back.green", "\x1b[42m"), ("back.yellow", "\x1b[43m"),
|
||||
("back.blue", "\x1b[44m"), ("back.magenta", "\x1b[45m"), ("back.cyan", "\x1b[46m"), ("back.white", "\x1b[47m"),
|
||||
// Background colors
|
||||
("back.black", "\x1b[40m"), ("back.red", "\x1b[41m"), ("back.green", "\x1b[42m"), ("back.yellow", "\x1b[43m"),
|
||||
("back.blue", "\x1b[44m"), ("back.magenta", "\x1b[45m"), ("back.cyan", "\x1b[46m"), ("back.white", "\x1b[47m"),
|
||||
|
||||
// Bright background colors
|
||||
("back.bright_black", "\x1b[100m"), ("back.bright_red", "\x1b[101m"), ("back.bright_green", "\x1b[102m"),
|
||||
("back.bright_yellow", "\x1b[103m"), ("back.bright_blue", "\x1b[104m"), ("back.bright_magenta", "\x1b[105m"),
|
||||
("back.bright_cyan", "\x1b[106m"), ("back.bright_white", "\x1b[107m"),
|
||||
];
|
||||
arguments.append(&mut colors);
|
||||
// Bright background colors
|
||||
("back.bright_black", "\x1b[100m"), ("back.bright_red", "\x1b[101m"), ("back.bright_green", "\x1b[102m"),
|
||||
("back.bright_yellow", "\x1b[103m"), ("back.bright_blue", "\x1b[104m"), ("back.bright_magenta", "\x1b[105m"),
|
||||
("back.bright_cyan", "\x1b[106m"), ("back.bright_white", "\x1b[107m"),
|
||||
];
|
||||
arguments.append(&mut colors);
|
||||
|
||||
let file: File = File::open(Path::new(format!("./{}/{}.json", &self.path, &self.language).as_str())).unwrap();
|
||||
let reader: BufReader<File> = BufReader::new(file);
|
||||
let file: File = File::open(Path::new(format!("./{}/{}.json", &self.path, &self.language).as_str())).unwrap();
|
||||
let reader: BufReader<File> = BufReader::new(file);
|
||||
|
||||
let json: serde_json::Value = serde_json::to_value::<T>(serde_json::from_reader::<BufReader<File>, T>(reader).unwrap()).unwrap().to_owned();
|
||||
let mut result: String = match json[key].as_str() {
|
||||
Some(value) => value.to_string(),
|
||||
None => "".to_string()
|
||||
};
|
||||
let json: serde_json::Value = serde_json::to_value::<T>(serde_json::from_reader::<BufReader<File>, T>(reader).unwrap()).unwrap().to_owned();
|
||||
let mut result: String = match json[key].as_str() {
|
||||
Some(value) => value.to_string(),
|
||||
None => "".to_string()
|
||||
};
|
||||
|
||||
for (key, value) in arguments {
|
||||
result = result.replace(("{{".to_owned() + key + "}}").as_str(), value);
|
||||
}
|
||||
for (key, value) in arguments {
|
||||
result = result.replace(("{{".to_owned() + key + "}}").as_str(), value);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user