perseus/translator/
dummy.rs

1use crate::translator::errors::*;
2
3/// An empty file extension, because all translators must provide one.
4pub const DUMMY_TRANSLATOR_FILE_EXT: &str = "";
5
6/// A dummy translator that will be used if an app doesn't use
7/// internationalization. This has literally no capabilities whatsoever,
8/// and serves as a blank API interface. If this is called as if it's a
9/// fully-fledged translator, it will panic.
10///
11/// If you're using i18n, enable the `translator-fluent` feature flag to replace
12/// this with `FluentTranslator`, which will actually translate things.
13#[derive(Clone, Debug)]
14pub struct DummyTranslator;
15impl DummyTranslator {
16    /// Creates a new dummy translator, accepting the usual parameters for
17    /// translators.
18    pub fn new(_locale: String, _translations_string: String) -> Result<Self, TranslatorError> {
19        Ok(Self {})
20    }
21    /// A dummy function for localizing a URL. This will panic if called.
22    pub fn url(&self, _url: &str) -> String {
23        panic!("attempted to call function on dummy translator, please add the `translator-fluent` feature flag if you want to use i18n")
24    }
25    /// Returns the `xx-XX` locale always, which is used by Perseus if i18n is
26    /// disabled.
27    pub fn get_locale(&self) -> String {
28        "xx-XX".to_string()
29    }
30    /// A dummy function that will NOT translate the given ID! This will panic
31    /// if called.
32    pub fn translate(&self, _id: &str) -> String {
33        panic!("attempted to call function on dummy translator, please add the `translator-fluent` feature flag if you want to use i18n")
34    }
35    /// A dummy function that will NOT translate the given ID! This will panic
36    /// if called.
37    pub fn translate_checked<I: Into<String> + std::fmt::Display>(
38        &self,
39        _id: &str,
40    ) -> Result<String, TranslatorError> {
41        panic!("attempted to call function on dummy translator, please add the `translator-fluent` feature flag if you want to use i18n")
42    }
43}
44
45/// A useless pseudo-map. This is a workaround until conditional compilation of
46/// expressions is supported, which will simplify this system significantly.
47#[doc(hidden)]
48#[derive(Debug)]
49pub struct TranslationArgs;
50impl TranslationArgs {
51    /// Creates a new instance of this `struct`.
52    #[allow(clippy::new_without_default)]
53    pub fn new() -> Self {
54        Self {}
55    }
56    /// A filler function to conform to the typical argument-setting interface.
57    /// Again, this will be entirely unnecessary once conditional expression
58    /// compilation is supported.
59    pub fn set(&self, _key: &str, _val: &str) {}
60}
61
62/// The internal dummy backend for the `t!` macro. This
63#[doc(hidden)]
64pub fn t_macro_backend(_id: &str) -> String {
65    panic!("attempted to call translator macro, you should enable the `translator-fluent` flag to use i18n")
66}
67/// The internal Fluent backend for the `t!` macro, when it's used with
68/// arguments.
69#[doc(hidden)]
70pub fn t_macro_backend_with_args(_id: &str, _args: TranslationArgs) -> String {
71    panic!("attempted to call translator macro, you should enable the `translator-fluent` flag to use i18n")
72}
73/// The internal Fluent backend for the `link!` macro.
74#[doc(hidden)]
75pub fn link_macro_backend(_url: &str) -> String {
76    panic!("attempted to call translator macro, you should enable the `translator-fluent` flag to use i18n")
77}