Skip to content

Commit

Permalink
remove onec_cell and cleanup code
Browse files Browse the repository at this point in the history
  • Loading branch information
ho-229 committed Aug 29, 2023
1 parent bd14377 commit c5b7e28
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 24 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@ exclude = [".github", "cli", "example", "proc-macros", "trans-support"]

[dependencies]
mod_use = "0.2"
once_cell = "1.17.1"
sys-locale = "0.3.0"
sys-locale = "0.3.1"
oxilangtag = "0.1"
phf = { version = "0.11.1", features = ["macros"] }
phf = { version = "0.11.2", features = ["macros"] }
dynfmt = { version = "0.1.5", default-features = false, features = ["curly"] }

r18-proc-macros = { path = "./proc-macros", version = "0.4.2" }
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

`r18` is a crate intends to simplify the internationalisation of Rust projects.

`MSRV >= 1.70.0 (stable)`

## Usage

Add `r18` as your project dependency:
Expand Down
2 changes: 1 addition & 1 deletion cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ fn update(root: impl AsRef<Path>, rm_unused: bool) -> Result<()> {
println!("{} untranslated text(s) were found", todo.len());
println!("Writing to TODO.{}", file_name);

translations.extend(todo.clone().into_iter());
translations.extend(todo.clone());
}

if !todo.is_empty() || (rm_unused && !unused.is_empty()) {
Expand Down
11 changes: 6 additions & 5 deletions proc-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,10 +141,10 @@ fn generate_primary(locales: &LocaleModel) -> proc_macro2::TokenStream {

quote! {
#[doc(hidden)]
const #code: r18::Locale = r18::Locale {
const #code: ::r18::Locale = ::r18::Locale {
name: #name,
translate: {
use r18::phf;
use ::r18::phf;
phf::phf_map! {
#( #translation ),*
}
Expand Down Expand Up @@ -199,7 +199,7 @@ fn generate_lang_matches(
quote! { (#primary, _) => Some(&#ident) , }
});

exact_matches.chain([fallback_match].into_iter()).collect()
exact_matches.chain([fallback_match]).collect()
} else {
quote!()
}
Expand All @@ -214,9 +214,10 @@ fn generate_helpers(config: &Config, model: &TranslationModel) -> proc_macro2::T
quote! {
#[doc(hidden)]
pub(crate) fn set_locale(locale: impl AsRef<str>) {
*r18::CURRENT_LOCALE
*::r18::CURRENT_LOCALE
.get_or_init(|| ::std::sync::Mutex::new(None))
.lock()
.unwrap() = match r18::LanguageTag::parse_and_normalize(locale.as_ref()) {
.unwrap() = match ::r18::LanguageTag::parse_and_normalize(locale.as_ref()) {
Ok(lang) => {
match (lang.primary_language(), lang.region()) {
#matches
Expand Down
18 changes: 10 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
//! `r18` is a crate intends to simplify the internationalisation of Rust
//! projects.
//!
//! `MSRV >= 1.70.0 (stable)`
//!
//! ## Usage
//!
//! Add `r18` as your project dependency:
Expand Down Expand Up @@ -71,13 +73,11 @@
//! }
//! ```

use std::sync::Mutex;
use std::sync::{Mutex, OnceLock};

#[doc(hidden)]
pub use dynfmt::{Format, SimpleCurlyFormat};
#[doc(hidden)]
pub use once_cell::sync::Lazy;
#[doc(hidden)]
pub use oxilangtag::{LanguageTag, LanguageTagParseError};
#[doc(hidden)]
pub use phf;
Expand All @@ -94,17 +94,19 @@ pub struct Locale {
}

#[doc(hidden)]
pub static CURRENT_LOCALE: Lazy<Mutex<Option<&'static Locale>>> = Lazy::new(|| Mutex::new(None));
pub static CURRENT_LOCALE: OnceLock<Mutex<Option<&'static Locale>>> = OnceLock::new();

/// Translate content with the locale setting and given prefix.
///
/// We recommend using [`tr!`] instead of [`translate`] for translate your
/// content.
pub fn translate(prefix: impl AsRef<str>, content: &str) -> &str {
let locale = CURRENT_LOCALE.lock().unwrap();
let locale = match *locale {
Some(l) => l,
None => return content,
let locale = CURRENT_LOCALE
.get_or_init(|| Mutex::new(None))
.lock()
.unwrap();
let Some(locale) = *locale else {
return content;
};

match locale
Expand Down
15 changes: 8 additions & 7 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
#[macro_export]
macro_rules! tr {
($content:expr) => {
r18::translate("", $content)
::r18::translate("", $content)
};
($content:expr, $($arg:expr),+) => {{
use r18::{Format, SimpleCurlyFormat};
SimpleCurlyFormat.format(r18::translate("", $content), &[$($arg),+])
use ::r18::{Format, SimpleCurlyFormat};
SimpleCurlyFormat.format(::r18::translate("", $content), &[$($arg),+])
.unwrap_or_default()
}};
([$prefix:expr] $content:expr) => {
r18::translate($prefix, $content)
::r18::translate($prefix, $content)
};
([$prefix:expr] $content:expr, $($arg:expr),+) => {{
use r18::{Format, SimpleCurlyFormat};
SimpleCurlyFormat.format(r18::translate($prefix, $content), &[$($arg),+])
use ::r18::{Format, SimpleCurlyFormat};
SimpleCurlyFormat.format(::r18::translate($prefix, $content), &[$($arg),+])
.unwrap_or_default()
}};
}
Expand Down Expand Up @@ -68,6 +68,7 @@ macro_rules! set_locale {
macro_rules! locale {
() => {
$crate::CURRENT_LOCALE
.get_or_init(|| ::std::sync::Mutex::new(None))
.lock()
.unwrap()
.as_ref()
Expand All @@ -79,6 +80,6 @@ macro_rules! locale {
#[macro_export]
macro_rules! auto_detect {
() => {
r18::get_locale().map(|l| r18::set_locale!(l))
::r18::get_locale().map(|l| ::r18::set_locale!(l))
};
}

0 comments on commit c5b7e28

Please sign in to comment.