gettext_simple_rails 0.0.2 → 0.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Rakefile +2 -2
- data/lib/gettext_simple_rails.rb +47 -0
- data/lib/gettext_simple_rails/i18n_injector.rb +73 -3
- data/lib/gettext_simple_rails/translators/active_admin_translator.rb +22 -0
- data/lib/gettext_simple_rails/translators/date_translator.rb +30 -0
- data/lib/gettext_simple_rails/translators/devise_translator.rb +9 -0
- data/lib/gettext_simple_rails/translators/simple_form_translator.rb +9 -0
- data/lib/gettext_simple_rails/version.rb +1 -1
- data/lib/tasks/gettext_simple_rails_tasks.rake +23 -0
- data/spec/date_translator_spec.rb +24 -0
- data/{test → spec}/dummy/README.rdoc +0 -0
- data/{test → spec}/dummy/Rakefile +0 -0
- data/{test → spec}/dummy/app/assets/javascripts/application.js +0 -0
- data/{test → spec}/dummy/app/assets/stylesheets/application.css +0 -0
- data/{test → spec}/dummy/app/controllers/application_controller.rb +0 -0
- data/{test → spec}/dummy/app/helpers/application_helper.rb +0 -0
- data/{test → spec}/dummy/app/models/role.rb +0 -0
- data/{test → spec}/dummy/app/models/user.rb +0 -0
- data/{test → spec}/dummy/app/views/layouts/application.html.erb +0 -0
- data/{test → spec}/dummy/bin/bundle +0 -0
- data/{test → spec}/dummy/bin/rails +0 -0
- data/{test → spec}/dummy/bin/rake +0 -0
- data/{test → spec}/dummy/config.ru +0 -0
- data/{test → spec}/dummy/config/application.rb +0 -0
- data/{test → spec}/dummy/config/boot.rb +0 -0
- data/{test → spec}/dummy/config/database.yml +0 -0
- data/{test → spec}/dummy/config/environment.rb +0 -0
- data/{test → spec}/dummy/config/environments/development.rb +0 -0
- data/{test → spec}/dummy/config/environments/production.rb +0 -0
- data/{test → spec}/dummy/config/environments/test.rb +0 -0
- data/{test → spec}/dummy/config/initializers/backtrace_silencers.rb +0 -0
- data/{test → spec}/dummy/config/initializers/cookies_serializer.rb +0 -0
- data/{test → spec}/dummy/config/initializers/filter_parameter_logging.rb +0 -0
- data/{test → spec}/dummy/config/initializers/inflections.rb +0 -0
- data/{test → spec}/dummy/config/initializers/mime_types.rb +0 -0
- data/{test → spec}/dummy/config/initializers/session_store.rb +0 -0
- data/{test → spec}/dummy/config/initializers/wrap_parameters.rb +0 -0
- data/{test → spec}/dummy/config/locales/da.yml +0 -0
- data/{test → spec}/dummy/config/locales/en.yml +0 -0
- data/{test → spec}/dummy/config/routes.rb +0 -0
- data/{test → spec}/dummy/config/secrets.yml +0 -0
- data/{test → spec}/dummy/db/development.sqlite3 +0 -0
- data/{test → spec}/dummy/db/migrate/20140410170418_create_users.rb +0 -0
- data/{test → spec}/dummy/db/migrate/20140411112622_create_roles.rb +0 -0
- data/{test → spec}/dummy/db/schema.rb +0 -0
- data/{test → spec}/dummy/db/test.sqlite3 +0 -0
- data/{test → spec}/dummy/lib/gettext_simple_rails/models/role_model_translations.rb +3 -0
- data/{test → spec}/dummy/lib/gettext_simple_rails/models/user_model_translations.rb +3 -0
- data/{test → spec}/dummy/log/development.log +0 -0
- data/{test → spec}/dummy/public/404.html +0 -0
- data/{test → spec}/dummy/public/422.html +0 -0
- data/{test → spec}/dummy/public/500.html +0 -0
- data/{test → spec}/dummy/public/favicon.ico +0 -0
- data/{test → spec}/dummy/test/fixtures/roles.yml +0 -0
- data/{test → spec}/dummy/test/fixtures/users.yml +0 -0
- data/{test → spec}/dummy/test/models/role_test.rb +0 -0
- data/{test → spec}/dummy/test/models/user_test.rb +0 -0
- data/{test/gettext_simple_rails_test.rb → spec/gettext_simple_rails_spec.rb} +5 -5
- data/spec/spec_helper.rb +45 -0
- data/{test → spec}/test_helper.rb +0 -0
- metadata +122 -105
- data/lib/gettext_simple_rails/simple_form_detector.rb +0 -9
- data/test/dummy/log/test.log +0 -630
- data/test/integration/navigation_test.rb +0 -10
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a7f781bf7138e3d82a21dc6c3ebb57bc2b1d7630
|
4
|
+
data.tar.gz: 7fc8c0216bf251ac18adc7b0502e81448b43de37
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb83683794a9f065235e31ba09eca9094d461d6e752870241b989e775f80896491222030bb0ee6f8df5b4ea21e20e552bc37a0f0d636c4750b912d8ac01478bb
|
7
|
+
data.tar.gz: c86446f8c9c07bc4e951c897296e0ef69211da392bd2a3a2a3d9f21083df2cc320f1a82b2df492d2dfd93ff4424a0e1f4e4bbb72f13ba3c04b55bafa7aa635b8
|
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ RDoc::Task.new(:rdoc) do |rdoc|
|
|
14
14
|
rdoc.rdoc_files.include('lib/**/*.rb')
|
15
15
|
end
|
16
16
|
|
17
|
-
APP_RAKEFILE = File.expand_path("../
|
17
|
+
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
18
18
|
load 'rails/tasks/engine.rake'
|
19
19
|
|
20
20
|
|
@@ -26,7 +26,7 @@ require 'rake/testtask'
|
|
26
26
|
Rake::TestTask.new(:test) do |t|
|
27
27
|
t.libs << 'lib'
|
28
28
|
t.libs << 'test'
|
29
|
-
t.pattern = '
|
29
|
+
t.pattern = 'spec/**/*_test.rb'
|
30
30
|
t.verbose = false
|
31
31
|
end
|
32
32
|
|
data/lib/gettext_simple_rails.rb
CHANGED
@@ -11,4 +11,51 @@ module GettextSimpleRails
|
|
11
11
|
def self.translation_dir
|
12
12
|
return "#{Rails.root}/lib/gettext_simple_rails"
|
13
13
|
end
|
14
|
+
|
15
|
+
def self.write_recursive_translations(fp, translations, pre_path = [])
|
16
|
+
if translations.is_a?(Hash)
|
17
|
+
translations.each do |key, val|
|
18
|
+
newpath = pre_path + [key]
|
19
|
+
write_recursive_translations(fp, val, newpath)
|
20
|
+
end
|
21
|
+
elsif translations.is_a?(Array)
|
22
|
+
translations.each do |val|
|
23
|
+
newpath = pre_path + [val]
|
24
|
+
write_recursive_translations(fp, val, newpath)
|
25
|
+
end
|
26
|
+
elsif translations.is_a?(String) || translations.is_a?(Fixnum)
|
27
|
+
fp.puts " _('#{pre_path.join(".")}')"
|
28
|
+
else
|
29
|
+
raise "Unknownt class: '#{translations.class.name}'."
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
class Translators
|
34
|
+
def self.const_missing(name)
|
35
|
+
require "#{::File.dirname(__FILE__)}/gettext_simple_rails/translators/#{::StringCases.camel_to_snake(name)}"
|
36
|
+
raise LoadError, "Still not loaded: '#{name}'." unless ::GettextSimpleRails::Translators.const_defined?(name)
|
37
|
+
return ::GettextSimpleRails::Translators.const_get(name)
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.load_all
|
41
|
+
result = []
|
42
|
+
|
43
|
+
path = "#{File.dirname(__FILE__)}/gettext_simple_rails/translators"
|
44
|
+
Dir.foreach(path) do |file|
|
45
|
+
next unless match = file.match(/^(.+_translator)\.rb$/)
|
46
|
+
require "#{path}/#{file}"
|
47
|
+
|
48
|
+
class_name = StringCases.snake_to_camel(match[1])
|
49
|
+
clazz = ::GettextSimpleRails::Translators.const_get(class_name)
|
50
|
+
|
51
|
+
result << {
|
52
|
+
:path => path,
|
53
|
+
:file => file,
|
54
|
+
:class => clazz
|
55
|
+
}
|
56
|
+
end
|
57
|
+
|
58
|
+
return result
|
59
|
+
end
|
60
|
+
end
|
14
61
|
end
|
@@ -1,11 +1,25 @@
|
|
1
1
|
class GettextSimpleRails::I18nInjector
|
2
2
|
def initialize(args = {})
|
3
3
|
@debug = args[:debug]
|
4
|
+
@i18n_backend = I18n.config.backend
|
4
5
|
end
|
5
6
|
|
6
|
-
def
|
7
|
-
|
7
|
+
def inject_translator_translations(gettext_simple)
|
8
|
+
@translator_translations = {}
|
8
9
|
|
10
|
+
GettextSimpleRails::Translators.load_all.each do |translator_data|
|
11
|
+
translator = translator_data[:class].new
|
12
|
+
next unless translator.detected?
|
13
|
+
|
14
|
+
I18n.available_locales.each do |locale|
|
15
|
+
next unless gettext_simple.locale_exists?(locale.to_s)
|
16
|
+
locale = locale.to_s
|
17
|
+
injector_recursive gettext_simple, locale, translator.translations
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def inject_model_translations(gettext_simple)
|
9
23
|
GettextSimpleRails::ModelInspector.model_classes do |inspector|
|
10
24
|
model = {}
|
11
25
|
attributes = {}
|
@@ -58,12 +72,68 @@ class GettextSimpleRails::I18nInjector
|
|
58
72
|
end
|
59
73
|
end
|
60
74
|
|
61
|
-
i18n_backend.store_translations(locale.to_sym, data)
|
75
|
+
@i18n_backend.store_translations(locale.to_sym, data)
|
62
76
|
end
|
63
77
|
end
|
64
78
|
end
|
65
79
|
|
80
|
+
private
|
81
|
+
|
66
82
|
def debug(str)
|
67
83
|
$stderr.puts str if @debug
|
68
84
|
end
|
85
|
+
|
86
|
+
def injector_recursive(gettext_simple, locale, translations, pre_path = [])
|
87
|
+
if translations.is_a?(Hash)
|
88
|
+
translations.each do |key, val|
|
89
|
+
newpath = pre_path + [key]
|
90
|
+
injector_recursive(gettext_simple, locale, val, newpath)
|
91
|
+
end
|
92
|
+
elsif translations.is_a?(Array)
|
93
|
+
injector_recursive_array(gettext_simple, locale, translations, pre_path)
|
94
|
+
elsif translations.is_a?(String)
|
95
|
+
gettext_key = "#{pre_path.join(".")}"
|
96
|
+
translation = gettext_simple.translate_with_locale(locale.to_s, gettext_key)
|
97
|
+
|
98
|
+
if !translation.to_s.empty? && translation != gettext_key
|
99
|
+
translation_hash = {}
|
100
|
+
translation_current = translation_hash
|
101
|
+
pre_path.each_with_index do |path, index|
|
102
|
+
if index == (pre_path.length - 1)
|
103
|
+
translation_current[path] = translation
|
104
|
+
else
|
105
|
+
translation_current[path] = {}
|
106
|
+
translation_current = translation_current[path]
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
@i18n_backend.store_translations(locale.to_sym, translation_hash)
|
111
|
+
end
|
112
|
+
else
|
113
|
+
raise "Unknown class: '#{translations.class.name}'."
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
def injector_recursive_array(gettext_simple, locale, translations, pre_path = [])
|
118
|
+
translation_array = []
|
119
|
+
translations.each_with_index do |val, index|
|
120
|
+
gettext_key = "#{pre_path.join(".")}.#{index}"
|
121
|
+
translation = gettext_simple.translate_with_locale(locale.to_s, gettext_key)
|
122
|
+
next if translation.to_s.empty? || translation == gettext_key
|
123
|
+
translation_array << translation
|
124
|
+
end
|
125
|
+
|
126
|
+
translation_hash = {}
|
127
|
+
translation_current = translation_hash
|
128
|
+
pre_path.each_with_index do |path, index|
|
129
|
+
if index == (pre_path.length - 1)
|
130
|
+
translation_current[path] = translation_array
|
131
|
+
else
|
132
|
+
translation_current[path] = {}
|
133
|
+
translation_current = translation_current[path]
|
134
|
+
end
|
135
|
+
end
|
136
|
+
|
137
|
+
@i18n_backend.store_translations(locale.to_sym, translation_hash)
|
138
|
+
end
|
69
139
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
class GettextSimpleRails::Translators::ActiveAdminTranslator
|
2
|
+
def detected?
|
3
|
+
return ::Kernel.const_defined?("ActiveAdmin")
|
4
|
+
end
|
5
|
+
|
6
|
+
def translations
|
7
|
+
result = {
|
8
|
+
"active_admin" => {
|
9
|
+
"globalize" => {
|
10
|
+
"language" => {},
|
11
|
+
"translations" => ""
|
12
|
+
}
|
13
|
+
}
|
14
|
+
}
|
15
|
+
|
16
|
+
I18n.available_locales.each do |locale|
|
17
|
+
result["active_admin"]["globalize"]["language"][locale.to_s] = locale.to_s
|
18
|
+
end
|
19
|
+
|
20
|
+
return result
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,30 @@
|
|
1
|
+
class GettextSimpleRails::Translators::DateTranslator
|
2
|
+
def detected?
|
3
|
+
true
|
4
|
+
end
|
5
|
+
|
6
|
+
def translations
|
7
|
+
return {
|
8
|
+
"date" => {
|
9
|
+
"formats" => {
|
10
|
+
"default" => "%Y-%m-%d",
|
11
|
+
"short" => "%b %d",
|
12
|
+
"long" => "%B %d, %Y"
|
13
|
+
},
|
14
|
+
"day_names" => [0, 1, 2, 3, 4, 5, 6],
|
15
|
+
"abbr_day_names" => [0, 1, 2, 3, 4, 5, 6],
|
16
|
+
"month_names" => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
|
17
|
+
"abbr_month_names" => [0 , 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
|
18
|
+
},
|
19
|
+
"time" => {
|
20
|
+
"formats" => {
|
21
|
+
"default" => "%a, %d %b %Y %H:%M:%S %z",
|
22
|
+
"short" => "%d %b %H:%M",
|
23
|
+
"long" => "%B %d, %Y %H:%M"
|
24
|
+
},
|
25
|
+
"am" => "am",
|
26
|
+
"pm" => "pm"
|
27
|
+
}
|
28
|
+
}
|
29
|
+
end
|
30
|
+
end
|
@@ -1,4 +1,27 @@
|
|
1
1
|
namespace :gettext_simple_rails do
|
2
|
+
task "generate_translator_files" => :environment do
|
3
|
+
GettextSimpleRails::Translators.load_all.each do |translator_data|
|
4
|
+
translator = translator_data[:class].new
|
5
|
+
next unless translator.detected?
|
6
|
+
|
7
|
+
translation_path = "#{GettextSimpleRails.translation_dir}/#{File.basename(translator_data[:file], ".rb")}_translations.rb"
|
8
|
+
|
9
|
+
puts "Generating translation for #{translator.class.name} in #{translation_path}"
|
10
|
+
|
11
|
+
FileUtils.mkdir_p(File.dirname(translation_path)) unless File.exists?(File.dirname(translation_path))
|
12
|
+
|
13
|
+
File.open(translation_path, "w") do |fp|
|
14
|
+
fp.puts "class GettextSimpleRails::MonthNames"
|
15
|
+
fp.puts " def translations"
|
16
|
+
|
17
|
+
GettextSimpleRails.write_recursive_translations(fp, translator.translations)
|
18
|
+
|
19
|
+
fp.puts " end"
|
20
|
+
fp.puts "end"
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
2
25
|
task "generate_model_translation_files" => :environment do
|
3
26
|
GettextSimpleRails::ModelInspector.model_classes do |inspector|
|
4
27
|
translation_path = "#{GettextSimpleRails.translation_dir}/models/#{inspector.snake_name}_model_translations.rb"
|
@@ -0,0 +1,24 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
|
3
|
+
describe GettextSimpleRails::Translators::DateTranslator do
|
4
|
+
before do
|
5
|
+
if !$translations_generated_for_date_translator
|
6
|
+
$translations_generated_for_date_translator = true
|
7
|
+
|
8
|
+
# Make it possible to call the Rake task.
|
9
|
+
::Dummy::Application.load_tasks
|
10
|
+
|
11
|
+
# Clean up any existing translations.
|
12
|
+
FileUtils.rm_r(GettextSimpleRails.translation_dir) if File.exists?(GettextSimpleRails.translation_dir)
|
13
|
+
|
14
|
+
# Generate model translations so we can check them.
|
15
|
+
::Rake::Task["gettext_simple_rails:generate_translator_files"].invoke
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
it "generates month names translations" do
|
20
|
+
filepath = "#{GettextSimpleRails.translation_dir}/date_translator_translations.rb"
|
21
|
+
cont = File.read(filepath)
|
22
|
+
cont.should include "_('date.day_names.6')"
|
23
|
+
end
|
24
|
+
end
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|
File without changes
|