aboutsummaryrefslogtreecommitdiff
path: root/azalea-chat/src/component.rs
diff options
context:
space:
mode:
authorTert0 <tert0byte@gmail.com>2025-09-15 06:58:00 +0200
committerGitHub <noreply@github.com>2025-09-14 23:58:00 -0500
commit1a42c08030c865948e9e9b6dc8a1f4e38550063a (patch)
treec5342b8e27edc4536d0f4a19628be48de9b15651 /azalea-chat/src/component.rs
parentbcefd0213db05b4c29c82a1031f4d6e838e1fc1f (diff)
downloadazalea-drasl-1a42c08030c865948e9e9b6dc8a1f4e38550063a.tar.xz
implement translation fallback (#244)
Diffstat (limited to 'azalea-chat/src/component.rs')
-rw-r--r--azalea-chat/src/component.rs72
1 files changed, 69 insertions, 3 deletions
diff --git a/azalea-chat/src/component.rs b/azalea-chat/src/component.rs
index 62e5e151..c7a69390 100644
--- a/azalea-chat/src/component.rs
+++ b/azalea-chat/src/component.rs
@@ -295,6 +295,16 @@ impl<'de> Deserialize<'de> for FormattedText {
.as_str()
.ok_or_else(|| de::Error::custom("\"translate\" must be a string"))?
.into();
+ let fallback = if let Some(fallback) = json.get("fallback") {
+ Some(
+ fallback
+ .as_str()
+ .ok_or_else(|| de::Error::custom("\"fallback\" must be a string"))?
+ .to_string(),
+ )
+ } else {
+ None
+ };
if let Some(with) = json.get("with") {
let with = with
.as_array()
@@ -316,13 +326,14 @@ impl<'de> Deserialize<'de> for FormattedText {
FormattedText::deserialize(item).map_err(de::Error::custom)?,
));
}
- component = FormattedText::Translatable(TranslatableComponent::new(
- translate, with_array,
+ component = FormattedText::Translatable(TranslatableComponent::with_fallback(
+ translate, fallback, with_array,
));
} else {
// if it doesn't have a "with", just have the with_array be empty
- component = FormattedText::Translatable(TranslatableComponent::new(
+ component = FormattedText::Translatable(TranslatableComponent::with_fallback(
translate,
+ fallback,
Vec::new(),
));
}
@@ -678,3 +689,58 @@ impl Default for FormattedText {
FormattedText::Text(TextComponent::default())
}
}
+
+#[cfg(test)]
+mod tests {
+ use serde_json::Value;
+
+ use super::*;
+
+ #[test]
+ fn deserialize_translation() {
+ let j: Value =
+ serde_json::from_str(r#"{"translate": "translation.test.args", "with": ["a", "b"]}"#)
+ .unwrap();
+ let component = FormattedText::deserialize(&j).unwrap();
+ assert_eq!(
+ component,
+ FormattedText::Translatable(TranslatableComponent::new(
+ "translation.test.args".to_string(),
+ vec![
+ StringOrComponent::String("a".to_string()),
+ StringOrComponent::String("b".to_string())
+ ]
+ ))
+ );
+ }
+
+ #[test]
+ fn deserialize_translation_invalid_arguments() {
+ let j: Value =
+ serde_json::from_str(r#"{"translate": "translation.test.args", "with": {}}"#).unwrap();
+ assert!(FormattedText::deserialize(&j).is_err());
+ }
+
+ #[test]
+ fn deserialize_translation_fallback() {
+ let j: Value = serde_json::from_str(r#"{"translate": "translation.test.undefined", "fallback": "fallback: %s", "with": ["a"]}"#).unwrap();
+ let component = FormattedText::deserialize(&j).unwrap();
+ assert_eq!(
+ component,
+ FormattedText::Translatable(TranslatableComponent::with_fallback(
+ "translation.test.undefined".to_string(),
+ Some("fallback: %s".to_string()),
+ vec![StringOrComponent::String("a".to_string())]
+ ))
+ );
+ }
+
+ #[test]
+ fn deserialize_translation_invalid_fallback() {
+ let j: Value = serde_json::from_str(
+ r#"{"translate": "translation.test.undefined", "fallback": {"text": "invalid"}}"#,
+ )
+ .unwrap();
+ assert!(FormattedText::deserialize(&j).is_err());
+ }
+}