Skip to content

Instantly share code, notes, and snippets.

@iseebi
Last active August 21, 2019 02:06
Show Gist options
  • Save iseebi/e3cf668cf6c32506ab38e013c640d4f5 to your computer and use it in GitHub Desktop.
Save iseebi/e3cf668cf6c32506ab38e013c640d4f5 to your computer and use it in GitHub Desktop.
Localizable.strings / strings.xml を CSV にする
#!/usr/bin/env ruby
LANGS = ['ja', 'en', 'zh-Hant']
def put_row(items)
puts '"' + items.join('","') + '"'
end
values = Hash.new
LANGS.each do |lang|
filename = "./#{lang}.lproj/Localizable.strings"
contents = File.open(filename, 'r') {|f| f.read }
contents.scan(/^"([^"]+)"\s*=\s*"([^"]*)"\s*;/).each do |m|
key = m[0]
value = m[1]
if values[key]
values[key][lang] = value
else
values[key] = { lang => value }
end
end
end
put_row(['key'] + LANGS)
values.keys.each do |k|
row = [k]
LANGS.each do |l|
row << values[k][l]
end
put_row(row)
end
#!/usr/bin/env ruby
require 'rexml/document'
LANGS = {'ja' => 'values-ja', 'en' => 'values', 'cn' => 'values-zh-rTW'}
BASE_DIR = '.'
def put_row(items)
puts '"' + items.join('","') + '"'
end
values = Hash.new
LANGS.each do |lang,dir|
filename = "#{BASE_DIR}/src/main/res/#{dir}/strings.xml"
File.open(filename, 'r') do |f|
doc = REXML::Document.new(f)
doc.elements.each('resources/string') do |elem|
key = elem.attributes['name']
value = elem.text
if values[key]
values[key][lang] = value
else
values[key] = { lang => value }
end
end
end
end
put_row(['key'] + LANGS.keys)
values.keys.each do |k|
row = [k]
LANGS.keys.each do |l|
row << values[k][l]
end
put_row(row)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment