i18n_format/
lib.rs

1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: (c) 2025 Hubert Figuière
3
4#![doc = include_str!("../README.md")]
5
6pub use formatx::formatx;
7pub use gettextrs::{gettext, ngettext};
8pub use i18n_format_macro::i18n_fmt;
9
10/// Implement i18n_fmt. This is not meant to be called directly
11/// but rather by the proc-macro `i18n-fmt`.
12///
13/// # Panic
14///
15/// The output code will panic under the same condition as
16/// [`formatx::formatx!`]
17#[macro_export]
18macro_rules! i18n_fmt_impl {
19    ($template:expr) => {
20        format!("{}", $crate::gettext($template))
21    };
22    ($template:expr, $($values:tt)*) => {
23        format!("{}", $crate::formatx!($crate::gettext($template), $($values)*).unwrap_or_else(|err| err.message()))
24    };
25}
26
27/// Implement i18n_nfmt. This is not meant to be called directly
28/// but rather by the proc-macro `i18n-fmt`.
29///
30/// # Panic
31///
32/// The output code will panic under the same condition as
33/// [`formatx::formatx!`]
34#[macro_export]
35macro_rules! i18n_nfmt_impl {
36    ($templates:expr, $templatep:expr, $count:expr) => {
37        format!("{}", $crate::ngettext($templates, $templatep, $count))
38    };
39    ($templates:expr, $templatep:expr, $count:expr, $($values:tt)*) => {
40        format!("{}", $crate::formatx!($crate::ngettext($templates, $templatep, $count), $($values)*).unwrap_or_else(|err| err.message()))
41    };
42}