#gettext #internationalization #proc-macro

macro i18n-format

A simple proc-macro to allow gettext! and xgettext to work together

2 unstable releases

0.2.0 Jan 22, 2023
0.1.0 Jan 21, 2023

#604 in Procedural macros

Download history 231/week @ 2024-07-21 371/week @ 2024-07-28 265/week @ 2024-08-04 230/week @ 2024-08-11 195/week @ 2024-08-18 260/week @ 2024-08-25 679/week @ 2024-09-01 419/week @ 2024-09-08 408/week @ 2024-09-15 311/week @ 2024-09-22 739/week @ 2024-09-29 296/week @ 2024-10-06 957/week @ 2024-10-13 1196/week @ 2024-10-20 828/week @ 2024-10-27 1327/week @ 2024-11-03

4,313 downloads per month

MIT license

6KB

i18n-format

xgettext is used to extract strings from source files. Unfortunately it doesn't support Rust. The patch has been stuck in review since 2019.

Fortunately it almost work with Rust. You can specify the keyword gettext or whatever alias you use (I usually use i18n), but to use the formatting version gettext! it doesn't work because of the !.

This crate provide wrapper macros to use gettextrs::gettext! and gettextrs::ngettext! in a way that allow xgettext to find strings for .po files as it doesn't support a keyword with a !. 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 = gettext!("This is number {}, make it so !", number);

But the string will be extracted.

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

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.

License

This crate is licensed under the MIT license.

Author: Hubert Figuière hub@figuiere.net

Dependencies

~14MB
~65K SLoC