Skip to content

Commit

Permalink
Add script to convert from iOS string resources to Android and conver…
Browse files Browse the repository at this point in the history
…t all

The instructions in the script are all based on my folder config, but with
a little tweaking will work for anyone. However, we should only need to do
this one time as the iOS strings aren't changing. All new strings have been
manually added at the end of the XML files after the iOS ones.

Note that all strings within the XML are now surrounded by double quotes.
This removes the need to escape the apostrophes which appear in many strings.
  • Loading branch information
davecraig committed Dec 10, 2024
1 parent 185b000 commit fd58213
Show file tree
Hide file tree
Showing 14 changed files with 14,416 additions and 2,411 deletions.
81 changes: 81 additions & 0 deletions app/src/main/res/convertIOStoAndroid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/usr/bin/ruby

# based on https://github.com/tmurakam/cashflow/blob/0a01ac9e0350dfb04979986444244f8daf4cb5a8/android/convertStrings.rb
# support comments and Converter such as "%@", "%d", "%0.1f"...
# in your directory : ./main.rb Localizable.strings

# Here's how we use it in Soundscape to convert from iOS.
#
# 1. Clone the soundscape repo
# git clone https://github.com/soundscape-community/soundscape.git
#
# 2. Change to the localization directory:
# cd ~/STA/Soundscape-iOS/apps/ios/GuideDogs/Assets/Localization
#
# 2a. Convert to UTF-8 from UTF-16LE
# find -name Localizable.strings -print0 | xargs -0 -n1 -i sh -c 'iconv -f UTF-16LE -t UTF-8 {} -o {}.utf8'
#
# 3. Run the conversion script:
# find -name Localizable.strings.utf8 -print0 | xargs -0 -n1 ~/STA/Soundscape-AndroidTest/app/src/main/res/convertIOStoAndroid.rb {}
#
# 3a. Tidy up empty files:
# rm en-US.lproj/Localizable.strings.utf8.xml
#
# 4. We now have the files in Android format e.g. da-DK.lproj/Localizable.strings.xml
#
# 5. Now copy them into place:
# mkdir -p ~/STA/Soundscape-AndroidTest/app/src/main/res/values-en
# find -name Localizable.strings.utf8.xml -print0 | xargs -0 -n1 -i sh -c 'lang=`echo $"{}" | cut -c4-5`; mv "{}" ~/STA/Soundscape-AndroidTest/app/src/main/res/values-$lang/strings.xml'
# mv ~/STA/Soundscape-AndroidTest/app/src/main/res/values-en/strings.xml ~/STA/Soundscape-AndroidTest/app/src/main/res/values/strings.xml
# rm -fr ~/STA/Soundscape-AndroidTest/app/src/main/res/values-en
#
# Note that this script was amended to strip out comments (they were all empty in the iOS files),
# and remove blank lines.

printf "Opening input %s\n", ARGV[1]

input_file = File.open(ARGV[1], "r");
output_file = File.open(ARGV[1] + ".xml", "w");

output_file.puts "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
output_file.puts "<resources>"

multiple_line_comment = false

input_file.each do |line|
if (line =~ /\"(.*)\"\s*=\s*\"(.*)\"/)
name = $1
value = $2

name.gsub!(/[ .]/, "_")

value.gsub!(/&/, "&amp;")
value.gsub!(/</, "&lt;")

i = 0
# convert %@ to %1$s
value.gsub!(/%([0-9]*.*[@sScCdoxXfeEgabBhH])/) {|s|
i += 1
match = $1
match.gsub!(/@/, "s")
"%#{match}"
}

output_file.puts " <string name=\"#{name}\">\"#{value}\"</string>"
# one line comment // The cake is a lie
# multiple line comment on one line /* The cake is a lie */
elsif (line =~ /\/\/(.*)/ || line =~ /\/\*(.*)\*\//)
# multiple line comment (start)
elsif (line =~ /\/\*(.*)/)
multiple_line_comment = true
# multiple line comment (middle or end)
elsif (multiple_line_comment)
#end of the multiple line comment
if (line =~ /(.*)\*\//)
multiple_line_comment = false
end
end
end

output_file.puts "</resources>"

1,287 changes: 1,102 additions & 185 deletions app/src/main/res/values-da/strings.xml

Large diffs are not rendered by default.

1,286 changes: 1,102 additions & 184 deletions app/src/main/res/values-de/strings.xml

Large diffs are not rendered by default.

1,288 changes: 1,102 additions & 186 deletions app/src/main/res/values-el/strings.xml

Large diffs are not rendered by default.

1,286 changes: 1,102 additions & 184 deletions app/src/main/res/values-es/strings.xml

Large diffs are not rendered by default.

1,287 changes: 1,102 additions & 185 deletions app/src/main/res/values-fi/strings.xml

Large diffs are not rendered by default.

1,286 changes: 1,102 additions & 184 deletions app/src/main/res/values-fr/strings.xml

Large diffs are not rendered by default.

1,285 changes: 1,102 additions & 183 deletions app/src/main/res/values-it/strings.xml

Large diffs are not rendered by default.

1,287 changes: 1,102 additions & 185 deletions app/src/main/res/values-ja/strings.xml

Large diffs are not rendered by default.

1,287 changes: 1,102 additions & 185 deletions app/src/main/res/values-nb/strings.xml

Large diffs are not rendered by default.

1,288 changes: 1,102 additions & 186 deletions app/src/main/res/values-nl/strings.xml

Large diffs are not rendered by default.

1,288 changes: 1,102 additions & 186 deletions app/src/main/res/values-pt/strings.xml

Large diffs are not rendered by default.

1,286 changes: 1,102 additions & 184 deletions app/src/main/res/values-sv/strings.xml

Large diffs are not rendered by default.

1,305 changes: 1,111 additions & 194 deletions app/src/main/res/values/strings.xml

Large diffs are not rendered by default.

0 comments on commit fd58213

Please sign in to comment.