#gettext #proc-macro

i18n-format

A simple macro to allow formatting with gettext while keeping xgettext to work

3 releases (breaking)

0.3.0 Feb 9, 2025
0.2.0 Jan 22, 2023
0.1.0 Jan 21, 2023

#305 in Internationalization (i18n)

Download history 826/week @ 2024-10-28 1327/week @ 2024-11-04 668/week @ 2024-11-11 519/week @ 2024-11-18 679/week @ 2024-11-25 257/week @ 2024-12-02 728/week @ 2024-12-09 975/week @ 2024-12-16 632/week @ 2024-12-23 437/week @ 2024-12-30 139/week @ 2025-01-06 725/week @ 2025-01-13 770/week @ 2025-01-20 695/week @ 2025-01-27 549/week @ 2025-02-03 553/week @ 2025-02-10

2,581 downloads per month

MIT license

7KB

i18n-format

xgettext is used to extract strings from source files. Unfortunately it doesn't support Rust. The patch just got merged in Jan 2025. Then it will take a release and distros to ship it.

Fortunately it almost work with Rust. You can specify the keyword gettext or whatever alias you use (I usually use i18n).

The gettextrs create doesn't offer a formatting version as the formatting macro gettext! was removed.

These twi crates provide a way to have formatting macros with gettext and support the extraction, as the macro syntax is not compatible with xgettext that extract strings for .po files. Specify i18n_fmt and i18n_nfmt as keywords for calls to xgettext, and then write your formatting gettext call like this:

use i18n_format::i18n_fmt;

let number = 1;
let s = i18n_fmt! {
    i18n_fmt("This is number {}, make it so !", number)
};

is equivalent to

use gettextrs::gettext;

let number = 1;
let s = format!(gettext!("This is number {}, make it so !"), number);

The latter wouldn't even compile as format! expect a literal.

And the string will be extracted.

i18n_fmt is just a placeholder in the block for i18n_fmt!, and it will be replaced by another macro that will call gettext!. To call ngettext! just use i18n_nfmt as a placeholder inside the macro.

Error

The underlying formatx::formatx! macro will panic in some case. When it returns an error, i18n_format will output the error instead of the string.

Meson support

The original use of this crate is with gtk-rs apps built with meson. If you use the regular gtk-rust-template, internationalization support is mostly setup. The po/meson.build file should contain a section like this:

i18n.gettext(gettext_package, preset: 'glib')

Just add to the args the following to the i18n.gettext function:

  args [
    '--keyword=i18n_fmt',
    '--keyword=i18n_nfmt:1,2'
  ]

If args already exist, just add the items to the list.

Future

This version of the crate has been reworked to work around a breakage in gettextrs. See Issue 1

It turns out that two thing might happen soon:

  1. gettext will have native Rust support.
  2. gettextrs might offer the formatting macros.

When this happens I hope these crates will be obsolete.

License

This crate is licensed under the MIT license.

Author: Hubert Figuière hub@figuiere.net

Dependencies

~14MB
~67K SLoC