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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
//! An i18n utilities for carapax
#![warn(missing_docs)]

use carapax::{
    types::{CallbackQuery, ChosenInlineResult, InlineQuery, Message, PreCheckoutQuery, ShippingQuery, Update, User},
    Command,
};
use std::{
    collections::HashMap,
    convert::{TryFrom, TryInto},
    error::Error,
    fmt,
    sync::Arc,
};

pub use gettext::Catalog;

/// A store for translators
#[derive(Debug, Clone)]
pub struct TranslatorStore {
    default_translator: Translator,
    translators: HashMap<String, Translator>,
}

impl TranslatorStore {
    /// Creates a new store
    ///
    /// # Arguments
    ///
    /// * default_translator - Default translator used when translator not found for locale or locale is missing
    pub fn new(default_translator: Translator) -> Self {
        Self {
            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
    }

    /// Returns a translator for given locale
    ///
    /// If translator not found for locale,
    /// default translator will be returned.
    pub fn get_translator<L>(&self, locale: L) -> Translator
    where
        L: TryInto<Locale>,
    {
        match locale.try_into() {
            Ok(locale) => self
                .translators
                .get(&locale.0)
                .cloned()
                .unwrap_or_else(|| self.default_translator.clone()),
            Err(_) => self.default_translator.clone(),
        }
    }
}

/// 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 get_locale(&self) -> &str {
        &self.locale
    }
}

/// A locale
pub struct Locale(String);

impl Locale {
    /// Creates a new locale
    ///
    /// # Arguments
    ///
    /// * locale - Locale string
    pub fn new<L>(locale: L) -> Self
    where
        L: Into<String>,
    {
        Self(locale.into())
    }
}

/// User not found when converting an object to locale
#[derive(Debug)]
pub struct LocaleNotFound;

impl Error for LocaleNotFound {}

impl fmt::Display for LocaleNotFound {
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        write!(out, "unable to get locale")
    }
}

impl TryFrom<&User> for Locale {
    type Error = LocaleNotFound;

    fn try_from(user: &User) -> Result<Self, Self::Error> {
        match user.language_code {
            Some(ref language_code) => Ok(Self::new(language_code.clone())),
            None => Err(LocaleNotFound),
        }
    }
}

impl TryFrom<&Update> for Locale {
    type Error = LocaleNotFound;

    fn try_from(update: &Update) -> Result<Self, Self::Error> {
        match update.get_user() {
            Some(user) => Self::try_from(user),
            None => Err(LocaleNotFound),
        }
    }
}

impl TryFrom<&CallbackQuery> for Locale {
    type Error = LocaleNotFound;

    fn try_from(callback_query: &CallbackQuery) -> Result<Self, Self::Error> {
        Self::try_from(&callback_query.from)
    }
}

impl TryFrom<&ChosenInlineResult> for Locale {
    type Error = LocaleNotFound;

    fn try_from(chosen_inline_result: &ChosenInlineResult) -> Result<Self, Self::Error> {
        Self::try_from(&chosen_inline_result.from)
    }
}

impl TryFrom<&Command> for Locale {
    type Error = LocaleNotFound;

    fn try_from(command: &Command) -> Result<Self, Self::Error> {
        match command.get_message().get_user() {
            Some(user) => Self::try_from(user),
            None => Err(LocaleNotFound),
        }
    }
}

impl TryFrom<&InlineQuery> for Locale {
    type Error = LocaleNotFound;

    fn try_from(inline_query: &InlineQuery) -> Result<Self, Self::Error> {
        Self::try_from(&inline_query.from)
    }
}

impl TryFrom<&Message> for Locale {
    type Error = LocaleNotFound;

    fn try_from(message: &Message) -> Result<Self, Self::Error> {
        match message.get_user() {
            Some(user) => Self::try_from(user),
            None => Err(LocaleNotFound),
        }
    }
}

impl TryFrom<&PreCheckoutQuery> for Locale {
    type Error = LocaleNotFound;

    fn try_from(pre_checkout_query: &PreCheckoutQuery) -> Result<Self, Self::Error> {
        Self::try_from(&pre_checkout_query.from)
    }
}

impl TryFrom<&ShippingQuery> for Locale {
    type Error = LocaleNotFound;

    fn try_from(shipping_query: &ShippingQuery) -> Result<Self, Self::Error> {
        Self::try_from(&shipping_query.from)
    }
}

/// 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 store() {
        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 store = TranslatorStore::new(en).add_translator(ru);

        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,
            ),
            (
                TranslationKey::singular("This is context").context("context"),
                "Это контекст",
                ru_update,
            ),
        ] {
            let translator = store.get_translator(&update);
            assert_eq!(translator.translate(key), value);
        }
    }
}