Class: Twine::Formatters::Tizen

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

Constant Summary collapse

LANG_CODES =
Hash[
  'eng-GB' => 'en',
  'rus-RU' => 'ru',
  'fra-FR' => 'fr',
  'deu-DE' => 'de',
  'spa-ES' => 'es',
  'ita-IT' => 'it',
  'ces-CZ' => 'cs',
  'pol-PL' => 'pl',
  'por-PT' => 'pt',
  'ukr-UA' => 'uk'
]

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_value, #format_row, #format_section, #initialize, #output_path_for_language, #set_comment_for_key, #set_translation_for_key, #should_include_row, #write_all_files, #write_file

Constructor Details

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

Instance Method Details

#can_handle_directory?(path) ⇒ Boolean



31
32
33
# File 'lib/twine/formatters/tizen.rb', line 31

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

#default_file_nameObject



35
36
37
# File 'lib/twine/formatters/tizen.rb', line 35

def default_file_name
  return 'strings.xml'
end

#determine_language_given_path(path) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/twine/formatters/tizen.rb', line 39

def determine_language_given_path(path)
  path_arr = path.split(File::SEPARATOR)
  path_arr.each do |segment|
    match = /^(.*-.*)\.xml$/.match(segment)
    if match
      lang = match[1]
      lang = LANG_CODES.fetch(lang, nil)
      return lang
    end
  end
  return
end

#extensionObject



27
28
29
# File 'lib/twine/formatters/tizen.rb', line 27

def extension
  '.xml'
end

#format_comment(row, lang) ⇒ Object



111
112
113
# File 'lib/twine/formatters/tizen.rb', line 111

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

#format_header(lang) ⇒ Object



95
96
97
# File 'lib/twine/formatters/tizen.rb', line 95

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

#format_key(key) ⇒ Object



119
120
121
# File 'lib/twine/formatters/tizen.rb', line 119

def format_key(key)
  key.upcase
end

#format_nameObject



23
24
25
# File 'lib/twine/formatters/tizen.rb', line 23

def format_name
  'tizen'
end

#format_section_header(section) ⇒ Object



107
108
109
# File 'lib/twine/formatters/tizen.rb', line 107

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

#format_sections(strings, lang) ⇒ Object



99
100
101
102
103
104
105
# File 'lib/twine/formatters/tizen.rb', line 99

def format_sections(strings, lang)
  result = '<string_table  Bversion="2.0.0.201311071819" Dversion="20120315">'
  
  result += super + "\n"

  result += '</string_table>'
end

#format_value(value) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/twine/formatters/tizen.rb', line 123

def format_value(value)
  value = escape_quotes(value)
  # Tizen enforces the following rules on the values
  #  1) apostrophes and quotes must be escaped with a backslash
  value.gsub!("'", "\\\\'")
  #  2) HTML escape the string
  value = CGI.escapeHTML(value)
  #  3) fix substitutions (e.g. %s/%@)
  value = convert_placeholders_from_twine_to_android(value)
  #  4) replace beginning and end spaces with \0020. Otherwise Tizen strips them.
  value.gsub(/\A *| *\z/) { |spaces| '\u0020' * spaces.length }
end

#key_value_patternObject



115
116
117
# File 'lib/twine/formatters/tizen.rb', line 115

def key_value_pattern
  "\t<text id=\"IDS_%{key}\">%{value}</text>"
end

#read_file(path, lang) ⇒ Object



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
92
93
# File 'lib/twine/formatters/tizen.rb', line 52

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 = convert_placeholders_from_android_to_twine(value)
            value.gsub!(/(\\u0020)*|(\\u0020)*\z/) { |spaces| ' ' * (spaces.length / 6) }
          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