Add the ability to consume comments in a strings file.

This commit is contained in:
Sebastian Celis 2012-04-16 13:25:43 -05:00
parent 68b5469e46
commit 369e64d65f
4 changed files with 32 additions and 11 deletions

View file

@ -67,6 +67,9 @@ module Twine
opts.on('-d', '--developer-language LANG', 'When writing the strings data file, set the specified language as the "developer language". In practice, this just means that this language will appear first in the strings data file.') do |d|
@options[:developer_language] = d
end
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 currently only works with Apple .strings files and is currently only supported in Ruby 1.9.3 or greater.') do |e|
if !"".respond_to?(:encode)
raise Twine::Error.new "The --encoding flag is only supported on Ruby 1.9.3 or greater."

View file

@ -36,6 +36,12 @@ module Twine
end
end
def set_comment_for_key(key, comment)
if @strings.strings_map.include?(key)
@strings.strings_map[key].comment = comment
end
end
def default_file_name
raise NotImplementedError.new("You must implement default_file_name in your formatter class.")
end

View file

@ -96,7 +96,7 @@ module Twine
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!('\'', '\\\\\'')
@ -106,12 +106,12 @@ module Twine
value.gsub!('<', '&lt;')
# 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
@ -124,11 +124,11 @@ module Twine
f.puts '</resources>'
end
end
def iosify_substitutions(str)
# 1) use "@" instead of "s" for substituting strings
str.gsub!(/%([0-9\$]*)s/, '%\1@')
# 2) if substitutions are numbered, see if we can remove the numbering safely
expectedSub = 1
startFound = false
@ -156,19 +156,19 @@ module Twine
foundSub = 0
end
end
# if we got this far, then the numbering (if any) is in order left-to-right and safe to remove
if expectedSub > 1
str.gsub!(/%\d+\$(.)/, '%\1')
end
return str
end
def androidify_substitutions(str)
# 1) use "s" instead of "@" for substituting strings
str.gsub!(/%([0-9\$]*)@/, '%\1s')
# 2) if there is more than one substitution in a string, make sure they are numbered
substituteCount = 0
startFound = false
@ -187,7 +187,7 @@ module Twine
startFound = true
end
end
if substituteCount > 1
currentSub = 1
startFound = false
@ -209,7 +209,7 @@ module Twine
return str
end
end
end
end
end

View file

@ -46,6 +46,7 @@ module Twine
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!
@ -62,6 +63,17 @@ module Twine
value = match[2]
value.gsub!('\\"', '"')
set_translation_for_key(key, lang, value)
if last_comment
set_comment_for_key(key, last_comment)
end
end
if @options[:consume_comments]
match = /\/\* (.*) \*\//.match(line)
if match
last_comment = match[1]
else
last_comment = nil
end
end
end
end