i18n-embed-impl-0.8.4/.cargo_vcs_info.json0000644000000001700000000000100136330ustar { "git": { "sha1": "b3a58bc1321c6c3c8e032026e79228385639d0c4" }, "path_in_vcs": "i18n-embed/i18n-embed-impl" }i18n-embed-impl-0.8.4/.gitignore000064400000000000000000000000451046102023000144140ustar 00000000000000/target **/*.rs.bk .vscode Cargo.locki18n-embed-impl-0.8.4/Cargo.toml0000644000000032230000000000100116330ustar # THIS FILE IS AUTOMATICALLY GENERATED BY CARGO # # When uploading crates to the registry Cargo will automatically # "normalize" Cargo.toml files for maximal compatibility # with all versions of Cargo and also rewrite `path` dependencies # to registry (e.g., crates.io) dependencies. # # If you are reading this file be aware that the original Cargo.toml # will likely look very different (and much more reasonable). # See Cargo.toml.orig for the original contents. [package] edition = "2018" name = "i18n-embed-impl" version = "0.8.4" authors = ["Luke Frisken "] build = false autobins = false autoexamples = false autotests = false autobenches = false description = "Macro implementations for i18n-embed" readme = "README.md" keywords = [ "build", "i18n", "gettext", "locale", "fluent", ] categories = [ "localization", "internationalization", ] license = "MIT" repository = "https://github.com/kellpossible/cargo-i18n/tree/master/i18n-embed" [lib] name = "i18n_embed_impl" path = "src/lib.rs" proc-macro = true [dependencies.find-crate] version = "0.6" optional = true [dependencies.i18n-config] version = "0.4.7" optional = true [dependencies.proc-macro2] version = "1.0" [dependencies.quote] version = "1.0" optional = true [dependencies.syn] version = "2.0" features = [ "derive", "proc-macro", "parsing", "printing", "extra-traits", ] [dev-dependencies.rust-embed] version = "8.0" [features] default = [] fluent-system = [ "i18n-config", "find-crate", "quote", ] gettext-system = [ "i18n-config", "find-crate", "quote", ] [badges.maintenance] status = "actively-developed" i18n-embed-impl-0.8.4/Cargo.toml.orig000064400000000000000000000017721046102023000153230ustar 00000000000000[package] authors = ["Luke Frisken "] description = "Macro implementations for i18n-embed" edition = "2018" keywords = ["build", "i18n", "gettext", "locale", "fluent"] categories = ["localization", "internationalization"] license = "MIT" name = "i18n-embed-impl" readme = "../README.md" repository = "https://github.com/kellpossible/cargo-i18n/tree/master/i18n-embed" version = "0.8.4" [badges] maintenance = { status = "actively-developed" } [lib] proc-macro = true [dependencies] find-crate = { workspace = true, optional = true } i18n-config = { workspace = true, optional = true } proc-macro2 = { workspace = true } quote = { workspace = true, optional = true } [dev-dependencies] rust-embed = { workspace = true } [dependencies.syn] workspace = true default-features = false features = ["derive", "proc-macro", "parsing", "printing", "extra-traits",] [features] default = [] gettext-system = ["i18n-config", "find-crate", "quote"] fluent-system = ["i18n-config", "find-crate", "quote"] i18n-embed-impl-0.8.4/LICENSE.txt000064400000000000000000000020601046102023000142460ustar 00000000000000Copyright 2020 Luke Frisken Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. © 2020 Luke Friskeni18n-embed-impl-0.8.4/README.md000064400000000000000000000124771046102023000137170ustar 00000000000000# i18n-embed [![crates.io badge](https://img.shields.io/crates/v/i18n-embed.svg)](https://crates.io/crates/i18n-embed) [![docs.rs badge](https://docs.rs/i18n-embed/badge.svg)](https://docs.rs/i18n-embed/) [![license badge](https://img.shields.io/github/license/kellpossible/cargo-i18n)](https://github.com/kellpossible/cargo-i18n/blob/master/i18n-embed/LICENSE.txt) [![github actions badge](https://github.com/kellpossible/cargo-i18n/workflows/Rust/badge.svg)](https://github.com/kellpossible/cargo-i18n/actions?query=workflow%3ARust) Traits and macros to conveniently embed localization assets into your application binary or library in order to localize it at runtime. Works in unison with [cargo-i18n](https://crates.io/crates/cargo_i18n). Currently this library depends on [rust-embed](https://crates.io/crates/rust-embed) to perform the actual embedding of the language files. This may change in the future to make the library more convenient to use. **[Changelog](https://github.com/kellpossible/cargo-i18n/blob/master/i18n-embed/CHANGELOG.md)** ## Optional Features The `i18n-embed` crate has the following optional Cargo features: + `fluent-system` + Enable support for the [fluent](https://www.projectfluent.org/) localization system via the `FluentLanguageLoader`. + `gettext-system` + Enable support for the [gettext](https://www.gnu.org/software/gettext/) localization system using the [tr macro](https://docs.rs/tr/0.1.3/tr/) and the [gettext crate](https://docs.rs/gettext/0.4.0/gettext/) via the `GettextLanguageLoader`. + `desktop-requester` + Enables a convenience implementation of `LanguageRequester` trait called `DesktopLanguageRequester` for the desktop platform (windows, mac, linux),which makes use of the [locale_config](https://crates.io/crates/locale_config) crate for resolving the current system locale. + `web-sys-requester` + Enables a convenience implementation of `LanguageRequester` trait called `WebLanguageRequester` which makes use of the [web-sys](https://crates.io/crates/web-sys) crate for resolving the language being requested by the user's web browser in a WASM context. ## Example The following is a minimal example for how localize your binary using this library using the [fluent](https://www.projectfluent.org/) localization system. First you need to compile `i18n-embed` in your `Cargo.toml` with the `fluent-system` and `desktop-requester` features enabled: ```toml [dependencies] i18n-embed = { version = "VERSION", features = ["fluent-system", "desktop-requester"]} rust-embed = "6" unic-langid = "0.9" ``` Set up a minimal `i18n.toml` in your crate root to use with `cargo-i18n` (see [cargo i18n](../README.md#configuration) for more information on the configuration file format): ```toml # (Required) The language identifier of the language used in the # source code for gettext system, and the primary fallback language # (for which all strings must be present) when using the fluent # system. fallback_language = "en-GB" # Use the fluent localization system. [fluent] # (Required) The path to the assets directory. # The paths inside the assets directory should be structured like so: # `assets_dir/{language}/{domain}.ftl` assets_dir = "i18n" ``` Next, you want to create your localization resources, per language fluent files. `language` needs to conform to the [Unicode Language Identifier](https://unicode.org/reports/tr35/tr35.html#Unicode_language_identifier) standard, and will be parsed via the [unic_langid crate](https://docs.rs/unic-langid/0.9.0/unic_langid/). The directory structure should look like so: ```txt my_crate/ Cargo.toml i18n.toml src/ i18n/ {language}/ {domain}.ftl ``` Then you can instantiate your language loader and language requester: ```rust use i18n_embed::{DesktopLanguageRequester, fluent::{ FluentLanguageLoader, fluent_language_loader }}; use rust_embed::RustEmbed; #[derive(RustEmbed)] #[folder = "i18n"] // path to the compiled localization resources struct Localizations; fn main() { let language_loader: FluentLanguageLoader = fluent_language_loader!(); // Use the language requester for the desktop platform (linux, windows, mac). // There is also a requester available for the web-sys WASM platform called // WebLanguageRequester, or you can implement your own. let requested_languages = DesktopLanguageRequester::requested_languages(); let _result = i18n_embed::select( &language_loader, &Localizations, &requested_languages); // continue on with your application } ``` To access localizations, you can use `FluentLanguageLoader`'s methods directly, or, for added compile-time checks/safety, you can use the [fl!() macro](https://crates.io/crates/i18n-embed-fl). Having an `i18n.toml` configuration file enables you to do the following: + Use the [cargo i18n](https://crates.io/crates/cargo-i18n) tool to perform validity checks (not yet implemented). + Integrate with a code-base using the `gettext` localization system. + Use the `fluent::fluent_language_loader!()` macro to pull the configuration in at compile time to create the `fluent::FluentLanguageLoader`. + Use the [fl!() macro](https://crates.io/crates/i18n-embed-fl) to have added compile-time safety when accessing messages. Example projects can be found in [examples](./examples). For more explained examples, see the [documentation for i18n-embed](https://docs.rs/i18n-embed/). i18n-embed-impl-0.8.4/src/lib.rs000064400000000000000000000123471046102023000143370ustar 00000000000000/// A procedural macro to create a new `GettextLanguageLoader` using /// the current crate's `i18n.toml` configuration, and domain. /// /// ⚠️ *This API requires the following crate features to be /// activated: `gettext-system`.* /// /// ## Example /// /// ```ignore /// use i18n_embed::gettext::{gettext_language_loader, GettextLanguageLoader}; /// let my_language_loader: GettextLanguageLoader = gettext_language_loader!(); /// ``` #[proc_macro] #[cfg(feature = "gettext-system")] pub fn gettext_language_loader(_: proc_macro::TokenStream) -> proc_macro::TokenStream { let manifest = find_crate::Manifest::new().expect("Error reading Cargo.toml"); let current_crate_package_name = { manifest.crate_package().map(|pkg| pkg.name).unwrap_or( std::env::var("CARGO_PKG_NAME").expect("Error fetching `CARGO_PKG_NAME` env"), ) }; // Special case for when this macro is invoked in i18n-embed tests/docs let i18n_embed_crate_name = if current_crate_package_name == "i18n_embed" { "i18n_embed".to_string() } else { manifest .find(|s| s == "i18n-embed") .expect("i18n-embed should be an active dependency in your Cargo.toml") .name }; let i18n_embed_crate_ident = syn::Ident::new(&i18n_embed_crate_name, proc_macro2::Span::call_site()); let config_file_path = i18n_config::locate_crate_paths() .unwrap_or_else(|error| { panic!( "gettext_language_loader!() is unable to locate i18n config file: {}", error ) }) .i18n_config_file; let config = i18n_config::I18nConfig::from_file(&config_file_path).unwrap_or_else(|err| { panic!( "gettext_language_loader!() had a problem reading i18n config file {0:?}: {1}", std::fs::canonicalize(&config_file_path).unwrap_or_else(|_| config_file_path.clone()), err ) }); if config.gettext.is_none() { panic!( "gettext_language_loader!() had a problem parsing i18n config file {0:?}: there is no `[gettext]` section", std::fs::canonicalize(&config_file_path).unwrap_or(config_file_path) ) } let fallback_language = syn::LitStr::new( &config.fallback_language.to_string(), proc_macro2::Span::call_site(), ); let gen = quote::quote! { #i18n_embed_crate_ident::gettext::GettextLanguageLoader::new( module_path!(), #fallback_language.parse().unwrap(), ) }; gen.into() } /// A procedural macro to create a new `FluentLanguageLoader` using /// the current crate's `i18n.toml` configuration, and domain. /// /// ⚠️ *This API requires the following crate features to be /// activated: `fluent-system`.* /// /// ## Example /// /// ```ignore /// use i18n_embed::fluent::{fluent_language_loader, FluentLanguageLoader}; /// let my_language_loader: FluentLanguageLoader = fluent_language_loader!(); /// ``` #[proc_macro] #[cfg(feature = "fluent-system")] pub fn fluent_language_loader(_: proc_macro::TokenStream) -> proc_macro::TokenStream { let manifest = find_crate::Manifest::new().expect("Error reading Cargo.toml"); let current_crate_package_name = manifest .crate_package() .map(|pkg| pkg.name) .unwrap_or(std::env::var("CARGO_PKG_NAME").expect("Error fetching `CARGO_PKG_NAME` env")); // Special case for when this macro is invoked in i18n-embed tests/docs let i18n_embed_crate_name = if current_crate_package_name == "i18n_embed" { "i18n_embed".to_string() } else { manifest .find(|s| s == "i18n-embed") .expect("i18n-embed should be an active dependency in your Cargo.toml") .name }; let i18n_embed_crate_ident = syn::Ident::new(&i18n_embed_crate_name, proc_macro2::Span::call_site()); let config_file_path = i18n_config::locate_crate_paths() .unwrap_or_else(|error| { panic!( "fluent_language_loader!() is unable to locate i18n config file: {}", error ) }) .i18n_config_file; let config = i18n_config::I18nConfig::from_file(&config_file_path).unwrap_or_else(|err| { panic!( "fluent_language_loader!() had a problem reading i18n config file {0:?}: {1}", std::fs::canonicalize(&config_file_path).unwrap_or_else(|_| config_file_path.clone()), err ) }); if config.fluent.is_none() { panic!( "fluent_language_loader!() had a problem parsing i18n config file {0:?}: there is no `[fluent]` section", std::fs::canonicalize(&config_file_path).unwrap_or(config_file_path) ) } let fallback_language = syn::LitStr::new( &config.fallback_language.to_string(), proc_macro2::Span::call_site(), ); let domain_str = config .fluent .and_then(|f| f.domain) .unwrap_or(current_crate_package_name); let domain = syn::LitStr::new(&domain_str, proc_macro2::Span::call_site()); let gen = quote::quote! { #i18n_embed_crate_ident::fluent::FluentLanguageLoader::new( #domain, #fallback_language.parse().unwrap(), ) }; gen.into() }