From 50ad777da2f9dcd8135896870f71274eb6562ad8 Mon Sep 17 00:00:00 2001 From: Bryan Kearney Date: Sun, 15 Feb 2015 12:51:40 -0500 Subject: [PATCH] Plugins should support i18n. This commit adds the basics, but the engineers need to start translating code --- Gemfile | 2 +- Rakefile | 13 ++++++++ hammer_cli_csv.gemspec | 2 +- lib/hammer_cli_csv/i18n.rb | 24 ++++++++++++++ locale/Makefile | 65 ++++++++++++++++++++++++++++++++++++++ locale/README.md | 18 +++++++++++ locale/hammer_cli_csv.pot | 23 ++++++++++++++ 7 files changed, 145 insertions(+), 2 deletions(-) create mode 100644 lib/hammer_cli_csv/i18n.rb create mode 100644 locale/Makefile create mode 100644 locale/README.md create mode 100644 locale/hammer_cli_csv.pot diff --git a/Gemfile b/Gemfile index fe6daf3..f2116ff 100644 --- a/Gemfile +++ b/Gemfile @@ -15,4 +15,4 @@ end # load local gemfile local_gemfile = File.join(File.dirname(__FILE__), 'Gemfile.local') -self.instance_eval(Bundler.read_file(local_gemfile)) if File.exist?(local_gemfile) \ No newline at end of file +self.instance_eval(Bundler.read_file(local_gemfile)) if File.exist?(local_gemfile) diff --git a/Rakefile b/Rakefile index 50d8945..645bbbd 100644 --- a/Rakefile +++ b/Rakefile @@ -15,6 +15,19 @@ rescue puts "Rubocop not loaded" end +namespace :gettext do + desc "Update pot file" + task :find do + require "hammer_cli_csv/version" + require "hammer_cli_csv/i18n" + require 'gettext/tools' + + domain = HammerCLICsv::I18n::LocaleDomain.new + GetText.update_pofiles(domain.domain_name, domain.translated_files, "#{domain.domain_name} #{HammerCLICsv.version}", :po_root => domain.locale_dir) + end +end + + task :default do Rake::Task['rubocop'].execute end diff --git a/hammer_cli_csv.gemspec b/hammer_cli_csv.gemspec index e3e9bd4..36c9e02 100644 --- a/hammer_cli_csv.gemspec +++ b/hammer_cli_csv.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| spec.require_paths = %w(lib) spec.add_dependency('hammer_cli_katello') - + spec.add_dependency("gettext", "~> 2.0") spec.add_development_dependency("rubocop", "0.24.1") end diff --git a/lib/hammer_cli_csv/i18n.rb b/lib/hammer_cli_csv/i18n.rb new file mode 100644 index 0000000..13f9178 --- /dev/null +++ b/lib/hammer_cli_csv/i18n.rb @@ -0,0 +1,24 @@ +require 'hammer_cli/i18n' + +module HammerCLICsv + module I18n + + class LocaleDomain < HammerCLI::I18n::LocaleDomain + + def translated_files + Dir.glob(File.join(File.dirname(__FILE__), '../**/*.rb')) + end + + def locale_dir + File.join(File.dirname(__FILE__), '../../locale') + end + + def domain_name + 'hammer_cli_csv' + end + end + + end +end + +HammerCLI::I18n.add_domain(HammerCLICsv::I18n::LocaleDomain.new) diff --git a/locale/Makefile b/locale/Makefile new file mode 100644 index 0000000..dcb444b --- /dev/null +++ b/locale/Makefile @@ -0,0 +1,65 @@ +# +# Makefile for PO merging and MO generation. More info in the README. +# +# make all-mo (default) - generate MO files +# make check - check translations using translate-tool +# make tx-update - download and merge translations from Transifex +# make clean - clean everything +# +DOMAIN = hammer_cli_csv +VERSION = $(shell ruby -e 'require "rubygems";spec = Gem::Specification::load("../hammer_cli_csv.gemspec");puts spec.version') +POTFILE = $(DOMAIN).pot +MOFILE = $(DOMAIN).mo +POFILES = $(shell find . -name '*.po') +MOFILES = $(patsubst %.po,%.mo,$(POFILES)) +POXFILES = $(patsubst %.po,%.pox,$(POFILES)) + +%.mo: %.po + mkdir -p $(shell dirname $@)/LC_MESSAGES + msgfmt -o $(shell dirname $@)/LC_MESSAGES/$(MOFILE) $< + +# Generate MO files from PO files +all-mo: $(MOFILES) + +# Check for malformed strings +%.pox: %.po + msgfmt -c $< + pofilter --nofuzzy -t variables -t blank -t urls -t emails -t long -t newlines \ + -t endwhitespace -t endpunc -t puncspacing -t options -t printf -t validchars --gnome $< > $@ + cat $@ + ! grep -q msgid $@ + +check: $(POXFILES) + msgfmt -c ${POTFILE} + +# Merge PO files +update-po: + for f in $(shell find ./ -name "*.po") ; do \ + msgmerge -N --backup=none -U $$f ${POTFILE} ; \ + done + +# Unify duplicate translations +uniq-po: + for f in $(shell find ./ -name "*.po") ; do \ + msguniq $$f -o $$f ; \ + done + +tx-pull: + tx pull -f + for f in $(POFILES) ; do \ + sed -i 's/^\("Project-Id-Version: \).*$$/\1$(DOMAIN) $(VERSION)\\n"/' $$f; \ + done + -git commit -a -m "i18n - extracting new, pulling from tx" + +extract-strings: + bundle exec rake gettext:find + +reset-po: + # merging po files is unnecessary when using transifex.com + git checkout -- ../locale/*/*po + +tx-update: tx-pull extract-strings reset-po $(MOFILES) + # amend mo files + git add ../locale/*/LC_MESSAGES + git commit -a --amend -m "i18n - extracting new, pulling from tx" + -echo Changes commited! diff --git a/locale/README.md b/locale/README.md new file mode 100644 index 0000000..f051500 --- /dev/null +++ b/locale/README.md @@ -0,0 +1,18 @@ +Updating the translations +------------------------- + + 1. Check if there are any new languages with progress more than 50% on [transifex](https://www.transifex.com/projects/p/foreman/resource/hammer-cli-csv/). If so, do the following for each of the new languages: + + ``` + mkdir locale/ + cp locale/hammer_cli_csv.pot locale//hammer_cli_csv.po + ``` + 2. Make sure you have `transifex-client` installed + + 3. Update the translations. From GIT repo root directory run: + + ``` + make -C locale tx-update + ``` + + It will download translations from transifex, generates `mo` files, updates strings in `pot` file and wraps all the changes in a new commit. Transifex automatically updates its strings when the commit is pushed to Github. diff --git a/locale/hammer_cli_csv.pot b/locale/hammer_cli_csv.pot new file mode 100644 index 0000000..a1341b1 --- /dev/null +++ b/locale/hammer_cli_csv.pot @@ -0,0 +1,23 @@ +# SOME DESCRIPTIVE TITLE. +# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER +# This file is distributed under the same license as the PACKAGE package. +# FIRST AUTHOR , YEAR. +# +#, fuzzy +msgid "" +msgstr "" +"Project-Id-Version: hammer_cli_csv 0.0.6\n" +"Report-Msgid-Bugs-To: \n" +"POT-Creation-Date: 2015-02-15 12:46-0500\n" +"PO-Revision-Date: 2015-02-15 12:46-0500\n" +"Last-Translator: FULL NAME \n" +"Language-Team: LANGUAGE \n" +"Language: \n" +"MIME-Version: 1.0\n" +"Content-Type: text/plain; charset=UTF-8\n" +"Content-Transfer-Encoding: 8bit\n" +"Plural-Forms: nplurals=INTEGER; plural=EXPRESSION;\n" + +#: lib/hammer_cli_csv/csv.rb:21 +msgid "import to, or export from a running foretello server" +msgstr ""