v0.6.0
v0.6.0
I'm glad to announce the release of the long awaited v0.6.0.
This release add the support for leptos v0.8.x but also brings a lot of changes, this release note will list them but for more details take alook at the updated book.
Codegen rework
In older versions the codegen was done by the load_locales!() macro, worked fine but a couple of issues came with it, so it is now deprecated and users are recommanded to migrate to the new codegen: leptos_i18n_build.
Initially made for datagen of ICU4X datas, it is now also use to generate the i18n module in build scripts.
Here a basic setup:
# Cargo.toml
[features]
csr = ["leptos_i18n_build/csr"] # same for hydrate / ssr
[build-dependencies]
leptos_i18n_build = "0.6"use leptos_i18n_build::{Config, TranslationsInfos};
use std::error::Error;
use std::path::PathBuf;
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=Cargo.toml");
let i18n_mod_directory = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("i18n");
// `Config` represent the old "[package.metadata.leptos-i18n]" in `Cargo.toml`
let cfg = Config::new("en")?.add_locale("fr")?;
let translations_infos = TranslationsInfos::parse(cfg)?;
// Emits the diagnostics (errors and warnings)
// Instead of stopping at the first error, now tries to parse everything and report as much errors and warnings.
// Also prevent the codegen itself from failing and create dummies so you don't have errors at every `t*!` callsites.
// Warnings are now actual build scripts warnings instead of annoying `#[deprecated(note ="WARNING"]`.
translations_infos.emit_diagnostics();
// Cleaner file dependencies declaration, the one needed by the `load_locales!` macro was wacky
translations_infos.rerun_if_locales_changed();
// Also, it generate an actual file, no more macro expansion that looks awfull.
translations_infos.generate_i18n_module(i18n_mod_directory)?;
Ok(())
}New fallback resolution
If a locale is'nt the default and have no base locale, now find a "less specific" locale for missing keys.
For exemple "fr-FR" will inherit missing keys from "fr" even if "en" is the default.
You can overwrite this behavior by explicitly setting the base locale to the default one.
Deprecated ranges
Coming with v0.6.0, ranges are now deprecacted, users must migrate to plurals.
TOML support
Self explanatory, you can write translations in TOML. (thanks @sabify)
Self-closed components
You can now now interpolate components with no children:
{
"key": "before<br/>after"
}t!(i18n, key, <br/> = || view! { <br /> })Or
t!(i18n, key, <br/> = <br />)(thanks @sabify)
Custom formatters
You can inject parsers for formatters to the translations parser for custom value formatters, see the appendix for it in the book.
Component attributes in translations
You can now declare component attributes directly in the translations:
{
"key": "before<b id={{ id }} foo=\"bar\">middle</b>after"
}t!(i18n, key, id = "my_id", <b> = <b attr:bar="foo" />) // parsed attributes get passed to `<b>` alsongside user defined onesOr
let b = |children: ChildrenFn, attr: Vec<AnyAttribute>| view!{ <b {..attr} >{children}</b> }
t!(i18n, key, id = "my_id", <b>)How to migrate from v0.5
A project coming from v0.5.11 will get errors for unknown features, and deprecations warnings for the load_locales! macro and ranges (if you use them).
To fix all that, simply add leptos_i18n_build as a dev dependency and create a build.rs file at the root of the package, and paste this code:
use leptos_i18n_build::{Config, TranslationsInfos};
use std::error::Error;
use std::path::PathBuf;
fn main() -> Result<(), Box<dyn Error>> {
println!("cargo::rerun-if-changed=build.rs");
println!("cargo::rerun-if-changed=Cargo.toml");
let i18n_mod_directory = PathBuf::from(std::env::var_os("OUT_DIR").unwrap()).join("i18n");
let cfg = Config::new("en")?.add_locale("fr")?;
let translations_infos = TranslationsInfos::parse(cfg)?;
translations_infos.emit_diagnostics();
translations_infos.rerun_if_locales_changed();
translations_infos.generate_i18n_module(i18n_mod_directory)?;
Ok(())
}And replace the load_locales!() by
include!(concat!(env!("OUT_DIR"), "/i18n/mod.rs"));Now to migrate the config ("[package.metadata.leptos-i18n]") with the Config builder:
// The only required field is the default locale, others can be omitted.
let cfg = Config::new("en")? // default locale goes here
.add_locale("fr")?; // update your locales here
.add_locales(["it", "de"])? // or like that
.add_namespaces(["home", "common"])? // decribe your namespaces here
.locales_path("./path/to/locales") // custom locales path
.translations_uri("i18n/{locale}.json") // only needed in dynamic load CSR
.extend_locale("fr", "it")? // describe inheritances here (here makes "fr" inherit from "it")
// also be carefull to extend a locale *after* adding them, or else an error will be thrown
.parse_options(options); // options for the parsing (explained just after)So now you can remove the "[package.metadata.leptos-i18n]" part from your Cargo.toml.
Now the last errors: unknown features:
- show_keys_only
- interpolate_display
- suppress_key_warnings
- json_files
- json5_files
- yaml_files
- track_locale_files (completly removed, now use
translations_infos.rerun_if_locales_changed();)
You can simply enable them (appart from track_locale_files) with the ParseOptions builder:
use leptos_i18n_build::ParseOptions;
let options = ParseOptions::default()
.file_format(FileFormat::Yaml) // file format feature
.suppress_key_warnings(true) //
.interpolate_display(true)
.show_keys_only(true);And you should be good to go !
Special thanks for @sabify for the amazing work!
What's Changed
- Sync with main branch by @Baptistemontan in #216
- ICU4x v2.0 by @sabify in #211
- chore: upgrade to
leptos 0.8.0by @sabify in #219 - Rework errors by @Baptistemontan in #226
- Allow codegen from build.rs instead of a macro by @Baptistemontan in #227
- Deprecate
load_localesmacro by @Baptistemontan in #228 - Remove some features flags in favor of Options in the build.rs file by @Baptistemontan in #229
- Update docs for v0.6 by @Baptistemontan in #230
- v0.6.0-rc.1 by @Baptistemontan in #231
- Future proof options by @Baptistemontan in #232
- Rearrange workspace by @Baptistemontan in #233
- fix some clippy errors by @Baptistemontan in #235
- docs: updated icu4x
2.0.0-beta*to2.0.0by @sabify in #236 - fix: re-arrange workspace by @sabify in #240
- fallback to base lang if exist by @Baptistemontan in #237
- Rework warnings by @Baptistemontan in #238
- Update playwright by @Baptistemontan in #250
- update to latest leptos version by @Baptistemontan in #249
- do not sort matched locales by specificity by @cocoliliace in #251
- chore: update deps by @sabify in #239
- codegen options by @Baptistemontan in #254
- deprecate ranges by @Baptistemontan in #255
- feat: support toml file format by @sabify in #257
- chore: migrate to rust edition 2024 by @sabify in #258
- revert the render effect in context init by @Baptistemontan in #256
- fix: correctly serialize dynamic load translation data by @sabify in #259
- feat: support self-closed tags/components by @sabify in #262
- add
emit_diagnosticsfor all examples and test suits by @Baptistemontan in #263 - Allow custom formatters by @Baptistemontan in #260
- Component attributes parsing by @Baptistemontan in #264
- v0.6 cleanup by @Baptistemontan in #265
- Better to string impl for components attributes variable by @Baptistemontan in #266
- Refactor errors by @Baptistemontan in #267
- Set config in build script by @Baptistemontan in #268
- [WIP] v0.6 by @Baptistemontan in #217
New Contributors
- @cocoliliace made their first contribution in #251
Full Changelog: v0.5.11...v0.6.0