Moved heavily redundant file opening logic (necessary to read string files) from formatters to Runner and unified the encoding handling.
This commit is contained in:
parent
7124b36b2d
commit
9e7e51e4c6
21 changed files with 255 additions and 275 deletions
|
@ -83,7 +83,7 @@ module Twine
|
|||
opts.on('-c', '--consume-comments', 'Normally, when consuming a string file, Twine will ignore all comments in the file. With this flag set, any comments encountered will be read and parsed into the strings data file. This is especially useful when creating your first strings data file from an existing project.') do |c|
|
||||
options[:consume_comments] = true
|
||||
end
|
||||
opts.on('-e', '--encoding ENCODING', 'Twine defaults to encoding all output files in UTF-8. This flag will tell Twine to use an alternate encoding for these files. For example, you could use this to write Apple .strings files in UTF-16. This flag is currently only supported in Ruby 1.9.3 or greater.') do |e|
|
||||
opts.on('-e', '--encoding ENCODING', 'Twine defaults to encoding all output files in UTF-8. This flag will tell Twine to use an alternate encoding for these files. For example, you could use this to write Apple .strings files in UTF-16. When reading files, Twine does its best to determine the encoding automatically. However, if the files are UTF-16 without BOM, you need to specify if it\'s UTF-16LE or UTF16-BE.') do |e|
|
||||
options[:output_encoding] = e
|
||||
end
|
||||
opts.on('--validate', 'Validate the strings file before formatting it') do
|
||||
|
|
|
@ -1,20 +1,22 @@
|
|||
module Twine
|
||||
module Encoding
|
||||
def self.encoding_for_path path
|
||||
File.open(path, 'rb') do |f|
|
||||
begin
|
||||
a = f.readbyte
|
||||
b = f.readbyte
|
||||
if (a == 0xfe && b == 0xff)
|
||||
return 'UTF-16BE'
|
||||
elsif (a == 0xff && b == 0xfe)
|
||||
return 'UTF-16LE'
|
||||
end
|
||||
rescue EOFError
|
||||
end
|
||||
end
|
||||
|
||||
'UTF-8'
|
||||
def self.bom(path)
|
||||
first_bytes = IO.binread(path, 2)
|
||||
return nil unless first_bytes
|
||||
first_bytes = first_bytes.codepoints.map.to_a
|
||||
return 'UTF-16BE' if first_bytes == [0xFE, 0xFF]
|
||||
return 'UTF-16LE' if first_bytes == [0xFF, 0xFE]
|
||||
rescue EOFError
|
||||
return nil
|
||||
end
|
||||
|
||||
def self.has_bom?(path)
|
||||
!bom(path).nil?
|
||||
end
|
||||
|
||||
def self.encoding_for_path(path)
|
||||
bom(path) || 'UTF-8'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -83,8 +83,8 @@ module Twine
|
|||
lang
|
||||
end
|
||||
|
||||
def read_file(path, lang)
|
||||
raise NotImplementedError.new("You must implement read_file in your formatter class.")
|
||||
def read(io, lang)
|
||||
raise NotImplementedError.new("You must implement read in your formatter class.")
|
||||
end
|
||||
|
||||
def format_file(lang)
|
||||
|
|
|
@ -68,7 +68,7 @@ module Twine
|
|||
super(key, lang, value)
|
||||
end
|
||||
|
||||
def read_file(path, lang)
|
||||
def read(io, lang)
|
||||
resources_regex = /<resources(?:[^>]*)>(.*)<\/resources>/m
|
||||
key_regex = /<string name="(\w+)">/
|
||||
comment_regex = /<!-- (.*) -->/
|
||||
|
@ -77,27 +77,25 @@ module Twine
|
|||
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)
|
||||
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]
|
||||
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
|
||||
|
|
|
@ -35,56 +35,26 @@ module Twine
|
|||
"#{lang}.lproj"
|
||||
end
|
||||
|
||||
def read_file(path, lang)
|
||||
encoding = Twine::Encoding.encoding_for_path(path)
|
||||
sep = nil
|
||||
if !encoding.respond_to?(:encode)
|
||||
# This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
|
||||
if encoding.end_with? 'LE'
|
||||
sep = "\x0a\x00"
|
||||
elsif encoding.end_with? 'BE'
|
||||
sep = "\x00\x0a"
|
||||
else
|
||||
sep = "\n"
|
||||
def read(io, lang)
|
||||
last_comment = nil
|
||||
while line = io.gets
|
||||
match = /"((?:[^"\\]|\\.)+)"\s*=\s*"((?:[^"\\]|\\.)*)"/.match(line)
|
||||
if match
|
||||
key = match[1]
|
||||
key.gsub!('\\"', '"')
|
||||
value = match[2]
|
||||
value.gsub!('\\"', '"')
|
||||
set_translation_for_key(key, lang, value)
|
||||
if last_comment
|
||||
set_comment_for_key(key, last_comment)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if encoding.index('UTF-16')
|
||||
mode = "rb:#{encoding}"
|
||||
else
|
||||
mode = "r:#{encoding}"
|
||||
end
|
||||
|
||||
File.open(path, mode) do |f|
|
||||
last_comment = nil
|
||||
while line = (sep) ? f.gets(sep) : f.gets
|
||||
if encoding.index('UTF-16')
|
||||
if line.respond_to? :encode!
|
||||
line.encode!('UTF-8')
|
||||
else
|
||||
require 'iconv'
|
||||
line = Iconv.iconv('UTF-8', encoding, line).join
|
||||
end
|
||||
end
|
||||
match = /"((?:[^"\\]|\\.)+)"\s*=\s*"((?:[^"\\]|\\.)*)"/.match(line)
|
||||
if match
|
||||
key = match[1]
|
||||
key.gsub!('\\"', '"')
|
||||
value = match[2]
|
||||
value.gsub!('\\"', '"')
|
||||
set_translation_for_key(key, lang, value)
|
||||
if last_comment
|
||||
set_comment_for_key(key, last_comment)
|
||||
end
|
||||
end
|
||||
|
||||
match = /\/\* (.*) \*\//.match(line)
|
||||
if match
|
||||
last_comment = match[1]
|
||||
else
|
||||
last_comment = nil
|
||||
end
|
||||
|
||||
match = /\/\* (.*) \*\//.match(line)
|
||||
if match
|
||||
last_comment = match[1]
|
||||
else
|
||||
last_comment = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,67 +29,35 @@ module Twine
|
|||
return
|
||||
end
|
||||
|
||||
def read_file(path, lang)
|
||||
def read(io, lang)
|
||||
comment_regex = /#\. *"?(.*)"?$/
|
||||
key_regex = /msgid *"(.*)"$/
|
||||
value_regex = /msgstr *"(.*)"$/m
|
||||
|
||||
encoding = Twine::Encoding.encoding_for_path(path)
|
||||
sep = nil
|
||||
if !encoding.respond_to?(:encode)
|
||||
# This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
|
||||
if encoding.end_with? 'LE'
|
||||
sep = "\x0a\x00"
|
||||
elsif encoding.end_with? 'BE'
|
||||
sep = "\x00\x0a"
|
||||
else
|
||||
sep = "\n"
|
||||
last_comment = nil
|
||||
while line = io.gets
|
||||
comment_match = comment_regex.match(line)
|
||||
if comment_match
|
||||
comment = comment_match[1]
|
||||
end
|
||||
end
|
||||
|
||||
if encoding.index('UTF-16')
|
||||
mode = "rb:#{encoding}"
|
||||
else
|
||||
mode = "r:#{encoding}"
|
||||
end
|
||||
key_match = key_regex.match(line)
|
||||
if key_match
|
||||
key = key_match[1].gsub('\\"', '"')
|
||||
end
|
||||
value_match = value_regex.match(line)
|
||||
if value_match
|
||||
value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
|
||||
end
|
||||
|
||||
File.open(path, mode) do |f|
|
||||
last_comment = nil
|
||||
while line = (sep) ? f.gets(sep) : f.gets
|
||||
if encoding.index('UTF-16')
|
||||
if line.respond_to? :encode!
|
||||
line.encode!('UTF-8')
|
||||
else
|
||||
require 'iconv'
|
||||
line = Iconv.iconv('UTF-8', encoding, line).join
|
||||
end
|
||||
if key and key.length > 0 and value and value.length > 0
|
||||
set_translation_for_key(key, lang, value)
|
||||
if comment and comment.length > 0 and !comment.start_with?("--------- ")
|
||||
set_comment_for_key(key, comment)
|
||||
end
|
||||
|
||||
comment_match = comment_regex.match(line)
|
||||
if comment_match
|
||||
comment = comment_match[1]
|
||||
end
|
||||
|
||||
key_match = key_regex.match(line)
|
||||
if key_match
|
||||
key = key_match[1].gsub('\\"', '"')
|
||||
end
|
||||
value_match = value_regex.match(line)
|
||||
if value_match
|
||||
value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
|
||||
end
|
||||
|
||||
|
||||
if key and key.length > 0 and value and value.length > 0
|
||||
set_translation_for_key(key, lang, value)
|
||||
if comment and comment.length > 0 and !comment.start_with?("--------- ")
|
||||
set_comment_for_key(key, comment)
|
||||
end
|
||||
key = nil
|
||||
value = nil
|
||||
comment = nil
|
||||
end
|
||||
|
||||
key = nil
|
||||
value = nil
|
||||
comment = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -21,55 +21,25 @@ module Twine
|
|||
return
|
||||
end
|
||||
|
||||
def read_file(path, lang)
|
||||
encoding = Twine::Encoding.encoding_for_path(path)
|
||||
sep = nil
|
||||
if !encoding.respond_to?(:encode)
|
||||
# This code is not necessary in 1.9.3 and does not work as it did in 1.8.7.
|
||||
if encoding.end_with? 'LE'
|
||||
sep = "\x0a\x00"
|
||||
elsif encoding.end_with? 'BE'
|
||||
sep = "\x00\x0a"
|
||||
else
|
||||
sep = "\n"
|
||||
def read(io, lang)
|
||||
last_comment = nil
|
||||
while line = io.gets
|
||||
match = /((?:[^"\\]|\\.)+)\s*=\s*((?:[^"\\]|\\.)*)/.match(line)
|
||||
if match
|
||||
key = match[1]
|
||||
value = match[2].strip
|
||||
value.gsub!(/\{[0-9]\}/, '%@')
|
||||
set_translation_for_key(key, lang, value)
|
||||
if last_comment
|
||||
set_comment_for_key(key, last_comment)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if encoding.index('UTF-16')
|
||||
mode = "rb:#{encoding}"
|
||||
else
|
||||
mode = "r:#{encoding}"
|
||||
end
|
||||
|
||||
File.open(path, mode) do |f|
|
||||
last_comment = nil
|
||||
while line = (sep) ? f.gets(sep) : f.gets
|
||||
if encoding.index('UTF-16')
|
||||
if line.respond_to? :encode!
|
||||
line.encode!('UTF-8')
|
||||
else
|
||||
require 'iconv'
|
||||
line = Iconv.iconv('UTF-8', encoding, line).join
|
||||
end
|
||||
end
|
||||
match = /((?:[^"\\]|\\.)+)\s*=\s*((?:[^"\\]|\\.)*)/.match(line)
|
||||
if match
|
||||
key = match[1]
|
||||
value = match[2].strip
|
||||
value.gsub!(/\{[0-9]\}/, '%@')
|
||||
set_translation_for_key(key, lang, value)
|
||||
if last_comment
|
||||
set_comment_for_key(key, last_comment)
|
||||
end
|
||||
end
|
||||
|
||||
match = /# *(.*)/.match(line)
|
||||
if match
|
||||
last_comment = match[1]
|
||||
else
|
||||
last_comment = nil
|
||||
end
|
||||
|
||||
|
||||
match = /# *(.*)/.match(line)
|
||||
if match
|
||||
last_comment = match[1]
|
||||
else
|
||||
last_comment = nil
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -31,35 +31,34 @@ module Twine
|
|||
return
|
||||
end
|
||||
|
||||
def read_file(path, lang)
|
||||
def read(io, lang)
|
||||
comment_regex = /#.? *"(.*)"$/
|
||||
key_regex = /msgctxt *"(.*)"$/
|
||||
value_regex = /msgstr *"(.*)"$/m
|
||||
File.open(path, 'r:UTF-8') do |f|
|
||||
while item = f.gets("\n\n")
|
||||
key = nil
|
||||
value = nil
|
||||
comment = nil
|
||||
|
||||
while item = io.gets("\n\n")
|
||||
key = nil
|
||||
value = nil
|
||||
comment = nil
|
||||
|
||||
comment_match = comment_regex.match(item)
|
||||
if comment_match
|
||||
comment = comment_match[1]
|
||||
end
|
||||
key_match = key_regex.match(item)
|
||||
if key_match
|
||||
key = key_match[1].gsub('\\"', '"')
|
||||
end
|
||||
value_match = value_regex.match(item)
|
||||
if value_match
|
||||
value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
|
||||
end
|
||||
if key and key.length > 0 and value and value.length > 0
|
||||
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
|
||||
comment_match = comment_regex.match(item)
|
||||
if comment_match
|
||||
comment = comment_match[1]
|
||||
end
|
||||
key_match = key_regex.match(item)
|
||||
if key_match
|
||||
key = key_match[1].gsub('\\"', '"')
|
||||
end
|
||||
value_match = value_regex.match(item)
|
||||
if value_match
|
||||
value = value_match[1].gsub(/"\n"/, '').gsub('\\"', '"')
|
||||
end
|
||||
if key and key.length > 0 and value and value.length > 0
|
||||
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
|
||||
end
|
||||
end
|
||||
|
|
|
@ -29,18 +29,16 @@ module Twine
|
|||
return
|
||||
end
|
||||
|
||||
def read_file(path, lang)
|
||||
def read(io, lang)
|
||||
begin
|
||||
require "json"
|
||||
rescue LoadError
|
||||
raise Twine::Error.new "You must run 'gem install json' in order to read or write jquery-localize files."
|
||||
end
|
||||
|
||||
open(path) do |io|
|
||||
json = JSON.load(io)
|
||||
json.each do |key, value|
|
||||
set_translation_for_key(key, lang, value)
|
||||
end
|
||||
json = JSON.load(io)
|
||||
json.each do |key, value|
|
||||
set_translation_for_key(key, lang, value)
|
||||
end
|
||||
end
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ module Twine
|
|||
return
|
||||
end
|
||||
|
||||
def read_file(path, lang)
|
||||
def read(io, lang)
|
||||
resources_regex = /<resources(?:[^>]*)>(.*)<\/resources>/m
|
||||
key_regex = /<string name="(\w+)">/
|
||||
comment_regex = /<!-- (.*) -->/
|
||||
|
@ -58,35 +58,33 @@ module Twine
|
|||
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
|
||||
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)
|
||||
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
|
||||
comment_match = comment_regex.match(line)
|
||||
if comment_match
|
||||
comment = comment_match[1]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -295,7 +295,13 @@ module Twine
|
|||
end
|
||||
|
||||
formatter, lang = prepare_read_write(path, lang)
|
||||
formatter.read_file(path, lang)
|
||||
|
||||
encoding = @options[:encoding] || Twine::Encoding.encoding_for_path(path)
|
||||
|
||||
IO.open(IO.sysopen(path, 'rb'), 'rb', external_encoding: encoding, internal_encoding: 'UTF-8') do |io|
|
||||
io.read(2) if Twine::Encoding.has_bom?(path)
|
||||
formatter.read(io, lang)
|
||||
end
|
||||
end
|
||||
|
||||
def prepare_read_write(path, lang)
|
||||
|
|
BIN
test/fixtures/enc_utf16be.dummy
vendored
Normal file
BIN
test/fixtures/enc_utf16be.dummy
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/enc_utf16be_bom.dummy
vendored
Normal file
BIN
test/fixtures/enc_utf16be_bom.dummy
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/enc_utf16le.dummy
vendored
Normal file
BIN
test/fixtures/enc_utf16le.dummy
vendored
Normal file
Binary file not shown.
BIN
test/fixtures/enc_utf16le_bom.dummy
vendored
Normal file
BIN
test/fixtures/enc_utf16le_bom.dummy
vendored
Normal file
Binary file not shown.
2
test/fixtures/enc_utf8.dummy
vendored
Normal file
2
test/fixtures/enc_utf8.dummy
vendored
Normal file
|
@ -0,0 +1,2 @@
|
|||
Üß`
|
||||
da
|
|
@ -5,7 +5,7 @@ class TestConsumeLocDrop < CommandTestCase
|
|||
super
|
||||
|
||||
options = {}
|
||||
options[:input_path] = fixture 'consume_loc_drop.zip'
|
||||
options[:input_path] = fixture_path 'consume_loc_drop.zip'
|
||||
options[:output_path] = @output_path
|
||||
options[:format] = 'apple'
|
||||
|
||||
|
|
|
@ -14,31 +14,31 @@ class TestConsumeStringFile < CommandTestCase
|
|||
Twine::Runner.new(options, @strings)
|
||||
end
|
||||
|
||||
def prepare_mock_read_file_formatter(formatter_class)
|
||||
def prepare_mock_read_formatter(formatter_class)
|
||||
formatter = prepare_mock_formatter(formatter_class)
|
||||
formatter.expects(:read_file)
|
||||
formatter.expects(:read)
|
||||
end
|
||||
|
||||
def test_deducts_android_format_from_output_path
|
||||
prepare_mock_read_file_formatter Twine::Formatters::Android
|
||||
prepare_mock_read_formatter Twine::Formatters::Android
|
||||
|
||||
new_runner('fr', 'fr.xml').consume_string_file
|
||||
end
|
||||
|
||||
def test_deducts_apple_format_from_output_path
|
||||
prepare_mock_read_file_formatter Twine::Formatters::Apple
|
||||
prepare_mock_read_formatter Twine::Formatters::Apple
|
||||
|
||||
new_runner('fr', 'fr.strings').consume_string_file
|
||||
end
|
||||
|
||||
def test_deducts_jquery_format_from_output_path
|
||||
prepare_mock_read_file_formatter Twine::Formatters::JQuery
|
||||
prepare_mock_read_formatter Twine::Formatters::JQuery
|
||||
|
||||
new_runner('fr', 'fr.json').consume_string_file
|
||||
end
|
||||
|
||||
def test_deducts_gettext_format_from_output_path
|
||||
prepare_mock_read_file_formatter Twine::Formatters::Gettext
|
||||
prepare_mock_read_formatter Twine::Formatters::Gettext
|
||||
|
||||
new_runner('fr', 'fr.po').consume_string_file
|
||||
end
|
||||
|
@ -46,8 +46,74 @@ class TestConsumeStringFile < CommandTestCase
|
|||
def test_deducts_language_from_input_path
|
||||
random_language = KNOWN_LANGUAGES.sample
|
||||
formatter = prepare_mock_formatter Twine::Formatters::Android
|
||||
formatter.expects(:read_file).with(anything, random_language)
|
||||
formatter.expects(:read).with(anything, random_language)
|
||||
|
||||
new_runner(nil, "#{random_language}.xml").consume_string_file
|
||||
end
|
||||
|
||||
class TestEncodings < CommandTestCase
|
||||
class DummyFormatter < Twine::Formatters::Abstract
|
||||
attr_reader :content
|
||||
|
||||
def extension
|
||||
'.dummy'
|
||||
end
|
||||
|
||||
def format_name
|
||||
'dummy'
|
||||
end
|
||||
|
||||
def read(io, lang)
|
||||
@content = io.read
|
||||
end
|
||||
end
|
||||
|
||||
def new_runner(input_path, encoding = nil)
|
||||
options = {}
|
||||
options[:output_path] = @output_path
|
||||
options[:input_path] = input_path
|
||||
options[:encoding] = encoding if encoding
|
||||
options[:languages] = 'en'
|
||||
|
||||
@strings = Twine::StringsFile.new
|
||||
@strings.language_codes.concat KNOWN_LANGUAGES
|
||||
|
||||
Twine::Runner.new(options, @strings)
|
||||
end
|
||||
|
||||
def setup
|
||||
super
|
||||
@expected_content = "Üß`\nda\n"
|
||||
end
|
||||
|
||||
def test_reads_utf8
|
||||
formatter = prepare_mock_formatter DummyFormatter
|
||||
new_runner(fixture_path('enc_utf8.dummy')).consume_string_file
|
||||
assert_equal @expected_content, formatter.content
|
||||
end
|
||||
|
||||
def test_reads_utf16le_bom
|
||||
formatter = prepare_mock_formatter DummyFormatter
|
||||
new_runner(fixture_path('enc_utf16le_bom.dummy')).consume_string_file
|
||||
assert_equal @expected_content, formatter.content
|
||||
end
|
||||
|
||||
def test_reads_utf16be_bom
|
||||
formatter = prepare_mock_formatter DummyFormatter
|
||||
new_runner(fixture_path('enc_utf16be_bom.dummy')).consume_string_file
|
||||
assert_equal @expected_content, formatter.content
|
||||
end
|
||||
|
||||
def test_reads_utf16le
|
||||
formatter = prepare_mock_formatter DummyFormatter
|
||||
new_runner(fixture_path('enc_utf16le.dummy'), 'UTF-16LE').consume_string_file
|
||||
assert_equal @expected_content, formatter.content
|
||||
end
|
||||
|
||||
def test_reads_utf16be
|
||||
formatter = prepare_mock_formatter DummyFormatter
|
||||
new_runner(fixture_path('enc_utf16be.dummy'), 'UTF-16BE').consume_string_file
|
||||
assert_equal @expected_content, formatter.content
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -41,8 +41,8 @@ class TestAndroidFormatter < FormatterTest
|
|||
super Twine::Formatters::Android
|
||||
end
|
||||
|
||||
def test_read_file_format
|
||||
@formatter.read_file fixture('formatter_android.xml'), 'en'
|
||||
def test_read_format
|
||||
@formatter.read content_io('formatter_android.xml'), 'en'
|
||||
|
||||
assert_file_contents_read_correctly
|
||||
end
|
||||
|
@ -131,8 +131,8 @@ class TestAppleFormatter < FormatterTest
|
|||
super Twine::Formatters::Apple
|
||||
end
|
||||
|
||||
def test_read_file_format
|
||||
@formatter.read_file fixture('formatter_apple.strings'), 'en'
|
||||
def test_read_format
|
||||
@formatter.read content_io('formatter_apple.strings'), 'en'
|
||||
|
||||
assert_file_contents_read_correctly
|
||||
end
|
||||
|
@ -162,8 +162,8 @@ class TestJQueryFormatter < FormatterTest
|
|||
super Twine::Formatters::JQuery
|
||||
end
|
||||
|
||||
def test_read_file_format
|
||||
@formatter.read_file fixture('formatter_jquery.json'), 'en'
|
||||
def test_read_format
|
||||
@formatter.read content_io('formatter_jquery.json'), 'en'
|
||||
|
||||
assert_translations_read_correctly
|
||||
end
|
||||
|
@ -185,14 +185,14 @@ class TestGettextFormatter < FormatterTest
|
|||
super Twine::Formatters::Gettext
|
||||
end
|
||||
|
||||
def test_read_file_format
|
||||
@formatter.read_file fixture('formatter_gettext.po'), 'en'
|
||||
def test_read_format
|
||||
@formatter.read content_io('formatter_gettext.po'), 'en'
|
||||
|
||||
assert_file_contents_read_correctly
|
||||
end
|
||||
|
||||
def test_read_file_with_multiple_line_value
|
||||
@formatter.read_file fixture('gettext_multiline.po'), 'en'
|
||||
def test_read_with_multiple_line_value
|
||||
@formatter.read content_io('gettext_multiline.po'), 'en'
|
||||
|
||||
assert_equal 'multiline\nstring', @strings.strings_map['key1'].translations['en']
|
||||
end
|
||||
|
@ -211,9 +211,9 @@ class TestTizenFormatter < FormatterTest
|
|||
super Twine::Formatters::Tizen
|
||||
end
|
||||
|
||||
def test_read_file_format
|
||||
skip 'the current implementation of Tizen formatter does not support read_file'
|
||||
@formatter.read_file fixture('formatter_tizen.xml'), 'en'
|
||||
def test_read_format
|
||||
skip 'the current implementation of Tizen formatter does not support reading'
|
||||
@formatter.read content_io('formatter_tizen.xml'), 'en'
|
||||
|
||||
assert_file_contents_read_correctly
|
||||
end
|
||||
|
@ -231,8 +231,8 @@ class TestDjangoFormatter < FormatterTest
|
|||
super Twine::Formatters::Django
|
||||
end
|
||||
|
||||
def test_read_file_format
|
||||
@formatter.read_file fixture('formatter_django.po'), 'en'
|
||||
def test_read_format
|
||||
@formatter.read content_io('formatter_django.po'), 'en'
|
||||
|
||||
assert_file_contents_read_correctly
|
||||
end
|
||||
|
@ -249,8 +249,8 @@ class TestFlashFormatter < FormatterTest
|
|||
super Twine::Formatters::Flash
|
||||
end
|
||||
|
||||
def test_read_file_format
|
||||
@formatter.read_file fixture('formatter_flash.properties'), 'en'
|
||||
def test_read_format
|
||||
@formatter.read content_io('formatter_flash.properties'), 'en'
|
||||
|
||||
assert_file_contents_read_correctly
|
||||
end
|
||||
|
|
|
@ -6,7 +6,7 @@ class TestStringsFile < TwineTestCase
|
|||
super
|
||||
|
||||
@strings = Twine::StringsFile.new
|
||||
@strings.read fixture('twine_accent_values.txt')
|
||||
@strings.read fixture_path('twine_accent_values.txt')
|
||||
end
|
||||
|
||||
def test_reading_keeps_leading_accent
|
||||
|
|
|
@ -34,12 +34,15 @@ class TwineTestCase < Minitest::Test
|
|||
Twine::Runner.run(command.split(" "))
|
||||
end
|
||||
|
||||
def fixture(filename)
|
||||
def fixture_path(filename)
|
||||
File.join File.dirname(__FILE__), 'fixtures', filename
|
||||
end
|
||||
alias :f :fixture
|
||||
|
||||
def content(filename)
|
||||
ERB.new(File.read fixture(filename)).result
|
||||
ERB.new(File.read fixture_path(filename)).result
|
||||
end
|
||||
|
||||
def content_io(filename)
|
||||
StringIO.new ERB.new(File.read fixture_path(filename)).result
|
||||
end
|
||||
end
|
||||
|
|
Reference in a new issue