Class: Vocab::Merger::Android

Inherits:
Base
  • Object
show all
Defined in:
lib/vocab/merger/android.rb

Constant Summary collapse

FORMAT_PATTERN =
/%\w/
ARG_PATTERN =
/\$[a-zA-Z]/

Instance Attribute Summary

Attributes inherited from Base

#locales_dir, #updates_dir

Instance Method Summary collapse

Methods inherited from Base

#merge

Constructor Details

#initialize(locales_dir = nil, updates_dir = nil) ⇒ Android

Returns a new instance of Android.



7
8
9
10
11
12
13
14
15
# File 'lib/vocab/merger/android.rb', line 7

def initialize( locales_dir = nil, updates_dir = nil )
  @locales_dir = locales_dir || 'res'
  @updates_dir = updates_dir || 'tmp/translations'
  @english_path = "#{@locales_dir}/values/strings.xml"
  if File.exists?( @english_path )
    @english_strings = english_strings
    @english_plurals = english_plurals
  end
end

Instance Method Details

#check_all_format_strings(file) ⇒ Object



66
67
68
69
# File 'lib/vocab/merger/android.rb', line 66

def check_all_format_strings( file )
  strings( file )
  plurals( file )
end

#check_matching_format_strings(key, new_value, path, format_checker) ⇒ Object



62
63
64
# File 'lib/vocab/merger/android.rb', line 62

def check_matching_format_strings( key, new_value, path, format_checker )
  send( format_checker, key, new_value, path) 
end

#current_plurals_for_locale(path) ⇒ Object



104
105
106
# File 'lib/vocab/merger/android.rb', line 104

def current_plurals_for_locale( path )
  return Vocab::Translator::Android.plurals_from_xml( path )
end

#current_strings_for_locale(path) ⇒ Object



95
96
97
# File 'lib/vocab/merger/android.rb', line 95

def current_strings_for_locale( path )
  return Vocab::Translator::Android.hash_from_xml( path )
end

#english_pluralsObject



58
59
60
# File 'lib/vocab/merger/android.rb', line 58

def english_plurals
  return Vocab::Translator::Android.plurals_from_xml( @english_path )
end

#english_stringsObject



54
55
56
# File 'lib/vocab/merger/android.rb', line 54

def english_strings
  return Vocab::Translator::Android.hash_from_xml( @english_path )
end

#files_to_mergeObject



117
118
119
120
121
# File 'lib/vocab/merger/android.rb', line 117

def files_to_merge
  return translation_locales.collect do |locale|
    "#{@locales_dir}/values-#{locale}/strings.xml"
  end
end

#merge_file(path, strict = true) ⇒ Object



17
18
19
20
21
# File 'lib/vocab/merger/android.rb', line 17

def merge_file( path, strict = true )
  strings = strings( path )
  plurals = plurals( path )
  Vocab::Translator::Android.write( strings, plurals, path )
end

#plural_format_changed?(key, new_value, path) ⇒ Boolean

Returns:

  • (Boolean)


71
72
73
74
75
76
77
78
# File 'lib/vocab/merger/android.rb', line 71

def plural_format_changed?( key, new_value, path )
  new_value.each do |inner_key,inner_value|
    if ( @english_plurals[ key ][ inner_key ].to_s.scan( FORMAT_PATTERN ) != inner_value.to_s.scan( FORMAT_PATTERN ) ) ||
     ( @english_plurals[ key ][ inner_key ].to_s.scan( ARG_PATTERN ) != inner_value.to_s.scan( ARG_PATTERN ) )
      Vocab.ui.warn( "Format string mismatch for key #{key}, quantity #{inner_key} while merging #{path}. \n English: #{@english_plurals[ key ][ inner_key ]} \n Translation: #{new_value[ inner_key ]}\n\n" )
    end
  end
end

#plural_keysObject



91
92
93
# File 'lib/vocab/merger/android.rb', line 91

def plural_keys
  return Vocab::Translator::Android.plural_keys( @locales_dir )
end

#plurals(path) ⇒ Object



30
31
32
33
34
35
# File 'lib/vocab/merger/android.rb', line 30

def plurals( path )
  keys = plural_keys
  current = current_plurals_for_locale( path )
  updates = update_plurals_for_locale( path )
  return translation_hash( keys, current, updates, path, :plural_format_changed? )
end

#string_format_changed?(key, new_value, path) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
# File 'lib/vocab/merger/android.rb', line 80

def string_format_changed?( key, new_value, path )
  if ( @english_strings[ key ].to_s.scan( FORMAT_PATTERN ) != new_value.to_s.scan( FORMAT_PATTERN ) ) ||
     ( @english_strings[ key ].to_s.scan( ARG_PATTERN ) != new_value.to_s.scan( ARG_PATTERN ) )
    Vocab.ui.warn( "Format string mismatch for key #{key} while merging #{path}. \n English: #{@english_strings[ key ]} \n Translation: #{new_value}\n\n" )
  end
end

#string_keysObject



87
88
89
# File 'lib/vocab/merger/android.rb', line 87

def string_keys
  return Vocab::Translator::Android.string_keys( @locales_dir )
end

#strings(path) ⇒ Object



23
24
25
26
27
28
# File 'lib/vocab/merger/android.rb', line 23

def strings( path )
  keys = string_keys
  current = current_strings_for_locale( path )
  updates = update_strings_for_locale( path )
  return translation_hash( keys, current, updates, path, :string_format_changed? )
end

#translation_hash(keys, current, updates, path, format_checker = :string_format_changed?) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/vocab/merger/android.rb', line 37

def translation_hash( keys, current, updates, path, format_checker = :string_format_changed? )
  translation = {}
  keys.each do |key|
    next if Vocab::Translator::Base.ignore_key?( key )

    value = updates[ key ] || current[ key ]
    if value
      translation[ key ] = value
      check_matching_format_strings( key, value, path, format_checker )
    else
      Vocab.ui.warn( "No translation found for key #{key} while merging #{path}" )
    end
  end

  return translation
end

#translation_localesObject



113
114
115
# File 'lib/vocab/merger/android.rb', line 113

def translation_locales
  return Vocab::Translator::Android.locales( @updates_dir, false )
end

#update_file_path(path) ⇒ Object



123
124
125
126
127
128
129
# File 'lib/vocab/merger/android.rb', line 123

def update_file_path( path )
  name = path.gsub( "#{@locales_dir}/", '' )
  dirname = File.dirname( name )
  entries = Dir.glob( "#{updates_dir}/#{dirname}/*.xml" )
  Vocab.ui.warn( "Multiple update files for #{path}: #{entries.join( ',' )}" ) if entries.size > 1
  return entries.first
end

#update_plurals_for_locale(path) ⇒ Object



108
109
110
111
# File 'lib/vocab/merger/android.rb', line 108

def update_plurals_for_locale( path )
  update = update_file_path( path )
  return Vocab::Translator::Android.plurals_from_xml( update )
end

#update_strings_for_locale(path) ⇒ Object



99
100
101
102
# File 'lib/vocab/merger/android.rb', line 99

def update_strings_for_locale( path )
  update = update_file_path( path )
  return Vocab::Translator::Android.hash_from_xml( update )
end