Skip to content

Commit

Permalink
Extracted a seperate configuration class
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas Boerger committed Mar 30, 2015
1 parent 92ebdd6 commit c35f913
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 81 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions lib/gettext_i18n_rails_js.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
75 changes: 75 additions & 0 deletions lib/gettext_i18n_rails_js/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# -*- coding: UTF-8 -*-
#
# Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
# Copyright (c) 2015 Webhippie <http://www.webhippie.de>
#
# 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
114 changes: 78 additions & 36 deletions lib/gettext_i18n_rails_js/task.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,86 @@
require "gettext_i18n_rails/tasks"
# -*- coding: UTF-8 -*-
#
# Copyright (c) 2012-2015 Dropmysite.com <https://dropmyemail.com>
# Copyright (c) 2015 Webhippie <http://www.webhippie.de>
#
# 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,
"**",
Expand All @@ -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
46 changes: 1 addition & 45 deletions lib/tasks/gettext_i18n_rails_js_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit c35f913

Please sign in to comment.