From 9b62dc6f83a9fa3a265048c51cde378f6f20dd12 Mon Sep 17 00:00:00 2001 From: Saivann Date: Sat, 8 Mar 2014 22:13:45 -0500 Subject: [PATCH] Add script used to update translation templates --- README.md | 4 ++-- _contrib/updatetx.rb | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 41 insertions(+), 2 deletions(-) create mode 100755 _contrib/updatetx.rb diff --git a/README.md b/README.md index 4ba6869f..88a97597 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,9 @@ Installing dependencies on older Ubuntu and Debian distributions ### Import translations -**Update translations**: You can overwrite each language files in _translations by their updated version from Transifex. You should make sure that each .html files (in _layouts, _templates) don't serve outdated content for those languages. You should also make sure that no url has been changed by translators. If one page has been replaced or moved, a redirection can be added in _config.yml. +**Update translations**: You can update the relevant language file in \_translations/ and run ./\_contrib/updatetx.rb to update layouts and templates for this language. You should also make sure that no url has been changed by translators. If one page has been replaced or moved, a redirection can be added in \_config.yml. -**Add a new language**: You can put the language file from Transifex in _translations and add the language in _config.yml in the right display order for the language bar. Make sure to review all pages and check all links. +**Add a new language**: You can put the language file from Transifex in \_translations and add the language in \_config.yml in the right display order for the language bar. Make sure to review all pages and check all links. ### Update english strings diff --git a/_contrib/updatetx.rb b/_contrib/updatetx.rb new file mode 100755 index 00000000..8f713996 --- /dev/null +++ b/_contrib/updatetx.rb @@ -0,0 +1,39 @@ +#!/usr/bin/env ruby + +#Drop outdated fallback HTML code in all layouts for specified language. +#Example: ./_contrib/updatetx.rb + +def prompt(*args) + print(*args) + gets +end + +lang = prompt "Language code: " +lang = lang.gsub(/[^a-z]/,'') + +if !File.exist?('_translations/' + lang + '.yml') + print "Wrong language code. \n" + exit +end + +dirs = [ '_templates', '_layouts' ] + +dirs.each do |dir| + Dir.foreach(dir) do |file| + next if file == '.' or file == '..' + contents = File.read(dir + '/' + file) + # Drop HTML code applied to current language only ( until next {% when / else / endcase %} statement ) + contents.gsub!(Regexp.new("{% when '" + lang + "' %}((?!{% endcase %})(?!{% else %}).)*?{% when", Regexp::MULTILINE),'{% when') + contents.gsub!(Regexp.new("{% when '" + lang + "' %}((?!{% endcase %}).)*?{% else %}", Regexp::MULTILINE),'{% else %}') + contents.gsub!(Regexp.new("{% when '" + lang + "' %}.*?{% endcase %}", Regexp::MULTILINE),'{% endcase %}') + # Drop complete {% case / endcase %} statements when not used by any language anymore + contents.gsub!(Regexp.new("{% case page.lang %}(((?!{% when ).)*?){% else %}(.*?){% endcase %}", Regexp::MULTILINE),'\1 \3') + contents.gsub!(Regexp.new("{% case page.lang %}(((?!{% when ).)*?){% endcase %}", Regexp::MULTILINE),'\1') + # Drop language in statements applied to many languages ( e.g. {% when 'ar' or 'fr' .. %} ) + contents.gsub!(Regexp.new("{% when '" + lang + "' or (.*?) %}"),'{% when \1 %}') + contents.gsub!(Regexp.new("{% when (.*?) or '" + lang + "' (.*?) %}"),'{% when \1 \2 %}') + File.open(dir + '/' + file, 'w') do |file| + file.write(contents) + end + end +end