summaryrefslogtreecommitdiffhomepage
path: root/android
diff options
context:
space:
mode:
authorJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-03-29 22:17:54 +0000
committerJanito Vaqueiro Ferreira Filho <janito@mullvad.net>2021-05-19 12:13:36 +0000
commitee319af925b16662bcb77240e5b705c8fcc77231 (patch)
treeb33874b8acaba7bb6167e0ddb6496b8433aebc80 /android
parentb6497da49f7f31c2259dee709a0af1383d008017 (diff)
downloadmullvadvpn-ee319af925b16662bcb77240e5b705c8fcc77231.tar.xz
mullvadvpn-ee319af925b16662bcb77240e5b705c8fcc77231.zip
Move Android string resources code into new module
Diffstat (limited to 'android')
-rw-r--r--android/translations-converter/src/android/mod.rs128
-rw-r--r--android/translations-converter/src/android/strings.rs133
2 files changed, 135 insertions, 126 deletions
diff --git a/android/translations-converter/src/android/mod.rs b/android/translations-converter/src/android/mod.rs
index 71a9622f1a..0ddcad8508 100644
--- a/android/translations-converter/src/android/mod.rs
+++ b/android/translations-converter/src/android/mod.rs
@@ -1,4 +1,5 @@
mod string_value;
+mod strings;
use self::string_value::StringValue;
use serde::{Deserialize, Serialize};
@@ -7,132 +8,7 @@ use std::{
ops::{Deref, DerefMut},
};
-/// Contents of an Android string resources file.
-///
-/// This type can be created directly deserializing the `strings.xml` file.
-#[derive(Clone, Debug, Deserialize, Serialize)]
-pub struct StringResources {
- #[serde(rename = "string")]
- entries: Vec<StringResource>,
-}
-
-/// An entry in an Android string resources file.
-#[derive(Clone, Debug, Deserialize, Serialize)]
-pub struct StringResource {
- /// The string resource ID.
- pub name: String,
-
- /// If the string should be translated or not.
- #[serde(default = "default_translatable")]
- pub translatable: bool,
-
- /// The string value.
- #[serde(rename = "$value")]
- pub value: StringValue,
-}
-
-impl StringResources {
- /// Create an empty list of Android string resources.
- pub fn new() -> Self {
- StringResources {
- entries: Vec::new(),
- }
- }
-
- /// Normalize the strings into a common format.
- ///
- /// Allows the string values to be compared to the gettext messages.
- pub fn normalize(&mut self) {
- for entry in &mut self.entries {
- entry.normalize();
- }
- }
-
- /// Sorts the entries alphabetically based on their IDs.
- pub fn sort(&mut self) {
- self.entries
- .sort_by(|left, right| left.name.cmp(&right.name));
- }
-}
-
-impl Deref for StringResources {
- type Target = Vec<StringResource>;
-
- fn deref(&self) -> &Self::Target {
- &self.entries
- }
-}
-
-impl DerefMut for StringResources {
- fn deref_mut(&mut self) -> &mut Self::Target {
- &mut self.entries
- }
-}
-
-impl IntoIterator for StringResources {
- type Item = StringResource;
- type IntoIter = std::vec::IntoIter<Self::Item>;
-
- fn into_iter(self) -> Self::IntoIter {
- self.entries.into_iter()
- }
-}
-
-impl StringResource {
- /// Create a new Android string resource entry.
- ///
- /// The name is the resource ID, and the value will be properly escaped.
- pub fn new(name: String, value: &str) -> Self {
- StringResource {
- name,
- translatable: true,
- value: StringValue::from(value),
- }
- }
-
- /// Normalize the string value into a common format.
- ///
- /// Makes it possible to compare the Android strings with the gettext messages.
- pub fn normalize(&mut self) {
- self.value.normalize();
- }
-}
-
-fn default_translatable() -> bool {
- true
-}
-
-// Unfortunately, direct serialization to XML isn't working correctly.
-impl Display for StringResources {
- fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
- writeln!(formatter, r#"<?xml version="1.0" encoding="utf-8"?>"#)?;
- writeln!(formatter, "<resources>")?;
-
- for string in &self.entries {
- writeln!(formatter, " {}", string)?;
- }
-
- writeln!(formatter, "</resources>")
- }
-}
-
-impl Display for StringResource {
- fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
- if self.translatable {
- write!(
- formatter,
- r#"<string name="{}">{}</string>"#,
- self.name, self.value
- )
- } else {
- write!(
- formatter,
- r#"<string name="{}" translatable="false">{}</string>"#,
- self.name, self.value
- )
- }
- }
-}
+pub use self::strings::{StringResource, StringResources};
/// Contents of an Android plurals resources file.
///
diff --git a/android/translations-converter/src/android/strings.rs b/android/translations-converter/src/android/strings.rs
new file mode 100644
index 0000000000..3ef0dabc8f
--- /dev/null
+++ b/android/translations-converter/src/android/strings.rs
@@ -0,0 +1,133 @@
+use super::string_value::StringValue;
+use serde::{Deserialize, Serialize};
+use std::{
+ fmt::{self, Display, Formatter},
+ ops::{Deref, DerefMut},
+};
+
+/// Contents of an Android string resources file.
+///
+/// This type can be created directly deserializing the `strings.xml` file.
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub struct StringResources {
+ #[serde(rename = "string")]
+ entries: Vec<StringResource>,
+}
+
+/// An entry in an Android string resources file.
+#[derive(Clone, Debug, Deserialize, Serialize)]
+pub struct StringResource {
+ /// The string resource ID.
+ pub name: String,
+
+ /// If the string should be translated or not.
+ #[serde(default = "default_translatable")]
+ pub translatable: bool,
+
+ /// The string value.
+ #[serde(rename = "$value")]
+ pub value: StringValue,
+}
+
+impl StringResources {
+ /// Create an empty list of Android string resources.
+ pub fn new() -> Self {
+ StringResources {
+ entries: Vec::new(),
+ }
+ }
+
+ /// Normalize the strings into a common format.
+ ///
+ /// Allows the string values to be compared to the gettext messages.
+ pub fn normalize(&mut self) {
+ for entry in &mut self.entries {
+ entry.normalize();
+ }
+ }
+
+ /// Sorts the entries alphabetically based on their IDs.
+ pub fn sort(&mut self) {
+ self.entries
+ .sort_by(|left, right| left.name.cmp(&right.name));
+ }
+}
+
+impl Deref for StringResources {
+ type Target = Vec<StringResource>;
+
+ fn deref(&self) -> &Self::Target {
+ &self.entries
+ }
+}
+
+impl DerefMut for StringResources {
+ fn deref_mut(&mut self) -> &mut Self::Target {
+ &mut self.entries
+ }
+}
+
+impl IntoIterator for StringResources {
+ type Item = StringResource;
+ type IntoIter = std::vec::IntoIter<Self::Item>;
+
+ fn into_iter(self) -> Self::IntoIter {
+ self.entries.into_iter()
+ }
+}
+
+impl StringResource {
+ /// Create a new Android string resource entry.
+ ///
+ /// The name is the resource ID, and the value will be properly escaped.
+ pub fn new(name: String, value: &str) -> Self {
+ StringResource {
+ name,
+ translatable: true,
+ value: StringValue::from(value),
+ }
+ }
+
+ /// Normalize the string value into a common format.
+ ///
+ /// Makes it possible to compare the Android strings with the gettext messages.
+ pub fn normalize(&mut self) {
+ self.value.normalize();
+ }
+}
+
+fn default_translatable() -> bool {
+ true
+}
+
+// Unfortunately, direct serialization to XML isn't working correctly.
+impl Display for StringResources {
+ fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
+ writeln!(formatter, r#"<?xml version="1.0" encoding="utf-8"?>"#)?;
+ writeln!(formatter, "<resources>")?;
+
+ for string in &self.entries {
+ writeln!(formatter, " {}", string)?;
+ }
+
+ writeln!(formatter, "</resources>")
+ }
+}
+
+impl Display for StringResource {
+ fn fmt(&self, formatter: &mut Formatter) -> fmt::Result {
+ if self.translatable {
+ write!(
+ formatter,
+ r#"<string name="{}">{}</string>"#,
+ self.name, self.value
+ )
+ } else {
+ write!(
+ formatter,
+ r#"<string name="{}" translatable="false">{}</string>"#,
+ self.name, self.value
+ )
+ }
+ }
+}