-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(i18n): 🚚 refactored to prepare for future multi-translator s…
…upport
- Loading branch information
1 parent
89fa00e
commit 24f4362
Showing
8 changed files
with
78 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/// Defines app information about i18n, specifically about which locales are supported. | ||
#[derive(Clone)] | ||
pub struct Locales { | ||
/// The default locale, which will be used as a fallback if the user's locale can't be extracted. This will be built for at build-time. | ||
pub default: String, | ||
/// All other supported locales, which will all be built at build time. | ||
pub other: Vec<String>, | ||
} | ||
impl Locales { | ||
/// Gets all the supported locales by combining the default, and other. | ||
pub fn get_all(&self) -> Vec<&String> { | ||
let mut vec: Vec<&String> = vec![&self.default]; | ||
vec.extend(&self.other); | ||
|
||
vec | ||
} | ||
/// Checks if the given locale is supported. | ||
pub fn is_supported(&self, locale: &str) -> bool { | ||
let locales = self.get_all(); | ||
locales.iter().any(|l| *l == locale) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
pub use error_chain::bail; | ||
use error_chain::error_chain; | ||
|
||
// This has its own error management because the user might be implementing translators themselves | ||
error_chain! { | ||
errors { | ||
/// For when a translation ID doesn't exist. | ||
TranslationIdNotFound(id: String, locale: String) { | ||
description("translation id not found for current locale") | ||
display("translation id '{}' not found for locale '{}'", id, locale) | ||
} | ||
/// For when the given string of translations couldn't be correctly parsed | ||
TranslationsStrSerFailed(locale: String, err: String) { | ||
description("given translations string couldn't be parsed") | ||
display("given translations string for locale '{}' couldn't be parsed: '{}'", locale, err) | ||
} | ||
/// For when the given locale was invalid. This takes an error because different i18n systems may have different requirements. | ||
InvalidLocale(locale: String, err: String) { | ||
description("given locale was invalid") | ||
display("given locale '{}' was invalid: '{}'", locale, err) | ||
} | ||
/// For when the translation of a message failed for some reason generally. | ||
TranslationFailed(id: String, locale: String, err: String) { | ||
description("message translation failed") | ||
display("translation of message with id '{}' into locale '{}' failed: '{}'", id, locale, err) | ||
} | ||
/// For when the we couldn't arrive at a translation for some reason. This might be caused by an invalid variant for a compound | ||
/// message. | ||
NoTranslationDerived(id: String, locale: String) { | ||
description("no translation derived for message") | ||
display("no translation could be derived for message with id '{}' in locale '{}'", id, locale) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
/// Errors for translators. These are separate so new translators can easily be created in a modular fashion. | ||
pub mod errors; | ||
mod fluent; | ||
|
||
// We export each translator by name | ||
pub use fluent::{FluentTranslator, FLUENT_TRANSLATOR_FILE_EXT}; | ||
|
||
// And then we export defaults using feature gates | ||
// TODO feature-gate these lines | ||
pub use FluentTranslator as Translator; | ||
pub use FLUENT_TRANSLATOR_FILE_EXT as TRANSLATOR_FILE_EXT; |