1
// This file is part of hnefatafl-copenhagen.
2
//
3
// hnefatafl-copenhagen is free software: you can redistribute it and/or modify
4
// it under the terms of the GNU Affero General Public License as published by
5
// the Free Software Foundation, either version 3 of the License, or
6
// (at your option) any later version.
7
//
8
// hnefatafl-copenhagen is distributed in the hope that it will be useful,
9
// but WITHOUT ANY WARRANTY; without even the implied warranty of
10
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
// GNU Affero General Public License for more details.
12
//
13
// You should have received a copy of the GNU Affero General Public License
14
// along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
//
16
// SPDX-License-Identifier: AGPL-3.0-or-later
17
// SPDX-FileCopyrightText: 2025 David Campbell <david@hnefatafl.org>
18

            
19
use std::fmt;
20

            
21
use serde::{Deserialize, Serialize};
22

            
23
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
24
pub enum Locale {
25
    #[default]
26
    English,
27
    Chinese,
28
    Spanish,
29
    Arabic,
30
    Indonesian,
31
    PortugueseBr,
32
    PortuguesePt,
33
    French,
34
    Japanese,
35
    Russian,
36
    German,
37
    Icelandic,
38
    IcelandicRunic,
39
    Swedish,
40
    Korean,
41
}
42

            
43
impl Locale {
44
    #[must_use]
45
    pub fn txt(self) -> String {
46
        match self {
47
            Self::English => "en-US".to_string(),
48
            Self::Chinese => "zh-CN".to_string(),
49
            Self::Spanish => "es".to_string(),
50
            Self::Arabic => "ar".to_string(),
51
            Self::Indonesian => "id".to_string(),
52
            Self::PortugueseBr => "pt-BR".to_string(),
53
            Self::PortuguesePt => "pt-PT".to_string(),
54
            Self::French => "fr".to_string(),
55
            Self::Japanese => "ja".to_string(),
56
            Self::Russian => "ru".to_string(),
57
            Self::German => "de".to_string(),
58
            Self::Icelandic => "is-IS".to_string(),
59
            Self::IcelandicRunic => "is-RU".to_string(),
60
            Self::Swedish => "sv-SE".to_string(),
61
            Self::Korean => "ko".to_string(),
62
        }
63
    }
64
}
65

            
66
impl TryFrom<&str> for Locale {
67
    type Error = anyhow::Error;
68

            
69
    fn try_from(locale: &str) -> anyhow::Result<Self> {
70
        let locale: String = locale.to_lowercase().chars().take(2).collect();
71
        match locale.as_str() {
72
            "en" => Ok(Self::English),
73
            "zh" => Ok(Self::Chinese),
74
            "es" => Ok(Self::Spanish),
75
            "ar" => Ok(Self::Arabic),
76
            "id" => Ok(Self::Indonesian),
77
            "pt" => Ok(Self::PortugueseBr),
78
            "fr" => Ok(Self::French),
79
            "ja" => Ok(Self::Japanese),
80
            "ru" => Ok(Self::Russian),
81
            "de" => Ok(Self::German),
82
            "is" => Ok(Self::Icelandic),
83
            "sv" => Ok(Self::Swedish),
84
            "ko" => Ok(Self::Korean),
85
            _ => Err(anyhow::Error::msg("can't find a locale")),
86
        }
87
    }
88
}
89

            
90
impl fmt::Display for Locale {
91
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92
        match self {
93
            Self::English => write!(f, "English (United States)"),
94
            Self::Chinese => write!(f, "中文 (中国)"),
95
            Self::Spanish => write!(f, "Español"),
96
            Self::Arabic => write!(f, "العربية"),
97
            Self::Indonesian => write!(f, "bahasa Indonesia"),
98
            Self::PortugueseBr => write!(f, "Português (Brasil)"),
99
            Self::PortuguesePt => write!(f, "Português (Portugal)"),
100
            Self::French => write!(f, "Français"),
101
            Self::Japanese => write!(f, "日本人"),
102
            Self::Russian => write!(f, "Русский"),
103
            Self::German => write!(f, "Deutsch"),
104
            Self::Icelandic => write!(f, "Íslenska"),
105
            Self::IcelandicRunic => write!(f, "ᛇᛋᛚᛂᚿᛋᚴᛁ ᚱᚤᛐᚢᚱᛁᚿᚿ (Íslenska Rúturinn)"),
106
            Self::Swedish => write!(f, "Svenska"),
107
            Self::Korean => write!(f, "한국인"),
108
        }
109
    }
110
}