Class: Vocab::Translator::Android
- Defined in:
- lib/vocab/translator/android.rb
Class Method Summary collapse
-
.doc_from_xml(path) ⇒ Object
Helper methods.
- .hash_from_xml(path) ⇒ Object
- .locales(dir, strict = true) ⇒ Object
- .plural_keys(locales_dir) ⇒ Object
-
.plurals_from_xml(path) ⇒ Object
Extracts plural definitions from xml to a hash.
- .string_keys(locales_dir) ⇒ Object
- .write(strings, plurals, path) ⇒ Object
Methods inherited from Base
Class Method Details
.doc_from_xml(path) ⇒ Object
Helper methods
93 94 95 96 97 |
# File 'lib/vocab/translator/android.rb', line 93 def self.doc_from_xml( path ) xml = File.open( path ) { |f| f.read } doc = Nokogiri::HTML.fragment( xml ) { |config| config.noblanks } return doc end |
.hash_from_xml(path) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 |
# File 'lib/vocab/translator/android.rb', line 5 def self.hash_from_xml( path ) doc = doc_from_xml( path ) children = doc.search( 'resources/string' ) hash = {} children.each do |child| text = child.text # escape apostrophes for android resource loader text = text.gsub( /([^\\])'/, %q[\1\\\'] ) hash[ child['name'] ] = text end return hash end |
.locales(dir, strict = true) ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/vocab/translator/android.rb', line 79 def self.locales( dir, strict = true ) xml_pattern = strict ? 'strings.xml' : '*' locales = [] Dir.glob( "#{dir}/values-*/#{xml_pattern}" ).each do |path| locales << $1 if path =~ /values-(.*)\// end return locales end |
.plural_keys(locales_dir) ⇒ Object
72 73 74 75 76 77 |
# File 'lib/vocab/translator/android.rb', line 72 def self.plural_keys( locales_dir ) path = "#{locales_dir}/values/strings.xml" translations = Vocab::Translator::Android.plurals_from_xml( path ) keys = translations.keys.map { |key| Vocab::Translator::Base.ignore_key?( key ) ? nil : key }.compact return keys end |
.plurals_from_xml(path) ⇒ Object
Extracts plural definitions from xml to a hash
For example:
<plurals name="user_count">
<item quantity="one">1 user</item>
<item quantity="many">%d users</item>
</plurals>
Is returned as:
{ "user_count" => { "one" => "1 user",
"many" => "%d users" } }
31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/vocab/translator/android.rb', line 31 def self.plurals_from_xml( path ) doc = doc_from_xml( path ) plurals = {} doc.search( 'resources/plurals' ).each do |plural| items = {} plural.search( 'item' ).each do |item| items[ item['quantity' ] ] = item.text end plurals[ plural['name'] ] = items end return plurals end |
.string_keys(locales_dir) ⇒ Object
65 66 67 68 69 70 |
# File 'lib/vocab/translator/android.rb', line 65 def self.string_keys( locales_dir ) path = "#{locales_dir}/values/strings.xml" translations = Vocab::Translator::Android.hash_from_xml( path ) keys = translations.keys.map { |key| Vocab::Translator::Base.ignore_key?( key ) ? nil : key }.compact return keys end |
.write(strings, plurals, path) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/vocab/translator/android.rb', line 46 def self.write( strings, plurals, path ) builder = Nokogiri::XML::Builder.new do |xml| xml.resources do strings.sort.each do |key, value| xml.string( value, :name => key ) end plurals.keys.sort.each do |key| xml.plurals( :name => key ) do plurals[ key ].each do |quantity, value| xml.item( value, :quantity => quantity ) end end end end end File.open( path, 'w' ) { |f| f.write( builder.to_xml( :encoding => 'UTF-8' ) ) } end |