Class: Twine::Formatters::Android

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

Constant Summary collapse

FORMAT_NAME =
'android'
EXTENSION =
'.xml'
DEFAULT_FILE_NAME =
'strings.xml'
LANG_CODES =
Hash[
  'zh' => 'zh-Hans',
  'zh-rCN' => 'zh-Hans',
  'zh-rHK' => 'zh-Hant',
  'en-rGB' => 'en-UK',
  'in' => 'id',
  'nb' => 'no'
  # TODO: spanish
]
DEFAULT_LANG_CODES =

TODO: spanish

Hash[
  'zh-TW' => 'zh-Hant' # if we don't have a zh-TW translation, try zh-Hant before en
]

Instance Attribute Summary

Attributes inherited from Abstract

#options, #strings

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Abstract

#androidify_substitutions, #initialize, #iosify_substitutions, #set_comment_for_key, #set_translation_for_key, #write_all_files

Constructor Details

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

Class Method Details

.can_handle_directory?(path) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#default_file_nameObject



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

def default_file_name
  return DEFAULT_FILE_NAME
end

#determine_language_given_path(path) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/twine/formatters/android.rb', line 32

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
      match = /^values-(.*)$/.match(segment)
      if match
        lang = match[1]
        lang = LANG_CODES.fetch(lang, lang)
        lang.sub!('-r', '-')
        return lang
      end
    end
  end

  return
end

#read_file(path, lang) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/twine/formatters/android.rb', line 51

def read_file(path, 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

  File.open(path, 'r:UTF-8') do |f|
    content_match = resources_regex.match(f.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)
          if value_match
            value = value_match[1]
            value = CGI.unescapeHTML(value)
            value.gsub!('\\\'', '\'')
            value.gsub!('\\"', '"')
            value = iosify_substitutions(value)
          else
            value = ""
          end
          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
end

#write_file(path, lang) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/twine/formatters/android.rb', line 93

def write_file(path, lang)
  default_lang = nil
  if DEFAULT_LANG_CODES.has_key?(lang)
    default_lang = DEFAULT_LANG_CODES[lang]
  end
  File.open(path, 'w:UTF-8') do |f|
    f.puts "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<!-- Android Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->"
    f.write '<resources>'
    @strings.sections.each do |section|
      printed_section = false
      section.rows.each do |row|
        if row.matches_tags?(@options[:tags], @options[:untagged])
          if !printed_section
            f.puts ''
            if section.name && section.name.length > 0
              section_name = section.name.gsub('--', '')
              f.puts "\t<!-- SECTION: #{section_name} -->"
            end
            printed_section = true
          end

          key = row.key

          value = row.translated_string_for_lang(lang, default_lang)
          if !value && @options[:include_untranslated]
            value = row.translated_string_for_lang(@strings.language_codes[0])
          end

          if value # if values is nil, there was no appropriate translation, so let Android handle the defaulting
            value = String.new(value) # use a copy to prevent modifying the original

            # Android enforces the following rules on the values
            #  1) apostrophes and quotes must be escaped with a backslash
            value.gsub!('\'', '\\\\\'')
            value.gsub!('"', '\\\\"')
            #  2) HTML escape the string
            value = CGI.escapeHTML(value)
            #  3) fix substitutions (e.g. %s/%@)
            value = androidify_substitutions(value)

            comment = row.comment
            if comment
              comment = comment.gsub('--', '')
            end

            if comment && comment.length > 0
              f.puts "\t<!-- #{comment} -->\n"
            end
            f.puts "\t<string name=\"#{key}\">#{value}</string>"
          end
        end
      end
    end

    f.puts '</resources>'
  end
end