Skip to content

v0.6.0

Choose a tag to compare

@Baptistemontan Baptistemontan released this 06 Jan 19:17
· 151 commits to master since this release

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 ones

Or

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

New Contributors

Full Changelog: v0.5.11...v0.6.0