-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
init: draft version of i18n dependency
- Loading branch information
Showing
8 changed files
with
169 additions
and
6 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
// Folder-specific settings | ||
// | ||
// For a full list of overridable settings, and general information on folder-specific settings, | ||
// see the documentation: https://zed.dev/docs/configuring-zed#settings-files | ||
{ | ||
"lsp": { | ||
"rust-analyzer": { | ||
"initialization_options": { | ||
"check": { | ||
"command": "clippy" | ||
} | ||
}, | ||
"cargo": { | ||
"features": "all" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
lorem = "ipsum shit" | ||
dablayob = "and is something shit" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
use super::language::Language; | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
struct I18n { | ||
users: HashMap<i64, String>, | ||
pub languages: HashMap<String, Language>, | ||
} | ||
|
||
impl I18n { | ||
pub fn new() -> Self { | ||
I18n::default() | ||
} | ||
|
||
pub fn load_translations(&mut self) -> () { | ||
let locales = std::fs::read_dir("locales").unwrap(); | ||
|
||
for locale in locales { | ||
let safe = locale.unwrap(); | ||
|
||
let path = safe.path(); | ||
let file = path.to_str().unwrap(); | ||
|
||
let mut language = Language::new(); | ||
language.parse_translation(file); | ||
|
||
let name = file.split("/").last().unwrap().split(".").next().unwrap(); | ||
|
||
self.languages.insert(name.to_owned(), language); | ||
} | ||
} | ||
|
||
pub fn add_user(&mut self, user_id: i64, language: &str) { | ||
self.users.insert(user_id, language.to_string()); | ||
} | ||
|
||
pub fn get_translation(&self, language: &str, key: &str) -> Option<&String> { | ||
self.languages.get(language)?.get_translation(key) | ||
} | ||
|
||
pub fn get_user_translation(&self, user_id: i64, key: &str) -> Option<&String> { | ||
let language = self.users.get(&user_id)?; | ||
self.get_translation(language, key) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_i18n_init() { | ||
// Create instance and load locales | ||
let mut i18n = I18n::new(); | ||
i18n.load_translations(); | ||
|
||
// Get a translation | ||
let translation = i18n.get_translation("en", "lorem"); | ||
|
||
// Test | ||
assert_eq!(translation, Some(&"ipsum shit".to_string())); | ||
} | ||
|
||
#[test] | ||
fn test_user() { | ||
// Create instance and load locales | ||
let mut i18n = I18n::new(); | ||
i18n.load_translations(); | ||
|
||
// | ||
i18n.add_user(123123123123, "en"); | ||
|
||
// Let's try sending request | ||
let translation = i18n.get_user_translation(123123123123, "lorem"); | ||
|
||
// Test | ||
assert_eq!(translation, Some(&"ipsum shit".to_string())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
use std::collections::HashMap; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
pub struct Language { | ||
translations: HashMap<String, String>, | ||
} | ||
|
||
impl Language { | ||
pub fn new() -> Self { | ||
Language::default() | ||
} | ||
|
||
pub fn parse_translation(&mut self, file: &str) { | ||
let content = std::fs::read_to_string(file).unwrap(); | ||
|
||
let translations: HashMap<String, String> = toml::from_str(&content).unwrap(); | ||
|
||
for (key, value) in translations { | ||
self.add_translation(key.to_string(), value.as_str().to_string()); | ||
} | ||
} | ||
|
||
pub fn add_translation(&mut self, key: String, value: String) { | ||
self.translations.insert(key, value); | ||
} | ||
|
||
pub fn get_translation(&self, key: &str) -> Option<&String> { | ||
self.translations.get(key) | ||
} | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn test_language_kv() { | ||
let mut language = Language::new(); | ||
language.add_translation("hello".to_string(), "Hello, World!".to_string()); | ||
|
||
let translation = language.get_translation("hello"); | ||
|
||
// Test | ||
assert_eq!(translation, Some(&"Hello, World!".to_string())); | ||
} | ||
|
||
#[test] | ||
fn test_language_read_file() { | ||
let mut language = Language::new(); | ||
language.parse_translation("./locales/en.toml"); | ||
|
||
let translation = language.get_translation("lorem"); | ||
|
||
// Test | ||
assert_eq!(translation, Some(&"ipsum shit".to_string())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
pub mod i18n; | ||
pub mod language; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
pub mod i18n; | ||
pub mod keyboard; | ||
pub mod timer; | ||
pub mod topic; | ||
|