aboutsummaryrefslogtreecommitdiff
path: root/src/i18n.rs
blob: 03c30c35bf2132001b3a7885c2ec8e823e2455a8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// 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 crate::info;
use gettextrs::{LocaleCategory, bindtextdomain, setlocale, textdomain};

/// 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(info::GETTEXT_DOMAIN, *dir);
            break;
        }
    }

    textdomain(info::GETTEXT_DOMAIN).ok();
}

/// 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)
    };
}