From c35f9133075821ea8819663235a026bdd3960964 Mon Sep 17 00:00:00 2001 From: Thomas Boerger Date: Mon, 30 Mar 2015 17:46:16 +0200 Subject: [PATCH] Extracted a seperate configuration class --- README.md | 16 +++ lib/gettext_i18n_rails_js.rb | 6 ++ lib/gettext_i18n_rails_js/config.rb | 75 ++++++++++++++ lib/gettext_i18n_rails_js/task.rb | 114 ++++++++++++++------- lib/tasks/gettext_i18n_rails_js_tasks.rake | 46 +-------- 5 files changed, 176 insertions(+), 81 deletions(-) create mode 100644 lib/gettext_i18n_rails_js/config.rb diff --git a/README.md b/README.md index 882d6ab..739284c 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,22 @@ jed_options: pretty: false ``` +If you prefer an initializer file within your rails application you can use +that in favor of the YML configuration as well: + +```ruby +GettextI18nRailsJs.config do |config| + config.output_path = "app/assets/javascripts/locale" + + config.handlebars_function = "__" + config.javascript_function = "__" + + config.jed_options = { + pretty: false + } +end +``` + ## Todo diff --git a/lib/gettext_i18n_rails_js.rb b/lib/gettext_i18n_rails_js.rb index c67edeb..97ab3c8 100644 --- a/lib/gettext_i18n_rails_js.rb +++ b/lib/gettext_i18n_rails_js.rb @@ -42,7 +42,13 @@ require_relative "gettext_i18n_rails_js/version" require_relative "gettext_i18n_rails_js/parser" +require_relative "gettext_i18n_rails_js/config" require_relative "gettext_i18n_rails_js/engine" module GettextI18nRailsJs + class << self + def config(&block) + @config ||= GettextI18nRailsJs::Config.new(&block) + end + end end diff --git a/lib/gettext_i18n_rails_js/config.rb b/lib/gettext_i18n_rails_js/config.rb new file mode 100644 index 0000000..e35e730 --- /dev/null +++ b/lib/gettext_i18n_rails_js/config.rb @@ -0,0 +1,75 @@ +# -*- coding: UTF-8 -*- +# +# Copyright (c) 2012-2015 Dropmysite.com +# Copyright (c) 2015 Webhippie +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# + +module GettextI18nRailsJs + class Config + attr_accessor :output_path + attr_accessor :handlebars_function + attr_accessor :javascript_function + attr_accessor :jed_options + + def initialize(&block) + @output_path = defaults[:output_path] + @handlebars_function = defaults[:handlebars_function] + @javascript_function = defaults[:javascript_function] + @jed_options = defaults[:jed_options].symbolize_keys + + instance_eval(&block) if block_given? + end + + protected + + def defaults + file = ::Rails.root.join( + "config", + "gettext_i18n_rails_js.yml" + ) + + values = { + output_path: File.join( + "app", + "assets", + "javascripts", + "locale" + ), + handlebars_function: "__", + javascript_function: "__", + jed_options: { + pretty: false + } + } + + if file.exist? + yaml = YAML.load_file(file) || {} + + values.deep_merge( + yaml + ).with_indifferent_access + else + values.with_indifferent_access + end + end + end +end diff --git a/lib/gettext_i18n_rails_js/task.rb b/lib/gettext_i18n_rails_js/task.rb index ec5449b..e58e6b3 100644 --- a/lib/gettext_i18n_rails_js/task.rb +++ b/lib/gettext_i18n_rails_js/task.rb @@ -1,11 +1,86 @@ -require "gettext_i18n_rails/tasks" +# -*- coding: UTF-8 -*- +# +# Copyright (c) 2012-2015 Dropmysite.com +# Copyright (c) 2015 Webhippie +# +# Permission is hereby granted, free of charge, to any person obtaining +# a copy of this software and associated documentation files (the +# "Software"), to deal in the Software without restriction, including +# without limitation the rights to use, copy, modify, merge, publish, +# distribute, sublicense, and/or sell copies of the Software, and to +# permit persons to whom the Software is furnished to do so, subject to +# the following conditions: +# +# The above copyright notice and this permission notice shall be +# included in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +# module GettextI18nRailsJs module Task extend self + def po_to_json + GettextI18nRailsJs::Parser::Javascript + .gettext_function = GettextI18nRailsJs.config.javascript_function + + GettextI18nRailsJs::Parser::Handlebars + .gettext_function = GettextI18nRailsJs.config.handlebars_function + + if files_list.empty? + puts "Couldn't find PO files in #{locale_path}, run 'rake gettext:find'" + else + files_list.each do |input| + # Language is used for filenames, while language code is used as the + # in-app language code. So for instance, simplified chinese will live + # in app/assets/locale/zh_CN/app.js but inside the file the language + # will be referred to as locales['zh-CN']. This is to adapt to the + # existing gettext_rails convention. + + language = input.dirname.basename.to_s + language_code = language.gsub("_", "-") + + destination = output_path.join(language) + destination.mkpath + + json = PoToJson.new( + input.to_s + ).generate_for_jed( + language_code, + GettextI18nRailsJs.config.jed_options + ) + + destination.join("app.js").open("w") do |f| + f.rewind + f.write(json) + end + + puts "Created app.js in #{destination}" + end + + puts + puts "All files created, make sure they are being added to your assets." + puts "If they are not, you can add them with this line (configurable):" + puts + puts "//= require_tree ./locale" + puts "//= require gettext/all" + puts + end + end + + protected + def files_list - Pathname.glob( + require "gettext_i18n_rails/tasks" + + ::Pathname.glob( ::File.join( locale_path, "**", @@ -16,41 +91,8 @@ def files_list def output_path ::Rails.root.join( - config[:output_path] + GettextI18nRailsJs.config.output_path ) end - - def config - @config ||= begin - file = ::Rails.root.join( - "config", - "gettext_i18n_rails_js.yml" - ) - - defaults = { - output_path: File.join( - "app", - "assets", - "javascripts", - "locale" - ), - handlebars_function: "__", - javascript_function: "__", - jed_options: { - pretty: false - } - } - - if file.exist? - yaml = YAML.load_file(file) || {} - - defaults.deep_merge( - yaml - ).with_indifferent_access - else - defaults.with_indifferent_access - end - end - end end end diff --git a/lib/tasks/gettext_i18n_rails_js_tasks.rake b/lib/tasks/gettext_i18n_rails_js_tasks.rake index 2535573..e517b50 100644 --- a/lib/tasks/gettext_i18n_rails_js_tasks.rake +++ b/lib/tasks/gettext_i18n_rails_js_tasks.rake @@ -29,51 +29,7 @@ require "gettext_i18n_rails_js/task" namespace :gettext do desc "Convert PO files to JS files" task po_to_json: :environment do - GettextI18nRailsJs::Parser::Javascript - .gettext_function = GettextI18nRailsJs::Task.config[:javascript_function] - - GettextI18nRailsJs::Parser::Handlebars - .gettext_function = GettextI18nRailsJs::Task.config[:handlebars_function] - - if GettextI18nRailsJs::Task.files_list.empty? - puts "Couldn't find PO files in #{locale_path}, run 'rake gettext:find'" - else - GettextI18nRailsJs::Task.files_list.each do |input| - # Language is used for filenames, while language code is used as the - # in-app language code. So for instance, simplified chinese will live - # in app/assets/locale/zh_CN/app.js but inside the file the language - # will be referred to as locales['zh-CN']. This is to adapt to the - # existing gettext_rails convention. - - language = input.dirname.basename.to_s - language_code = language.gsub("_", "-") - - destination = GettextI18nRailsJs::Task.output_path.join(language) - destination.mkpath - - json = PoToJson.new( - input.to_s - ).generate_for_jed( - language_code, - GettextI18nRailsJs::Task.config[:jed_options].symbolize_keys - ) - - destination.join("app.js").open("w") do |f| - f.rewind - f.write(json) - end - - puts "Created app.js in #{destination}" - end - - puts - puts "All files created, make sure they are being added to your assets." - puts "If they are not, you can add them with this line (configurable):" - puts - puts "//= require_tree ./locale" - puts "//= require gettext/all" - puts - end + GettextI18nRailsJs::Task.po_to_json end # Required for gettext to filter the files