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
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//! An i18n handler for carapax
#![warn(missing_docs)]

use carapax::{context::Context, core::types::Update, Handler};
use std::{collections::HashMap, sync::Arc};

pub use gettext::Catalog;

/// An i18n handler for carapax
#[derive(Debug, Clone)]
pub struct I18nHandler<R> {
    resolver: R,
    default_translator: Translator,
    translators: HashMap<String, Translator>,
}

impl<R> I18nHandler<R> {
    /// Creates a new I18nHandler
    ///
    /// # Arguments
    ///
    /// * resolver - A locale resolver
    /// * default_translator - Default translator used when translator not found for locale or locale is missing
    pub fn new(resolver: R, default_translator: Translator) -> Self {
        Self {
            resolver,
            default_translator,
            translators: HashMap::new(),
        }
    }

    /// Adds a translator
    pub fn add_translator(mut self, translator: Translator) -> Self {
        self.translators.insert(translator.locale.clone(), translator);
        self
    }
}

impl<R> Handler for I18nHandler<R>
where
    R: LocaleResolver,
{
    type Input = Update;
    type Output = ();

    fn handle(&self, context: &mut Context, update: Self::Input) -> Self::Output {
        let locale = self.resolver.resolve(&update);
        let translator = locale
            .as_ref()
            .and_then(|locale| self.translators.get(locale).cloned())
            .unwrap_or_else(|| self.default_translator.clone());
        context.set(translator);
    }
}

/// An i18n translator uses `gettext` crate
#[derive(Debug, Clone)]
pub struct Translator {
    locale: String,
    catalog: Arc<Catalog>,
}

impl Translator {
    /// Creates a new translator
    ///
    /// # Arguments
    ///
    /// * locale - A locale string
    /// * catalog - A gettext message catalog
    pub fn new<S: Into<String>>(locale: S, catalog: Catalog) -> Self {
        Self {
            locale: locale.into(),
            catalog: Arc::new(catalog),
        }
    }

    /// Translates a given string
    pub fn translate<K: Into<TranslationKey>>(&self, key: K) -> String {
        let key = key.into();
        let singular = &key.id;
        match (&key.kind, &key.context) {
            (TranslationKind::Singular, None) => self.catalog.gettext(singular),
            (TranslationKind::Plural { id, n }, None) => self.catalog.ngettext(singular, id, *n),
            (TranslationKind::Singular, Some(context)) => self.catalog.pgettext(context, singular),
            (TranslationKind::Plural { id, n }, Some(context)) => self.catalog.npgettext(context, singular, id, *n),
        }
        .to_string()
    }

    /// Returns a locale
    pub fn locale(&self) -> &str {
        &self.locale
    }
}

/// A locale resolver trait
pub trait LocaleResolver {
    /// Returns a locale string from given update
    fn resolve(&self, update: &Update) -> Option<String>;
}

/// Resolves a locale from user's language code
#[derive(Debug, Clone, Copy)]
pub struct UserLocaleResolver;

impl LocaleResolver for UserLocaleResolver {
    fn resolve(&self, update: &Update) -> Option<String> {
        update.get_user().and_then(|user| user.language_code.clone())
    }
}

/// A key for translation
#[derive(Debug, Clone)]
pub struct TranslationKey {
    id: String,
    kind: TranslationKind,
    context: Option<String>,
}

impl TranslationKey {
    /// Creates a new key for a singular
    pub fn singular<S: Into<String>>(id: S) -> Self {
        Self {
            id: id.into(),
            kind: TranslationKind::Singular,
            context: None,
        }
    }

    /// Creates a new key for a plural
    pub fn plural<S, P>(id: S, plural_id: P, n: u64) -> Self
    where
        S: Into<String>,
        P: Into<String>,
    {
        Self {
            id: id.into(),
            kind: TranslationKind::Plural {
                id: plural_id.into(),
                n,
            },
            context: None,
        }
    }

    /// Sets `gettext` context
    pub fn context<S: Into<String>>(mut self, context: S) -> Self {
        self.context = Some(context.into());
        self
    }
}

impl<S> From<S> for TranslationKey
where
    S: Into<String>,
{
    fn from(s: S) -> Self {
        TranslationKey::singular(s)
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum TranslationKind {
    Singular,
    Plural { id: String, n: u64 },
}

#[cfg(test)]
mod tests {
    use super::*;

    const EN: &[u8] = include_bytes!("../data/en.mo");
    const RU: &[u8] = include_bytes!("../data/ru.mo");

    #[test]
    fn test_handler() {
        let en = Catalog::parse(EN).unwrap();
        let en = Translator::new("en", en);

        let ru = Catalog::parse(RU).unwrap();
        let ru = Translator::new("ru", ru);

        let handler = I18nHandler::new(UserLocaleResolver, en).add_translator(ru);
        let mut context = Context::default();

        let en_update: serde_json::Value = serde_json::json!({
            "update_id": 1,
            "message": {
                "message_id": 1,
                "date": 0,
                "from": {
                    "id": 1,
                    "is_bot": false,
                    "first_name": "test",
                    "username": "username",
                },
                "chat": {
                    "id": 1,
                    "type": "private",
                    "first_name": "test",
                    "username": "username"
                },
                "text": "test i18n"
            }
        });
        let mut ru_update = en_update.clone();
        ru_update
            .get_mut("message")
            .unwrap()
            .get_mut("from")
            .unwrap()
            .as_object_mut()
            .unwrap()
            .insert("language_code".to_string(), serde_json::Value::from("ru"));
        let en_update: Update = serde_json::from_value(en_update).unwrap();
        let ru_update: Update = serde_json::from_value(ru_update).unwrap();

        for (key, value, update) in vec![
            (TranslationKey::singular("Apple"), "Apple", en_update.clone()),
            (TranslationKey::singular("Apple"), "Яблоко", ru_update.clone()),
            (
                TranslationKey::plural("{} apple", "{} apples", 2),
                "{} apples",
                en_update.clone(),
            ),
            (
                TranslationKey::plural("{} apple", "{} apples", 2),
                "{} яблока",
                ru_update.clone(),
            ),
            (
                TranslationKey::singular("This is context").context("context"),
                "This is context",
                en_update.clone(),
            ),
            (
                TranslationKey::singular("This is context").context("context"),
                "Это контекст",
                ru_update.clone(),
            ),
        ] {
            handler.handle(&mut context, update.clone());
            let translator = context.get::<Translator>();
            assert_eq!(translator.translate(key), value);
        }
    }
}