From 9b8298de28cb69ee529aa00c13610281549a5619 Mon Sep 17 00:00:00 2001 From: Martin Edenhofer Date: Fri, 10 Feb 2017 15:06:15 +0100 Subject: [PATCH] Improved support for different version of translation cache files. --- app/models/locale.rb | 8 +++++--- app/models/translation.rb | 8 +++++--- contrib/packager.io/fetch_locales.rb | 11 +++++++---- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/app/models/locale.rb b/app/models/locale.rb index 619ef4c4a..969c51729 100644 --- a/app/models/locale.rb +++ b/app/models/locale.rb @@ -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 diff --git a/app/models/translation.rb b/app/models/translation.rb index 6f1fb2da0..bc04c7f51 100644 --- a/app/models/translation.rb +++ b/app/models/translation.rb @@ -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 diff --git a/contrib/packager.io/fetch_locales.rb b/contrib/packager.io/fetch_locales.rb index f97d3dc74..0c7eba657 100755 --- a/contrib/packager.io/fetch_locales.rb +++ b/contrib/packager.io/fetch_locales.rb @@ -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)