From ef8034180634a49025577379fbbac8ea178c6a1e Mon Sep 17 00:00:00 2001 From: Gerhard Schlager Date: Fri, 27 Apr 2018 19:01:34 +0200 Subject: [PATCH] Update pull_translations script to work with latest Transifex changes * supports resources created with Transifex's YML handler version 3 * uses translations-manager gem * makes sure that the locales supported by translations-manager are not out of sync * update the lang_map in tx client config before pulling translations --- script/pull_translations.rb | 349 ++++-------------------------------- 1 file changed, 34 insertions(+), 315 deletions(-) mode change 100644 => 100755 script/pull_translations.rb diff --git a/script/pull_translations.rb b/script/pull_translations.rb old mode 100644 new mode 100755 index 8b5faedb413..4370b229032 --- a/script/pull_translations.rb +++ b/script/pull_translations.rb @@ -1,327 +1,46 @@ -# This script pulls translation files from Transifex and ensures they are in the format we need. -# You need the Transifex client installed. -# http://docs.transifex.com/developer/client/setup -# -# Don't use this script to create pull requests. Do translations in Transifex. The Discourse -# team will pull them in. +#!/usr/bin/env ruby -require 'open3' -require 'psych' -require 'set' -require 'fileutils' -require_relative '../lib/i18n/locale_file_walker' +require 'bundler/inline' + +gemfile(true) do + gem 'translations-manager', git: 'https://github.com/discourse/translations-manager.git' +end + +require 'translations_manager' + +def expand_path(path) + File.expand_path("../../#{path}", __FILE__) +end + +def supported_locales + Dir.glob(expand_path('config/locales/client.*.yml')) + .map { |x| x.split('.')[-2] } + .select { |x| x != 'en' } + .sort +end YML_DIRS = ['config/locales', 'plugins/poll/config/locales', 'plugins/discourse-details/config/locales', 'plugins/discourse-narrative-bot/config/locales', 'plugins/discourse-nginx-performance-report/config/locales', - 'plugins/discourse-presence/config/locales'] + 'plugins/discourse-presence/config/locales'].map { |dir| expand_path(dir) } YML_FILE_PREFIXES = ['server', 'client'] +TX_CONFIG = expand_path('.tx/config') -if `which tx`.strip.empty? - puts '', 'The Transifex client needs to be installed to use this script.' - puts 'Instructions are here: http://docs.transifex.com/client/setup/' - puts '', 'On Mac:', '' - puts ' sudo easy_install pip' - puts ' sudo pip install transifex-client', '' +if TranslationsManager::SUPPORTED_LOCALES != supported_locales + STDERR.puts <<~MESSAGE + + The supported locales are out of sync. + Please update the TranslationsManager::SUPPORTED_LOCALES in translations-manager. + https://github.com/discourse/translations-manager + + The following locales are currently supported by Discourse: + + MESSAGE + + STDERR.puts locales.map { |l| "'#{l}'" }.join(",\n") exit 1 end -if ARGV.include?('force') - STDERR.puts 'Usage: ruby pull_translations.rb [languages]' - STDERR.puts 'Example: ruby pull_translations.rb de it', '' - exit 1 -end - -def get_languages - if ARGV.empty? - Dir.glob(File.expand_path('../../config/locales/client.*.yml', __FILE__)) - .map { |x| x.split('.')[-2] } - else - ARGV - end -end - -def yml_path(dir, prefix, language) - path = "../../#{dir}/#{prefix}.#{language}.yml" - File.expand_path(path, __FILE__) -end - -def yml_path_if_exists(dir, prefix, language) - path = yml_path(dir, prefix, language) - File.exists?(path) ? path : nil -end - -languages = get_languages.select { |x| x != 'en' }.sort - -# ensure that all locale files exists. tx doesn't create missing locale files during pull -YML_DIRS.each do |dir| - YML_FILE_PREFIXES.each do |prefix| - next unless yml_path_if_exists(dir, prefix, 'en') - - languages.each do |language| - filename = yml_path(dir, prefix, language) - FileUtils.touch(filename) unless File.exists?(filename) - end - end -end - -puts 'Pulling new translations...', '' -command = "tx pull --mode=developer --language=#{languages.join(',')} --force" - -return_value = Open3.popen2e(command) do |_, stdout_err, wait_thr| - while (line = stdout_err.gets) - puts line - end - wait_thr.value -end - -puts '' - -unless return_value.success? - STDERR.puts 'Something failed. Check the output above.', '' - exit return_value.exitstatus -end - -YML_FILE_COMMENTS = <= next_line[/^\s*/].size - # remove lines which have an empty value and are not followed - # by a key on the next level - lines[i] = nil - end - end - end - - break if lines.compact!.nil? - end - - File.open(filename, 'w+') do |f| - f.puts(lines) - end -end - -YML_DIRS.each do |dir| - YML_FILE_PREFIXES.each do |prefix| - english_alias_data = get_english_alias_data(dir, prefix) - - languages.each do |language| - filename = yml_path_if_exists(dir, prefix, language) - - if filename - # The following methods were added to handle a bug in Transifex's yml. Should not be needed now. - # fix_invalid_yml_keys(filename) - # fix_invalid_yml(filename) - - # TODO check if this is still needed with recent Transifex changes - # Nov 14, 2017: yup, still needed - add_anchors_and_aliases(english_alias_data, filename) - - update_file_header(filename, language) - end - end - end -end +TranslationsManager::TransifexUpdater.new(YML_DIRS, YML_FILE_PREFIXES, *ARGV).perform(tx_config_filename: TX_CONFIG)