Class: Twine::Formatters::Android

Inherits:
Abstract
  • Object
show all
Includes:
Placeholders
Defined in:
lib/twine/formatters/android.rb

Constant Summary collapse

LANG_MAPPINGS =
Hash[
  'zh-rCN' => 'zh-Hans',
  'zh-rHK' => 'zh-Hant',
  'en-rGB' => 'en-UK',
  'zh' => 'zh-Hans',
  'in' => 'id',
  'nb' => 'no'
  # TODO: spanish
]

Constants included from Placeholders

Placeholders::PLACEHOLDER_FLAGS_WIDTH_PRECISION_LENGTH, Placeholders::PLACEHOLDER_PARAMETER_FLAGS_WIDTH_PRECISION_LENGTH

Instance Attribute Summary

Attributes inherited from Abstract

#options, #strings

Instance Method Summary collapse

Methods included from Placeholders

#convert_placeholders_from_android_to_twine, #convert_placeholders_from_twine_to_android

Methods inherited from Abstract

#escape_quotes, #format_file, #format_key, #format_key_value, #format_row, #format_section, #initialize, #set_comment_for_key, #should_include_row

Constructor Details

This class inherits a constructor from Twine::Formatters::Abstract

Instance Method Details

#can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/twine/formatters/android.rb', line 28

def can_handle_directory?(path)
  Dir.entries(path).any? { |item| /^values.*$/.match(item) }
end

#default_file_nameObject



32
33
34
# File 'lib/twine/formatters/android.rb', line 32

def default_file_name
  return 'strings.xml'
end

#determine_language_given_path(path) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/twine/formatters/android.rb', line 36

def determine_language_given_path(path)
  path_arr = path.split(File::SEPARATOR)
  path_arr.each do |segment|
    if segment == 'values'
      return @strings.language_codes[0]
    else
      # The language is defined by a two-letter ISO 639-1 language code, optionally followed by a two letter ISO 3166-1-alpha-2 region code (preceded by lowercase "r").
      # see http://developer.android.com/guide/topics/resources/providing-resources.html#AlternativeResources
      match = /^values-([a-z]{2}(-r[a-z]{2})?)$/i.match(segment)
      if match
        lang = match[1]
        lang = LANG_MAPPINGS.fetch(lang, lang)
        lang.sub!('-r', '-')
        return lang
      end
    end
  end

  return
end

#extensionObject



24
25
26
# File 'lib/twine/formatters/android.rb', line 24

def extension
  '.xml'
end

#format_comment(row, lang) ⇒ Object



120
121
122
# File 'lib/twine/formatters/android.rb', line 120

def format_comment(row, lang)
  "\t<!-- #{row.comment.gsub('--', '')} -->\n" if row.comment
end

#format_header(lang) ⇒ Object



104
105
106
# File 'lib/twine/formatters/android.rb', line 104

def format_header(lang)
  "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Android Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->"
end

#format_nameObject



20
21
22
# File 'lib/twine/formatters/android.rb', line 20

def format_name
  'android'
end

#format_section_header(section) ⇒ Object



116
117
118
# File 'lib/twine/formatters/android.rb', line 116

def format_section_header(section)
  "\t<!-- SECTION: #{section.name} -->"
end

#format_sections(strings, lang) ⇒ Object



108
109
110
111
112
113
114
# File 'lib/twine/formatters/android.rb', line 108

def format_sections(strings, lang)
  result = '<resources>'
  
  result += super + "\n"

  result += "</resources>\n"
end

#format_value(value) ⇒ Object



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/twine/formatters/android.rb', line 128

def format_value(value)
  # Android enforces the following rules on the values
  #  1) apostrophes and quotes must be escaped with a backslash
  value = escape_quotes(value)
  value.gsub!("'", "\\\\'")
  #  2) HTML escape the string
  value = CGI.escapeHTML(value)
  #  3) convert placeholders (e.g. %@ -> %s)
  value = convert_placeholders_from_twine_to_android(value)
  #  4) escape non resource identifier @ signs (http://developer.android.com/guide/topics/resources/accessing-resources.html#ResourcesFromXml)
  resource_identifier_regex = /@(?!([a-z\.]+:)?[a-z+]+\/[a-zA-Z_]+)/   # @[<package_name>:]<resource_type>/<resource_name>
  value.gsub!(resource_identifier_regex, '\@')
  #  5) replace beginning and end spaces with \0020. Otherwise Android strips them.
  value.gsub(/\A *| *\z/) { |spaces| '\u0020' * spaces.length }
end

#key_value_patternObject



124
125
126
# File 'lib/twine/formatters/android.rb', line 124

def key_value_pattern
  "\t<string name=\"%{key}\">%{value}</string>"
end

#output_path_for_language(lang) ⇒ Object



57
58
59
# File 'lib/twine/formatters/android.rb', line 57

def output_path_for_language(lang)
  "values-" + (LANG_MAPPINGS.key(lang) || lang)
end

#read(io, lang) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/twine/formatters/android.rb', line 71

def read(io, lang)
  resources_regex = /<resources(?:[^>]*)>(.*)<\/resources>/m
  key_regex = /<string name="(\w+)">/
  comment_regex = /<!-- (.*) -->/
  value_regex = /<string name="\w+">(.*)<\/string>/
  key = nil
  value = nil
  comment = nil

  content_match = resources_regex.match(io.read)
  if content_match
    for line in content_match[1].split(/\r?\n/)
      key_match = key_regex.match(line)
      if key_match
        key = key_match[1]
        value_match = value_regex.match(line)
        value = value_match ? value_match[1] : ""
        
        set_translation_for_key(key, lang, value)
        if comment and comment.length > 0 and !comment.start_with?("SECTION:")
          set_comment_for_key(key, comment)
        end
        comment = nil
      end
      
      comment_match = comment_regex.match(line)
      if comment_match
        comment = comment_match[1]
      end
    end
  end
end

#set_translation_for_key(key, lang, value) ⇒ Object



61
62
63
64
65
66
67
68
69
# File 'lib/twine/formatters/android.rb', line 61

def set_translation_for_key(key, lang, value)
  value = CGI.unescapeHTML(value)
  value.gsub!('\\\'', '\'')
  value.gsub!('\\"', '"')
  value = convert_placeholders_from_android_to_twine(value)
  value.gsub!('\@', '@')
  value.gsub!(/(\\u0020)*|(\\u0020)*\z/) { |spaces| ' ' * (spaces.length / 6) }
  super(key, lang, value)
end