diff --git a/Cargo.toml b/Cargo.toml index 1df5e4a..a0f7de9 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" } diff --git a/README.md b/README.md index 3b2ed13..73b98a7 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/cli/src/main.rs b/cli/src/main.rs index c2f96ec..5ce8793 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -122,7 +122,7 @@ fn update(root: impl AsRef, 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()) { diff --git a/proc-macros/src/lib.rs b/proc-macros/src/lib.rs index 391e229..6aa3adf 100644 --- a/proc-macros/src/lib.rs +++ b/proc-macros/src/lib.rs @@ -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 ),* } @@ -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!() } @@ -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) { - *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 diff --git a/src/lib.rs b/src/lib.rs index d6bfa1c..a29e372 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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: @@ -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; @@ -94,17 +94,19 @@ pub struct Locale { } #[doc(hidden)] -pub static CURRENT_LOCALE: Lazy>> = Lazy::new(|| Mutex::new(None)); +pub static CURRENT_LOCALE: OnceLock>> = 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, 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 diff --git a/src/macros.rs b/src/macros.rs index d43e738..e8c560f 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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() }}; } @@ -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() @@ -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)) }; }