From 02642586a53d18624ceb19f79ba801dc6e29d828 Mon Sep 17 00:00:00 2001 From: Benjamin Winger Date: Tue, 21 Jul 2020 16:10:02 +0000 Subject: [PATCH] Fallback to default when requested language is missing, rather than panicking (#16) --- templates/src/loader/arc_loader.rs | 10 ++++++---- templates/src/loader/static_loader.rs | 16 ++++++++++------ 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/templates/src/loader/arc_loader.rs b/templates/src/loader/arc_loader.rs index 3d61166..ae4fae5 100644 --- a/templates/src/loader/arc_loader.rs +++ b/templates/src/loader/arc_loader.rs @@ -109,9 +109,11 @@ impl super::Loader for ArcLoader { text_id: &str, args: Option<&HashMap>, ) -> String { - for l in self.fallbacks.get(lang).expect("language not found") { - if let Some(val) = self.lookup_single_language(l, text_id, args) { - return val; + if let Some(fallbacks) = self.fallbacks.get(lang) { + for l in fallbacks { + if let Some(val) = self.lookup_single_language(l, text_id, args) { + return val; + } } } if *lang != self.fallback { @@ -168,7 +170,7 @@ impl ArcLoader { None } } else { - panic!("Unknown language {}", lang) + None } } } diff --git a/templates/src/loader/static_loader.rs b/templates/src/loader/static_loader.rs index 2d59429..ad4ae17 100644 --- a/templates/src/loader/static_loader.rs +++ b/templates/src/loader/static_loader.rs @@ -69,9 +69,11 @@ impl StaticLoader { text_id: &str, args: Option<&HashMap>, ) -> Option { - for l in self.fallbacks.get(lang).expect("language not found") { - if let Some(val) = self.lookup_single_language(l, text_id, args) { - return Some(val); + if let Some(fallbacks) = self.fallbacks.get(lang) { + for l in fallbacks { + if let Some(val) = self.lookup_single_language(l, text_id, args) { + return Some(val); + } } } @@ -87,9 +89,11 @@ impl super::Loader for StaticLoader { text_id: &str, args: Option<&HashMap>, ) -> String { - for l in self.fallbacks.get(lang).expect("language not found") { - if let Some(val) = self.lookup_single_language(l, text_id, args) { - return val; + if let Some(fallbacks) = self.fallbacks.get(lang) { + for l in fallbacks { + if let Some(val) = self.lookup_single_language(l, text_id, args) { + return val; + } } } if *lang != self.fallback {