Skip to content

Commit

Permalink
fix(i18n): 🐛 fixed link! macro with base path
Browse files Browse the repository at this point in the history
  • Loading branch information
arctic-hen7 committed Oct 9, 2021
1 parent 9a4a956 commit d676471
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 17 deletions.
6 changes: 2 additions & 4 deletions packages/perseus/src/client_translations_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ impl ClientTranslationsManager {
Ok(translations_str) => match translations_str {
Some(translations_str) => {
// All good, turn the translations into a translator
let translator =
Translator::new(locale.to_string(), translations_str, &path_prefix);
let translator = Translator::new(locale.to_string(), translations_str);
match translator {
Ok(translator) => translator,
Err(err) => {
Expand Down Expand Up @@ -78,8 +77,7 @@ impl ClientTranslationsManager {
Ok(Rc::clone(self.cached_translator.as_ref().unwrap()))
} else if !self.locales.using_i18n {
// If we aren't even using i18n, then it would be pointless to fetch translations
let translator =
Translator::new("xx-XX".to_string(), "".to_string(), &path_prefix).unwrap();
let translator = Translator::new("xx-XX".to_string(), "".to_string()).unwrap();
// Cache that translator
self.cached_translator = Some(Rc::new(translator));
// Now return that
Expand Down
9 changes: 5 additions & 4 deletions packages/perseus/src/translations_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,12 @@ impl TranslationsManager for FsTranslationsManager {
translations_str = self.get_translations_str_for_locale(locale.clone()).await?;
}
// We expect the translations defined there, but not the locale itself
let translator = Translator::new(locale.clone(), translations_str, &self.path_prefix)
.map_err(|err| TranslationsManagerError::SerializationFailed {
let translator = Translator::new(locale.clone(), translations_str).map_err(|err| {
TranslationsManagerError::SerializationFailed {
locale: locale.clone(),
source: err.into(),
})?;
}
})?;

Ok(translator)
}
Expand Down Expand Up @@ -191,7 +192,7 @@ impl TranslationsManager for DummyTranslationsManager {
&self,
locale: String,
) -> Result<Translator, TranslationsManagerError> {
let translator = Translator::new(locale.clone(), String::new(), "").map_err(|err| {
let translator = Translator::new(locale.clone(), String::new()).map_err(|err| {
TranslationsManagerError::SerializationFailed {
locale,
source: err.into(),
Expand Down
11 changes: 2 additions & 9 deletions packages/perseus/src/translator/fluent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,10 @@ pub struct FluentTranslator {
bundle: Rc<FluentBundle<FluentResource>>,
/// The locale for which translations are being managed by this instance.
locale: String,
/// The path prefix to apply when calling the `.url()` method.
path_prefix: String,
}
impl FluentTranslator {
/// Creates a new translator for a given locale, passing in translations in FTL syntax form.
pub fn new(
locale: String,
ftl_string: String,
path_prefix: &str,
) -> Result<Self, TranslatorError> {
pub fn new(locale: String, ftl_string: String) -> Result<Self, TranslatorError> {
let resource = FluentResource::try_new(ftl_string)
// If this errors, we get it still and a vector of errors (wtf.)
.map_err(|(_, errs)| TranslatorError::TranslationsStrSerFailed {
Expand Down Expand Up @@ -59,12 +53,11 @@ impl FluentTranslator {
Ok(Self {
bundle: Rc::new(bundle),
locale,
path_prefix: path_prefix.to_string(),
})
}
/// Gets the path to the given URL in whatever locale the instance is configured for. This also applies the path prefix.
pub fn url<S: Into<String> + std::fmt::Display>(&self, url: S) -> String {
format!("{}/{}{}", self.path_prefix, self.locale, url)
format!("{}{}", self.locale, url)
}
/// Gets the locale for which this instancce is configured.
pub fn get_locale(&self) -> String {
Expand Down

0 comments on commit d676471

Please sign in to comment.