Skip to content

Commit

Permalink
allow mixed tree of translation and structure for .properties
Browse files Browse the repository at this point in the history
  • Loading branch information
pejuko committed Mar 19, 2011
1 parent b7fa286 commit 379f338
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
14 changes: 10 additions & 4 deletions lib/i18n/processor/properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ class Properties < Template
def import(data)
hash = {}

sep = @translate.options[:separator]
plus_key = sep+"translation"
key = nil
value = nil
status = :first
Expand All @@ -42,20 +44,20 @@ def import(data)
# end of continuous string
elsif line[%r{^(?:#{WHITE_SPACE})*#{VALUE_END}$}] and (status == :inside)
value << $1.to_s.strip
I18n::Translate.set(uninspect(key), uninspect(value), hash, @translate.options[:separator])
I18n::Translate.set(uninspect(key)+plus_key, uninspect(value), hash, sep)
value = nil
status = :first

# simple key = value
elsif line[%r{#{KEY}#{ASSIGN}#{VALUE}}]
key, value = $1.to_s.strip, $2.to_s.strip
I18n::Translate.set(uninspect(key), uninspect(value), hash, @translate.options[:separator])
I18n::Translate.set(uninspect(key)+plus_key, uninspect(value), hash, sep)

# empty key
elsif line[/#{KEY}\s*/]
key = $1.to_s.strip
value = ""
I18n::Translate.set(uninspect(key), uninspect(value), hash, @translate.options[:separator])
I18n::Translate.set(uninspect(key)+plus_key, uninspect(value), hash, sep)
else
puts "*** not match: '#{line}'"
end
Expand All @@ -66,6 +68,7 @@ def import(data)

# this export ignores data
def export(data)
sep = @translate.options[:separator]
target = data[@translate.lang]
str = ""
keys = I18n::Translate.hash_to_keys(target).sort
Expand All @@ -82,8 +85,11 @@ def export(data)
entry = value["translation"].to_s
end

k = key
k = $1 if k =~ /(.*)#{Regexp.escape sep}translation$/

# create record in format: key = value
str << key.gsub(/( |:|=)/){|m| "\\#{m}"} << " = " << entry.gsub("\n", "\\n") << "\n"
str << k.gsub(/( |:|=)/){|m| "\\#{m}"} << " = " << entry.gsub("\n", "\\n") << "\n"
end

str
Expand Down
2 changes: 1 addition & 1 deletion test/tc_processor_properties.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def test_0010_read
data = data['cze']
str = "Interpolovaný text '%{var}'"
#diff(str, data["extended"]["interpolation"])
assert_equal( str, data["extended"]["interpolation"] )
assert_equal( str, data["extended"]["interpolation"]["translation"] )
end

def test_0020_write
Expand Down

0 comments on commit 379f338

Please sign in to comment.