Improved support for different version of translation cache files.

This commit is contained in:
Martin Edenhofer 2017-02-10 15:06:15 +01:00
parent 4d2e578edf
commit 9b8298de28
3 changed files with 17 additions and 10 deletions

View file

@ -70,7 +70,8 @@ all:
=end
def self.load_from_file
file = Rails.root.join('config/locales.yml')
version = Version.get
file = Rails.root.join("config/locales-#{version}.yml")
return false if !File.exist?(file)
data = YAML.load_file(file)
to_database(data)
@ -88,12 +89,13 @@ all:
=end
def self.fetch
version = Version.get
url = 'https://i18n.zammad.com/api/v1/locales'
result = UserAgent.get(
url,
{
version: Version.get,
version: version,
},
{
json: true,
@ -103,7 +105,7 @@ all:
raise "Can't load locales from #{url}" if !result
raise "Can't load locales from #{url}: #{result.error}" if !result.success?
file = Rails.root.join('config/locales.yml')
file = Rails.root.join("config/locales-#{version}.yml")
File.open(file, 'w') do |out|
YAML.dump(result.data, out)
end

View file

@ -225,9 +225,10 @@ all:
=end
def self.load_from_file(dedicated_locale = nil)
version = Version.get
directory = Rails.root.join('config/translations')
locals_to_sync(dedicated_locale).each { |locale|
file = Rails.root.join("#{directory}/#{locale}.yml")
file = Rails.root.join("#{directory}/#{locale}-#{version}.yml")
return false if !File.exist?(file)
data = YAML.load_file(file)
to_database(locale, data)
@ -250,6 +251,7 @@ all:
=end
def self.fetch(dedicated_locale = nil)
version = Version.get
locals_to_sync(dedicated_locale).each { |locale|
url = "https://i18n.zammad.com/api/v1/translations/#{locale}"
if !UserInfo.current_user_id
@ -258,7 +260,7 @@ all:
result = UserAgent.get(
url,
{
version: Version.get,
version: version,
},
{
json: true,
@ -272,7 +274,7 @@ all:
if !File.directory?(directory)
Dir.mkdir(directory, 0o755)
end
file = Rails.root.join("#{directory}/#{locale}.yml")
file = Rails.root.join("#{directory}/#{locale}-#{version}.yml")
File.open(file, 'w') do |out|
YAML.dump(result.data, out)
end

View file

@ -7,14 +7,17 @@ require 'net/http'
require 'json'
require 'yaml'
version = File.read('VERSION')
version.strip!
url_locales = 'https://i18n.zammad.com/api/v1/locales'
url_translations = 'https://i18n.zammad.com/api/v1/translations/'
file_locales = 'config/locales.yml'
file_locales = "config/locales-#{version}.yml"
directory_translations = 'config/translations'
# download locales
uri = URI.parse(url_locales)
uri = URI.parse("#{url_locales}?version=#{version}")
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri)
@ -31,14 +34,14 @@ if !File.directory?(directory_translations)
Dir.mkdir(directory_translations, 0o755)
end
data.each { |locale|
url = "#{url_translations}#{locale['locale']}.yml"
url = "#{url_translations}#{locale['locale']}?version=#{version}"
uri = URI.parse(url)
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
request = Net::HTTP::Get.new(uri)
response = http.request(request)
data = JSON.parse(response.body)
file = "#{directory_translations}/#{locale['locale']}.yml"
file = "#{directory_translations}/#{locale['locale']}-#{version}.yml"
puts "Writing #{file}..."
File.open(file, 'w') do |out|
YAML.dump(data, out)