Skip to content

Instantly share code, notes, and snippets.

@florianmski
Created August 23, 2012 22:14
Show Gist options
  • Star 14 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save florianmski/3442664 to your computer and use it in GitHub Desktop.
Save florianmski/3442664 to your computer and use it in GitHub Desktop.
Convert your Localizable.strings (iOS) to strings.xml (Android)
#!/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
file = File.open("strings.xml", "w");
file.puts "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
file.puts "<resources>"
multiple_line_comment = false
ARGF.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")
"%#{i}$#{match}"
}
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 =~ /\/\*(.*)\*\//)
file.puts "<!--#{$1}-->"
# multiple line comment (start)
elsif (line =~ /\/\*(.*)/)
file.puts "<!--#{$1}"
multiple_line_comment = true
# multiple line comment (middle or end)
elsif (multiple_line_comment)
#end of the multiple line comment
if (line =~ /(.*)\*\//)
file.puts "#{$1}-->"
multiple_line_comment = false
else
file.puts line
end
elsif (line =~ /\n/)
file.puts line
end
end
file.puts "</resources>"
@alfogrillo
Copy link

Hi,
I want to report and issue:

Input:
"ABOUT..." = "About...";

Output:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <string name="ABOUT___">"About..."</string>
</resources>

As you can see the . was converted in _
Bye

@vastris
Copy link

vastris commented May 11, 2023

Thank you for the script! It appears to be correct and functional. This Python script is a helpful tool for developers who are converting from iOS to Android and need to migrate their localization strings. By automating the conversion process, this script saves developers time and reduces the risk of human error. The script is easy to use and well-documented, making it a valuable resource for developers who are new to Android development and may not be familiar with Android-specific syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment