diff options
| author | Rubén Beltrán del Río <jj@r.bdr.sh> | 2026-01-17 00:36:49 +0100 |
|---|---|---|
| committer | Rubén Beltrán del Río <jj@r.bdr.sh> | 2026-01-17 02:43:21 +0100 |
| commit | 17898fbabde35ab346c133114e78614e707c0eca (patch) | |
| tree | 90f0fd36211c036a507a0af092aac3c5fb8e7ac8 /src/i18n.rs | |
| parent | 90b12c34debaa4aa816d4e6862cdb4e39c7181e4 (diff) | |
Add localization
Diffstat (limited to 'src/i18n.rs')
| -rw-r--r-- | src/i18n.rs | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/src/i18n.rs b/src/i18n.rs new file mode 100644 index 0000000..1c574e0 --- /dev/null +++ b/src/i18n.rs @@ -0,0 +1,61 @@ +// Map, wardley map editor for linux +// Copyright (C) 2026 Rubén Beltrán del Río + +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. + +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. + +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see <http://www.gnu.org/licenses/>. + +//! Internationalization support using gettext. + +use gettextrs::{bindtextdomain, setlocale, textdomain, LocaleCategory}; + +/// The gettext domain for the application. +pub const GETTEXT_DOMAIN: &str = "systems.tranquil.Map"; + +/// Initializes the localization system. +pub fn init() { + setlocale(LocaleCategory::LcAll, ""); + + // Try standard locations for locale files + let locale_dirs = [ + // Development: local po directory + concat!(env!("CARGO_MANIFEST_DIR"), "/po"), + // Flatpak + "/app/share/locale", + // System-wide installation + "/usr/share/locale", + "/usr/local/share/locale", + ]; + + for dir in &locale_dirs { + if std::path::Path::new(dir).exists() { + let _ = bindtextdomain(GETTEXT_DOMAIN, *dir); + break; + } + } + + textdomain(GETTEXT_DOMAIN).expect("Failed to set gettext domain"); +} + +/// Macro for translating strings at runtime. +/// +/// Usage: +/// ```ignore +/// use crate::i18n::tr; +/// let translated = tr!("Hello, world!"); +/// ``` +#[macro_export] +macro_rules! tr { + ($msgid:expr) => { + gettextrs::gettext($msgid) + }; +} |