Compare commits

..

15 Commits
v1.1.1 ... main

Author SHA1 Message Date
ElBe
21d4ba85e9
Merge pull request #5 from ElBe-Development/dependabot/github_actions/actions/setup-python-5
Bump actions/setup-python from 4 to 5
2025-01-24 12:53:05 +01:00
ElBe
1addf7b72a
Merge pull request #11 from ElBe-Development/dependabot/pip/pre-commit-4.1.0
Bump pre-commit from 3.5.0 to 4.1.0
2025-01-23 21:28:44 +01:00
ElBe
7251484eaa
Merge pull request #7 from ElBe-Development/dependabot/github_actions/actions/dependency-review-action-4
Bump actions/dependency-review-action from 3 to 4
2025-01-23 21:26:25 +01:00
dependabot[bot]
1a749cb6ab
Bump pre-commit from 3.5.0 to 4.1.0
Bumps [pre-commit](https://github.com/pre-commit/pre-commit) from 3.5.0 to 4.1.0.
- [Release notes](https://github.com/pre-commit/pre-commit/releases)
- [Changelog](https://github.com/pre-commit/pre-commit/blob/main/CHANGELOG.md)
- [Commits](https://github.com/pre-commit/pre-commit/compare/v3.5.0...v4.1.0)

---
updated-dependencies:
- dependency-name: pre-commit
  dependency-type: direct:development
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2025-01-23 20:26:23 +00:00
ElBe
1ac8dcf6a5
Merge pull request #8 from ElBe-Development/dependabot/github_actions/codecov/codecov-action-4
Bump codecov/codecov-action from 3 to 4
2025-01-23 21:26:03 +01:00
ElBe
3d649300c7
Merge pull request #4 from ElBe-Development/dependabot/github_actions/actions/labeler-5
Bump actions/labeler from 4 to 5
2025-01-23 21:24:35 +01:00
dependabot[bot]
b9a8a54232
Bump codecov/codecov-action from 3 to 4
Bumps [codecov/codecov-action](https://github.com/codecov/codecov-action) from 3 to 4.
- [Release notes](https://github.com/codecov/codecov-action/releases)
- [Changelog](https://github.com/codecov/codecov-action/blob/main/CHANGELOG.md)
- [Commits](https://github.com/codecov/codecov-action/compare/v3...v4)

---
updated-dependencies:
- dependency-name: codecov/codecov-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-02-01 13:04:46 +00:00
dependabot[bot]
a79508348c
Bump actions/dependency-review-action from 3 to 4
Bumps [actions/dependency-review-action](https://github.com/actions/dependency-review-action) from 3 to 4.
- [Release notes](https://github.com/actions/dependency-review-action/releases)
- [Commits](https://github.com/actions/dependency-review-action/compare/v3...v4)

---
updated-dependencies:
- dependency-name: actions/dependency-review-action
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2024-01-19 12:33:50 +00:00
dependabot[bot]
152f973edd
Bump actions/setup-python from 4 to 5
Bumps [actions/setup-python](https://github.com/actions/setup-python) from 4 to 5.
- [Release notes](https://github.com/actions/setup-python/releases)
- [Commits](https://github.com/actions/setup-python/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/setup-python
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-06 12:54:42 +00:00
dependabot[bot]
39228823bd
Bump actions/labeler from 4 to 5
Bumps [actions/labeler](https://github.com/actions/labeler) from 4 to 5.
- [Release notes](https://github.com/actions/labeler/releases)
- [Commits](https://github.com/actions/labeler/compare/v4...v5)

---
updated-dependencies:
- dependency-name: actions/labeler
  dependency-type: direct:production
  update-type: version-update:semver-major
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-12-05 12:41:40 +00:00
ElBe
57bb936433
Bump pre-commit from 3.4.0 to 3.5.0
Bump pre-commit from 3.4.0 to 3.5.0
2023-10-23 20:18:53 +02:00
dependabot[bot]
f2caa5c3b1
Bump pre-commit from 3.4.0 to 3.5.0
Bumps [pre-commit](https://github.com/pre-commit/pre-commit) from 3.4.0 to 3.5.0.
- [Release notes](https://github.com/pre-commit/pre-commit/releases)
- [Changelog](https://github.com/pre-commit/pre-commit/blob/main/CHANGELOG.md)
- [Commits](https://github.com/pre-commit/pre-commit/compare/v3.4.0...v3.5.0)

---
updated-dependencies:
- dependency-name: pre-commit
  dependency-type: direct:development
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-10-16 12:44:34 +00:00
ElBe
163e31bb45 Added t!() macro 2023-10-06 07:39:56 +02:00
ElBe
a8fd02158b Fixed yaml syntax error 2023-09-15 15:13:42 +02:00
ElBe
a85b394b1e Added one single missing bang 2023-09-15 15:10:31 +02:00
12 changed files with 164 additions and 40 deletions

18
.github/README.md vendored
View File

@ -39,25 +39,19 @@ To use localizer-rs, you need a directory (eg. `translations`) with your transla
1. Import the localizer-rs crate: 1. Import the localizer-rs crate:
```rust,ignore ```rust,ignore
use localizer_rs; use localizer_rs;
``` ```
2. Create a new config object: 2. Create a new config object:
```rust,ignore ```rust,ignore
let config = localizer_rs::Config::new("translations", "en");
let config = localizer_rs::Config::new("DIRECTORY NAME", "LANGUAGE NAME");
``` ```
3. Translate your text: 3. Translate your text:
```rust,ignore ```rust,ignore
localizer_rs::t!(config, "key", "placeholder" ="value");
config.t("key", vec!["placeholder", "value"]);
``` ```
## Example ## Example
@ -65,33 +59,27 @@ To use localizer-rs, you need a directory (eg. `translations`) with your transla
With the following `en.json` file. With the following `en.json` file.
```json ```json
{ {
"error": "{{color.red}}{{bold}}Error:{{end}} Something went wrong: {{details}}." "error": "{{color.red}}{{bold}}Error:{{end}} Something went wrong: {{details}}."
} }
``` ```
And the following rust code. And the following rust code.
```rust,ignore ```rust,ignore
use localizer_rs; use localizer_rs;
fn main() { 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!("{:}", localizer_rs::t!(config, "error", "details" = "Path not found"));
} }
``` ```
You will get the following output: You will get the following output:
```bash ```bash
Error: Something went wrong: Path not found. Error: Something went wrong: Path not found.
``` ```
Where `Error:` is red and bold. Where `Error:` is red and bold.

1
.github/SECURITY.md vendored
View File

@ -7,6 +7,7 @@
| `v1.0.0` | :white_check_mark: | | `v1.0.0` | :white_check_mark: |
| `v1.1.0` | :white_check_mark: | | `v1.1.0` | :white_check_mark: |
| `v1.1.1` | :white_check_mark: | | `v1.1.1` | :white_check_mark: |
| `v1.2.0` | :white_check_mark: |
## Reporting a Vulnerability ## Reporting a Vulnerability

3
.github/errors.md vendored Normal file
View File

@ -0,0 +1,3 @@
# errors module
Module for dealing with errors.

View File

@ -6,7 +6,9 @@ on:
jobs: jobs:
build: build:
runs-on: ubuntu-latest
steps:
- name: Upload coverage reports to Codecov - name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3 uses: codecov/codecov-action@v4
env: env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }} CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}

View File

@ -18,7 +18,7 @@ jobs:
steps: steps:
- uses: actions/checkout@v4 - uses: actions/checkout@v4
- name: Set up Python ${{ matrix.python-version }} - name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4 uses: actions/setup-python@v5
with: with:
python-version: ${{ matrix.python-version }} python-version: ${{ matrix.python-version }}
- name: Install dependencies from txt files - name: Install dependencies from txt files
@ -43,4 +43,4 @@ jobs:
if: ${{ github.event_name != 'push' }} if: ${{ github.event_name != 'push' }}
- name: Dependency Review - name: Dependency Review
if: ${{ github.event_name != 'push' }} if: ${{ github.event_name != 'push' }}
uses: actions/dependency-review-action@v3 uses: actions/dependency-review-action@v4

View File

@ -20,6 +20,6 @@ jobs:
pull-requests: write pull-requests: write
steps: steps:
- uses: actions/labeler@v4 - uses: actions/labeler@v5
with: with:
repo-token: "${{ secrets.GITHUB_TOKEN }}" repo-token: "${{ secrets.GITHUB_TOKEN }}"

View File

@ -1,7 +1,7 @@
[package] [package]
name = "localizer-rs" name = "localizer-rs"
description = "Localizer helps localize (translate) your rust applications using json files." description = "Localizer helps localize (translate) your rust applications using json files."
version = "1.1.1" version = "1.2.0"
authors = [ authors = [
"ElBe-Plaq <elbe.dev.plaq@gmail.com>" "ElBe-Plaq <elbe.dev.plaq@gmail.com>"
] ]

View File

@ -1 +1 @@
pre-commit==3.4.0; python_version>='3.8' pre-commit==4.1.0; python_version>='3.8'

View File

@ -5,15 +5,16 @@ fn main() {
println!( println!(
"{:}", "{:}",
config.t( localizer_rs::t!(
config,
"error", "error",
vec![("details", "Something went wrong when trying to do stuff")] "details" = "Something went wrong when trying to do stuff"
) )
); );
println!( println!(
"{:}", "{:}",
config.t("success", vec![("balance", "$10"), ("user", "John Doe")]) localizer_rs::t!(config, "success", "balance" = "$10", "user" = "John Doe")
); );
println!("{:}", config.t("all", vec![])); println!("{:}", localizer_rs::t!(config, "all"));
} }

View File

@ -1,3 +1,4 @@
#![doc = include_str!("../.github/errors.md")]
// localizer-rs errors // localizer-rs errors
// Version: 1.1.1 // Version: 1.1.1
@ -66,7 +67,31 @@ pub struct Error {
pub exit_code: i32, pub exit_code: i32,
} }
/// Display implementation for the error object.
impl fmt::Display for Error { impl fmt::Display for Error {
/// Format implementation for the error object.
///
/// # Parameters
///
/// - `self`: The error object.
/// - `f`: The [`fmt::Formatter`] to use.
///
/// # Returns
///
/// A [`fmt::Result`] containing the formatted error message.
///
/// # Examples
///
/// ```rust
/// # use localizer_rs;
/// # let error = localizer_rs::errors::Error::new("name", "description", 1);
/// println!("{}", error);
/// ```
///
/// # See also
///
/// - [`fmt::Display`]
/// - [`Error`]
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "\x1b[31;1m{}\x1b[0m: {}", self.name, self.description) write!(f, "\x1b[31;1m{}\x1b[0m: {}", self.name, self.description)
} }

View File

@ -1,6 +1,6 @@
#[doc = include_str!("../.github/README.md")] #![doc = include_str!("../.github/README.md")]
// localizer-rs // localizer-rs
// Version: 1.1.1 // Version: 1.2.0
// Copyright (c) 2023-present ElBe Development. // Copyright (c) 2023-present ElBe Development.
@ -159,8 +159,7 @@ impl Config {
} }
} }
Err(_error) => { Err(_error) => {
let error: errors::Error = let error: errors::Error = errors::Error::new("OS Error", "Could not open path", 2);
errors::Error::new("OS Error", "Could not open path", 2);
error.raise(format!("Path: {:?}\nDetails: {}", str_path, _error).as_str()); error.raise(format!("Path: {:?}\nDetails: {}", str_path, _error).as_str());
} }
} }
@ -226,15 +225,49 @@ impl Config {
/// ///
/// # See also /// # See also
/// ///
/// - [`t!()`]
/// - [`Config`] /// - [`Config`]
pub fn t(&self, key: &str, arguments: Vec<(&str, &str)>) -> String { pub fn t(&self, key: &str, arguments: Vec<(&str, &str)>) -> String {
return self.translate::<serde_json::Value>(key, arguments); return self.translate(key, arguments);
} }
fn translate<T>(&self, key: &str, mut arguments: Vec<(&str, &str)>) -> String /// Translates the specified key in the language specified in the config.
where ///
T: serde::Serialize + for<'de> serde::Deserialize<'de>, /// # 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
///
/// ```rust
/// # use localizer_rs;
/// # let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
/// config.translate("test", vec![]);
/// ```
///
/// # See also
///
/// - [`t!()`]
/// - [`Config`]
/// - [`Config::t()`]
/// - [`serde_json`]
pub fn translate(&self, key: &str, mut arguments: Vec<(&str, &str)>) -> String {
let mut colors: Vec<(&str, &str)> = vec![ let mut colors: Vec<(&str, &str)> = vec![
// Formatting codes // Formatting codes
("end", "\x1b[0m"), ("end", "\x1b[0m"),
@ -305,8 +338,8 @@ impl Config {
}; };
let reader: BufReader<File> = BufReader::new(file); let reader: BufReader<File> = BufReader::new(file);
let json: serde_json::Value = match serde_json::to_value::<T>( let json: serde_json::Value = match serde_json::to_value::<serde_json::Value>(
match serde_json::from_reader::<BufReader<File>, T>(reader) { match serde_json::from_reader::<BufReader<File>, serde_json::Value>(reader) {
Ok(value) => value, Ok(value) => value,
Err(_error) => { Err(_error) => {
let error: errors::Error = errors::Error::new( let error: errors::Error = errors::Error::new(
@ -366,3 +399,50 @@ impl Config {
return result; return result;
} }
} }
/// Translates the specified key in the language specified in the config.
///
/// # Parameters
///
/// - `config`: The config object.
/// - `key`: The key to translate to.
/// - `arguments`: Optional parameter. The arguments to replace. Has to be of type `"name" = "value"`.
///
/// # Returns
///
/// A `String` containing the translated value.
///
/// # Examples
///
/// ```rust
/// # use localizer_rs;
/// # let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
/// localizer_rs::t!(config, "test");
/// localizer_rs::t!(config, "test", "variable" = "content");
/// ```
///
/// # See also
///
/// - [`Config`]
/// - [`Config::t()`]
#[macro_export]
macro_rules! t {
($config:expr, $key:expr) => {
{
$config.t($key, vec![])
}
};
($config:expr, $key:expr, $($argument_name:literal = $argument_value:literal),* $(,)?) => {
{
let mut arguments: Vec<(&str, &str)> = vec![];
$(
arguments.push(($argument_name, $argument_value));
)*
$config.t($key, arguments)
}
};
}

View File

@ -1,5 +1,5 @@
// localizer-rs tests // localizer-rs tests
// Version: 1.1.1 // Version: 1.2.0
// Copyright (c) 2023-present ElBe Development. // Copyright (c) 2023-present ElBe Development.
@ -80,6 +80,18 @@ mod tests {
#[test] #[test]
fn test_translate() { fn test_translate() {
let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
let translation: String =
config.translate("error", vec![("details", "Something went wrong")]);
assert_eq!(
translation.as_str(),
"\x1b[31m\x1b[1mError:\x1b[0m Something went wrong"
);
}
#[test]
fn test_translate_t() {
let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en"); let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
let translation: String = config.t("error", vec![("details", "Something went wrong")]); let translation: String = config.t("error", vec![("details", "Something went wrong")]);
@ -88,4 +100,16 @@ mod tests {
"\x1b[31m\x1b[1mError:\x1b[0m Something went wrong" "\x1b[31m\x1b[1mError:\x1b[0m Something went wrong"
); );
} }
#[test]
fn test_translate_macro() {
let config: localizer_rs::Config = localizer_rs::Config::new("examples/translations", "en");
let translation: String =
localizer_rs::t!(config, "error", "details" = "Something went wrong");
assert_eq!(
translation.as_str(),
"\x1b[31m\x1b[1mError:\x1b[0m Something went wrong"
);
}
} }