Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
...
Checking mergeability… Don't worry, you can still create the pull request.
  • 10 commits
  • 5 files changed
  • 0 commit comments
  • 2 contributors
View
2  README.md
@@ -77,6 +77,7 @@ Twine currently supports the following formats for outputting strings:
* [Gettext PO Files][gettextpo] (format: gettext)
* [jquery-localize Language Files][jquerylocalize] (format: jquery)
* [Django PO Files][djangopo] (format: django)
+* [Tizen String Resources][https://developer.tizen.org/documentation/articles/localization] (format: tizen)
If you would like to enable twine to create language files in another format, create an appropriate formatter in `lib/twine/formatters`.
@@ -174,6 +175,7 @@ Many thanks to all of the contributors to the Twine project, including:
* [Kevin Wood](https://github.com/kwood)
* [Mohammad Hejazi](https://github.com/MohammadHejazi)
* [Robert Guo](http://www.robertguo.me/)
+* [Sergey Pisarchik](https://github.com/SergeyPisarchik)
* [Shai Shamir](https://github.com/pichirichi)
View
3  lib/twine/formatters.rb
@@ -5,9 +5,10 @@
require 'twine/formatters/gettext'
require 'twine/formatters/jquery'
require 'twine/formatters/django'
+require 'twine/formatters/tizen'
module Twine
module Formatters
- FORMATTERS = [Formatters::Apple, Formatters::Android, Formatters::Gettext, Formatters::JQuery, Formatters::Flash, Formatters::Django]
+ FORMATTERS = [Formatters::Apple, Formatters::Android, Formatters::Gettext, Formatters::JQuery, Formatters::Flash, Formatters::Django, Formatters::Tizen]
end
end
View
175 lib/twine/formatters/tizen.rb
@@ -0,0 +1,175 @@
+# encoding: utf-8
+require 'cgi'
+require 'rexml/document'
+
+module Twine
+ module Formatters
+ class Tizen < Abstract
+ FORMAT_NAME = 'tizen'
+ EXTENSION = '.xml'
+ DEFAULT_FILE_NAME = 'strings.xml'
+ 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'
+ ]
+ DEFAULT_LANG_CODES = Hash[
+ ]
+
+ def self.can_handle_directory?(path)
+ Dir.entries(path).any? { |item| /^values.*$/.match(item) }
+ end
+
+ def default_file_name
+ return DEFAULT_FILE_NAME
+ end
+
+ def write_all_files(path)
+ if !File.directory?(path)
+ raise Twine::Error.new("Directory does not exist: #{path}")
+ end
+
+ langs_written = []
+ Dir.foreach(path) do |item|
+ if item == "." or item == ".."
+ next
+ end
+ item = File.join(path, item)
+ if !File.directory?(item)
+ lang = determine_language_given_path(item)
+ if lang
+ write_file(item, lang)
+ langs_written << lang
+ end
+ end
+ end
+ if langs_written.empty?
+ raise Twine::Error.new("Failed to genertate any files: No languages found at #{path}")
+ end
+ end
+
+ 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
+
+ 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)
+ 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
+
+ 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<!-- Tizen Strings File -->\n<!-- Generated by Twine #{Twine::VERSION} -->\n<!-- Language: #{lang} -->"
+ f.write '<string_table Bversion="2.0.0.201311071819" Dversion="20120315">'
+ @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 Tizen handle the defaulting
+ value = String.new(value) # use a copy to prevent modifying the original
+
+ # Tizen 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)
+ # 4) replace beginning and end spaces with \0020. Otherwise Tizen strips them.
+ value.gsub!(/\A *| *\z/) { |spaces| '\u0020' * spaces.length }
+
+ comment = row.comment
+ if comment
+ comment = comment.gsub('--', '')
+ end
+
+ if comment && comment.length > 0
+ f.puts "\t<!-- #{comment} -->\n"
+ end
+ f.puts "\t<text id=\"IDS_#{key.upcase}\">#{value}</text>"
+ end
+ end
+ end
+ end
+
+ f.puts '</string_table>'
+ end
+ end
+ end
+ end
+end
View
12 test/fixtures/test-output-12.txt
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!-- Tizen Strings File -->
+<!-- Generated by Twine <%= Twine::VERSION %> -->
+<!-- Language: fr -->
+<string_table Bversion="2.0.0.201311071819" Dversion="20120315">
+ <!-- SECTION: My Strings -->
+ <!-- This is a comment -->
+ <text id="IDS_KEY1">key1-french</text>
+ <text id="IDS_KEY2">key2-french</text>
+ <text id="IDS_KEY3">key3-english</text>
+ <text id="IDS_KEY4">key4-english</text>
+</string_table>
View
8 test/twine_test.rb
@@ -60,6 +60,14 @@ def test_generate_string_file_7
end
end
+ def test_generate_string_file_8
+ Dir.mktmpdir do |dir|
+ output_path = File.join(dir, 'fr.xml')
+ Twine::Runner.run(%W(generate-string-file --format tizen test/fixtures/strings-1.txt #{output_path} --include-untranslated))
+ assert_equal(ERB.new(File.read('test/fixtures/test-output-12.txt')).result, File.read(output_path))
+ end
+ end
+
def test_consume_string_file_1
Dir.mktmpdir do |dir|
output_path = File.join(dir, 'strings.txt')

No commit comments for this range

Something went wrong with that request. Please try again.