Created
May 8, 2017 11:13
-
-
Save fredv/d691a62ec25bdeec83785bc8333105cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
puts %{This script checks the main locale of a PhraseApp project for translations with copy changes and then adds the tag retranslate_<locale_code> to keys which translation has not changed since the copy change of its source. | |
It only includes verified main locale translations. | |
If it finds a newer change in the target language it removes the tag retranslate_<locale_code> again. | |
e.g. change in main locale en-GB, key is tagged retranslate_de, translator adapts (or verifies/unverifies) German translation, key tag retranslate_de is removed. | |
It takes the parameters via ENV: | |
PHRASEAPP_ACCESS_TOKEN=abc123 | |
PROJECT_ID=ee68... | |
SINCE=2017-05-01T00:00:00Z or 2017-05-01 | |
This will search the main locale of the project for changed translations after the date SINCE} | |
raise "Please specify PHRASEAPP_ACCESS_TOKEN, SINCE and PROJECT_ID" unless ENV["PHRASEAPP_ACCESS_TOKEN"] && ENV["SINCE"] && ENV["PROJECT_ID"] | |
require 'active_support' | |
require 'active_support/core_ext' | |
require 'phraseapp-ruby' | |
# helper methods | |
since = DateTime.parse(ENV.fetch('SINCE', (Date.today - 1.week).to_s(:db))) | |
def project_id | |
ENV.fetch('PROJECT_ID') | |
end | |
def tag_key(api, key, tag_name) | |
return if key.nil? || key["id"].nil? | |
params = PhraseApp::RequestParams::KeysTagParams.new(tags: tag_name, q: "ids:#{key["id"]}") | |
_, err = api.keys_tag(project_id, params) | |
if err != nil | |
puts err.inspect | |
end | |
end | |
def untag_key(api, key, tag_name) | |
return if key.nil? || key["id"].nil? | |
params = PhraseApp::RequestParams::KeysUntagParams.new(tags: tag_name, q: "ids:#{key["id"]}") | |
_, err = api.keys_untag(project_id, params) | |
if err != nil | |
puts err.inspect | |
end | |
end | |
credentials = PhraseApp::Auth::Credentials.new(token: ENV.fetch("PHRASEAPP_ACCESS_TOKEN")) | |
api = PhraseApp::Client.new(credentials) | |
# load locales | |
locales = [] | |
main_locales = [] | |
default_locale = nil | |
rsp, err = api.locales_list(project_id, 1, 100) | |
rsp.each do |locale| | |
if locale.default == true | |
default_locale = locale | |
end | |
if locale.main == true | |
main_locales << locale | |
end | |
locales << locale | |
end | |
non_main_locales = locales - main_locales | |
if main_locales.length > 1 | |
raise "currently only works for 1 main locale" | |
end | |
# detect changed translations | |
changed_main_segments = [] | |
page = 1 | |
loop do | |
puts since.to_s(:db) | |
params = PhraseApp::RequestParams::TranslationsByLocaleParams.new(sort: "updated_at", order: "desc", q: "updated_at:>=#{since.to_s} unverified:false excluded:false") | |
rsp, err = api.translations_by_locale(project_id, main_locales.first.id, page, 100, params) | |
if err != nil | |
puts err.inspect | |
end | |
puts "Found updated copy in keys: #{rsp.map { |t| t.key["id"] }.join(', ')}" | |
changed_main_segments = changed_main_segments.append(rsp).flatten.compact | |
break if rsp.length < 100 | |
page += 1 | |
sleep 1 | |
end | |
# go through changed translations | |
changed_main_segments.each do |main_segment| | |
# fetch translations in non main locales for the segment | |
rsp, err = api.translations_by_key(project_id, main_segment.key["id"], 1, 100, PhraseApp::RequestParams::TranslationsByKeyParams.new) | |
mapped_translations = non_main_locales.inject({}) do |hash, value| | |
hash[value.id] = rsp.collect { |t| if t.locale["id"] == value.id then t else nil end }.flatten.compact.first | |
hash | |
end | |
non_main_locales.each do |other_locale| | |
# check if a translation in the other_locale already exists, if not translators will translate it anyway | |
if existing_translation = mapped_translations[other_locale.id] | |
puts "Found existing translation in #{other_locale.code}, updated_at: #{existing_translation.updated_at}" | |
# if the latest change was after the update in the main_segment, we add a retranslate_<other_locale.code> tag | |
if existing_translation.updated_at <= main_segment.updated_at | |
puts "Not updated after change in main, tagging" | |
tag_key(api, existing_translation.key, "retranslate_#{other_locale.code}") | |
else | |
# if the update happened afterwards, we assume the translator or editor/country manager has based the latest change on the new source copy | |
puts "Updated after change in main, removing tag if it exists" | |
untag_key(api, existing_translation.key, "retranslate_#{other_locale.code}") | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment