Skip to content

Commit

Permalink
Sort translations before export
Browse files Browse the repository at this point in the history
  • Loading branch information
printercu committed Mar 19, 2014
1 parent da2888c commit 91a2174
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
24 changes: 14 additions & 10 deletions app/models/tolk/locale.rb
Expand Up @@ -50,10 +50,8 @@ def secondary_locales
all - [primary_locale]
end

def dump_all(to = self.locales_config_path, exporter = Tolk::Export)
secondary_locales.each do |locale|
exporter.dump(name: locale.name, data: locale.to_hash, destination: to)
end
def dump_all(*args)
secondary_locales.each { |locale| locale.dump(*args) }
end

def special_key_or_prefix?(prefix, key)
Expand All @@ -68,6 +66,10 @@ def pluralization_data?(data)
end
end

def dump(to = self.locales_config_path, exporter = Tolk::Export)
exporter.dump(name: name, data: to_hash, destination: to)
end

def has_updated_translations?
translations.count(:conditions => {:'tolk_translations.primary_updated' => true}) > 0
end
Expand Down Expand Up @@ -132,13 +134,15 @@ def search_phrases_without_translation(query, page = nil, options = {})
end

def to_hash
{ name => translations.each_with_object({}) do |translation, locale|
if translation.phrase.key.include?(".")
locale.deep_merge!(unsquish(translation.phrase.key, translation.value))
else
locale[translation.phrase.key] = translation.value
data = translations.includes(:phrase).order(phrases.arel_table[:key]).
each_with_object({}) do |translation, locale|
if translation.phrase.key.include?(".")
locale.deep_merge!(unsquish(translation.phrase.key, translation.value))
else
locale[translation.phrase.key] = translation.value
end
end
end }
{ name => data }
end

def to_param
Expand Down
10 changes: 7 additions & 3 deletions lib/tolk/export.rb
Expand Up @@ -10,7 +10,12 @@ def initialize(args)

def dump
File.open("#{destination}/#{name}.yml", "w+") do |file|
data.respond_to?(:ya2yaml) ? file.write(data.ya2yaml(:syck_compatible => true)) : file.write(YAML.dump(data).force_encoding file.external_encoding.name)
yml = if data.respond_to?(:ya2yaml)
data.ya2yaml(:syck_compatible => true)
else
YAML.dump(data).force_encoding(file.external_encoding.name)
end
file.write(yml)
end
end

Expand All @@ -23,6 +28,5 @@ def dump_path
Tolk::Locale._dump_path
end
end

end
end
end
2 changes: 1 addition & 1 deletion test/locales/basic/da.yml
@@ -1,9 +1,9 @@
---
da:
cozy: Hyggeligt
hello_world: Hej Verden
nested:
hello_world: Nedarvet Hej Verden
cozy: Hyggeligt
number:
currency:
format:
Expand Down
13 changes: 12 additions & 1 deletion test/unit/locale_test.rb
Expand Up @@ -14,7 +14,18 @@ class LocaleTest < ActiveSupport::TestCase
"hello_world" => "Nested Hello World",
"hello_country" => "Nested Hello Country"
},
"number"=>{"human"=>{"format"=>{"precision"=>1}},"currency"=>{"format"=>{"significant"=>false}}}
"number" => {
"human" => {
"format" => {
"precision" => 1
}
},
"currency" => {
"format" => {
"significant" => false
}
}
}
}}, tolk_locales(:en).to_hash)
end

Expand Down

0 comments on commit 91a2174

Please sign in to comment.