gettext_simple_rails 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/config/default.po +11 -0
- data/config/poedit_ruby_parser.conf +6 -0
- data/lib/gettext_simple_rails/cache_handler.rb +45 -0
- data/lib/gettext_simple_rails/i18n_injector.rb +22 -3
- data/lib/gettext_simple_rails/model_inspector.rb +1 -5
- data/lib/gettext_simple_rails/setup_helper.rb +50 -0
- data/lib/gettext_simple_rails/version.rb +1 -1
- data/lib/gettext_simple_rails.rb +18 -0
- data/lib/tasks/gettext_simple_rails_tasks.rake +53 -3
- data/spec/active_record_spec.rb +8 -12
- data/spec/cache_handler_spec.rb +26 -0
- data/spec/date_translator_spec.rb +8 -12
- data/spec/dummy/config/locales_gettext/da/LC_MESSAGES/default.mo +0 -0
- data/spec/dummy/config/locales_gettext/da/LC_MESSAGES/default.po +361 -0
- data/spec/dummy/config/locales_gettext/en/LC_MESSAGES/default.po +11 -0
- data/spec/dummy/config/locales_gettext/static_translation_file.json +1 -0
- data/spec/dummy/lib/gettext_simple_rails/models/role_model_translations.rb +21 -0
- data/spec/dummy/lib/gettext_simple_rails/models/user_model_translations.rb +22 -0
- data/spec/dummy/log/test.log +193 -0
- data/spec/gettext_simple_rails_spec.rb +8 -12
- data/spec/poedit_config_with_ruby.conf +116 -0
- data/spec/poedit_config_without_ruby.conf +101 -0
- data/spec/setup_helper_spec.rb +41 -0
- metadata +25 -7
- data/spec/dummy/lib/gettext_simple_rails/active_record_translator_translations.rb +0 -18
- data/spec/dummy/lib/gettext_simple_rails/date_translator_translations.rb +0 -60
- data/spec/dummy/lib/gettext_simple_rails/number_translator_translations.rb +0 -48
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 290c37e74f1b7060f1649328790356f73f7e57db
|
4
|
+
data.tar.gz: fea853034aee895ed19ef44075f954bd4bc2b4b3
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: de85e09d2606b44b9f846ae09806e2dc6fe443ff6fb8e5ab17ef43c30725e0f25894a3ec01704192412e97c775d9fee5f418bb117ffe360d19a52b0d54a468d2
|
7
|
+
data.tar.gz: a1efaa6beb26249ef35bb37a2af13986fd1bd74dd78f28bd80db5d748d7c4ffa7221320fa7653494f58febf1319ced88c4945fa4a11d7ce9709821458d1dfa77
|
data/config/default.po
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
msgid ""
|
2
|
+
msgstr ""
|
3
|
+
"Project-Id-Version: %{PROJECT_NAME}\n"
|
4
|
+
"Language: %{LOCALE}\n"
|
5
|
+
"MIME-Version: 1.0\n"
|
6
|
+
"Content-Type: text/plain; charset=UTF-8\n"
|
7
|
+
"Content-Transfer-Encoding: 8bit\n"
|
8
|
+
"X-Poedit-KeywordsList: _;gettext;gettext_noop\n"
|
9
|
+
"X-Poedit-Basepath: ../../../../\n"
|
10
|
+
"X-Poedit-SourceCharset: UTF-8\n"
|
11
|
+
"X-Poedit-SearchPath-0: .\n"
|
@@ -0,0 +1,45 @@
|
|
1
|
+
class GettextSimpleRails::CacheHandler
|
2
|
+
def newest_po_file
|
3
|
+
newest_time = nil
|
4
|
+
newest_path = nil
|
5
|
+
|
6
|
+
I18n.available_locales.each do |locale|
|
7
|
+
default_path_file = "#{Rails.root}/config/locales_gettext/#{locale}/LC_MESSAGES/default.po"
|
8
|
+
next unless File.exists?(default_path_file)
|
9
|
+
time = File.mtime(default_path_file)
|
10
|
+
newest_time = time if newest_time == nil || time > newest_time
|
11
|
+
newest_path = default_path_file
|
12
|
+
end
|
13
|
+
|
14
|
+
return newest_path
|
15
|
+
end
|
16
|
+
|
17
|
+
def static_cache_file_path
|
18
|
+
"#{Rails.root}/config/locales_gettext/static_translation_file.json"
|
19
|
+
end
|
20
|
+
|
21
|
+
def cache_file_too_old?
|
22
|
+
if !File.exists?(static_cache_file_path)
|
23
|
+
return true
|
24
|
+
elsif File.mtime(static_cache_file_path) < File.mtime(newest_po_file)
|
25
|
+
return true
|
26
|
+
else
|
27
|
+
return false
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def write_static_translation_file
|
32
|
+
require "gettext_simple"
|
33
|
+
gs = GettextSimple.new(:i18n => true)
|
34
|
+
gs.load_dir("#{Rails.root}/config/locales_gettext")
|
35
|
+
gs.register_kernel_methods
|
36
|
+
|
37
|
+
injector = GettextSimpleRails::I18nInjector.new(:store_in_hash => true)
|
38
|
+
injector.inject_model_translations(gs)
|
39
|
+
injector.inject_translator_translations(gs)
|
40
|
+
|
41
|
+
File.open(static_cache_file_path, "w") do |fp|
|
42
|
+
fp.write(injector.store_hash.to_json)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -1,7 +1,11 @@
|
|
1
1
|
class GettextSimpleRails::I18nInjector
|
2
|
+
attr_reader :store_hash
|
3
|
+
|
2
4
|
def initialize(args = {})
|
5
|
+
@args = args
|
3
6
|
@debug = args[:debug]
|
4
7
|
@i18n_backend = I18n.config.backend
|
8
|
+
@store_hash = {} if args[:store_in_hash]
|
5
9
|
end
|
6
10
|
|
7
11
|
def inject_translator_translations(gettext_simple)
|
@@ -72,11 +76,18 @@ class GettextSimpleRails::I18nInjector
|
|
72
76
|
end
|
73
77
|
end
|
74
78
|
|
75
|
-
|
79
|
+
store_translations(locale, data)
|
76
80
|
end
|
77
81
|
end
|
78
82
|
end
|
79
83
|
|
84
|
+
def inject_from_static_translation_file(args)
|
85
|
+
translation_hash = JSON.parse(File.read(args[:path]))
|
86
|
+
translation_hash.each do |locale, language_hash|
|
87
|
+
store_translations(locale, language_hash)
|
88
|
+
end
|
89
|
+
end
|
90
|
+
|
80
91
|
private
|
81
92
|
|
82
93
|
def debug(str)
|
@@ -107,7 +118,7 @@ private
|
|
107
118
|
end
|
108
119
|
end
|
109
120
|
|
110
|
-
|
121
|
+
store_translations(locale, translation_hash)
|
111
122
|
end
|
112
123
|
else
|
113
124
|
raise "Unknown class: '#{translations.class.name}'."
|
@@ -134,6 +145,14 @@ private
|
|
134
145
|
end
|
135
146
|
end
|
136
147
|
|
137
|
-
|
148
|
+
store_translations(locale, translation_hash)
|
149
|
+
end
|
150
|
+
|
151
|
+
def store_translations(locale, translation_hash)
|
152
|
+
if @args[:store_in_hash]
|
153
|
+
@store_hash.deep_merge!({locale.to_sym => translation_hash})
|
154
|
+
else
|
155
|
+
@i18n_backend.store_translations(locale.to_sym, translation_hash)
|
156
|
+
end
|
138
157
|
end
|
139
158
|
end
|
@@ -1,7 +1,6 @@
|
|
1
1
|
class GettextSimpleRails::ModelInspector
|
2
2
|
def self.model_classes
|
3
3
|
clazzes = []
|
4
|
-
|
5
4
|
::Rails.application.eager_load!
|
6
5
|
|
7
6
|
::Object.constants.each do |clazz|
|
@@ -25,10 +24,7 @@ class GettextSimpleRails::ModelInspector
|
|
25
24
|
end
|
26
25
|
|
27
26
|
def paperclip_attachments
|
28
|
-
|
29
|
-
return []
|
30
|
-
end
|
31
|
-
|
27
|
+
return [] unless ::Kernel.const_defined?("Paperclip")
|
32
28
|
Paperclip::AttachmentRegistry.names_for(@clazz).each do |name|
|
33
29
|
yield(name)
|
34
30
|
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
class GettextSimpleRails::SetupHelper
|
2
|
+
def initialize(args)
|
3
|
+
@args = args
|
4
|
+
end
|
5
|
+
|
6
|
+
def poedit_config_path
|
7
|
+
if @args[:poedit_config_path]
|
8
|
+
return @args[:poedit_config_path]
|
9
|
+
else
|
10
|
+
"#{ENV["HOME"]}/.poedit/config"
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def poedit_config_has_ruby_parser?
|
15
|
+
config_content = File.read(poedit_config_path)
|
16
|
+
return config_content.include?("[Parsers/Ruby]")
|
17
|
+
end
|
18
|
+
|
19
|
+
def poedit_add_ruby_parser_to_config
|
20
|
+
sample_path = "#{File.dirname(__FILE__)}/../../config/poedit_ruby_parser.conf"
|
21
|
+
sample_content = File.read(sample_path).strip
|
22
|
+
config_content = File.read(poedit_config_path).strip
|
23
|
+
|
24
|
+
File.open(poedit_config_path, "w") do |fp|
|
25
|
+
fp.puts(config_content)
|
26
|
+
fp.puts(sample_content)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def poedit_config_has_ruby_in_list_of_parsers?
|
31
|
+
config_content = File.read(poedit_config_path).strip
|
32
|
+
parsers_list_match = config_content.match(/\[Parsers\]\s+List=(.+?)\n/)
|
33
|
+
list = parsers_list_match[1].split(";")
|
34
|
+
return list.include?("Ruby")
|
35
|
+
end
|
36
|
+
|
37
|
+
def poedit_config_add_ruby_to_list_of_parsers
|
38
|
+
config_content = File.read(poedit_config_path).strip
|
39
|
+
parsers_list_match = config_content.match(/\[Parsers\]\s+List=(.+?)\n/)
|
40
|
+
list = parsers_list_match[1].split(";")
|
41
|
+
list << "Ruby"
|
42
|
+
|
43
|
+
new_config = config_content.gsub(parsers_list_match[0], "[Parsers]\nList=#{list.join(";")}\n")
|
44
|
+
raise "Could not add Ruby to list of parsers." if new_config == config_content
|
45
|
+
|
46
|
+
File.open(poedit_config_path, "w") do |fp|
|
47
|
+
fp.puts(new_config)
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
data/lib/gettext_simple_rails.rb
CHANGED
@@ -36,6 +36,24 @@ module GettextSimpleRails
|
|
36
36
|
end
|
37
37
|
end
|
38
38
|
|
39
|
+
def self.init
|
40
|
+
require "gettext_simple"
|
41
|
+
gettext_simple = GettextSimple.new(:i18n => true)
|
42
|
+
gettext_simple.load_dir("#{Rails.root}/config/locales_gettext")
|
43
|
+
gettext_simple.register_kernel_methods
|
44
|
+
|
45
|
+
injector = GettextSimpleRails::I18nInjector.new
|
46
|
+
cache_handler = GettextSimpleRails::CacheHandler.new
|
47
|
+
|
48
|
+
if cache_handler.cache_file_too_old?
|
49
|
+
puts "[GettextSimpleRails] Cache file too old - regenerating."
|
50
|
+
cache_handler.write_static_translation_file
|
51
|
+
end
|
52
|
+
|
53
|
+
puts "[GettextSimpleRails] Injecting translations from cache."
|
54
|
+
injector.inject_from_static_translation_file(:path => cache_handler.static_cache_file_path)
|
55
|
+
end
|
56
|
+
|
39
57
|
class Translators
|
40
58
|
def self.const_missing(name)
|
41
59
|
require "#{::File.dirname(__FILE__)}/gettext_simple_rails/translators/#{::StringCases.camel_to_snake(name)}"
|
@@ -41,7 +41,6 @@ namespace :gettext_simple_rails do
|
|
41
41
|
end
|
42
42
|
|
43
43
|
fp.puts " end"
|
44
|
-
|
45
44
|
fp.puts ""
|
46
45
|
fp.puts " def self.relationship_translations"
|
47
46
|
|
@@ -50,7 +49,6 @@ namespace :gettext_simple_rails do
|
|
50
49
|
end
|
51
50
|
|
52
51
|
fp.puts " end"
|
53
|
-
|
54
52
|
fp.puts ""
|
55
53
|
fp.puts " def self.paperclip_attachments"
|
56
54
|
|
@@ -59,7 +57,6 @@ namespace :gettext_simple_rails do
|
|
59
57
|
end
|
60
58
|
|
61
59
|
fp.puts " end"
|
62
|
-
|
63
60
|
fp.puts ""
|
64
61
|
fp.puts " def self.model_name"
|
65
62
|
fp.puts " puts _('#{inspector.gettext_key}.one')"
|
@@ -70,4 +67,57 @@ namespace :gettext_simple_rails do
|
|
70
67
|
end
|
71
68
|
end
|
72
69
|
end
|
70
|
+
|
71
|
+
task "create" => :environment do
|
72
|
+
require "fileutils"
|
73
|
+
I18n.available_locales.each do |locale|
|
74
|
+
dir_path = "#{Rails.root}/config/locales_gettext/#{locale}/LC_MESSAGES"
|
75
|
+
file_path = "#{dir_path}/default.po"
|
76
|
+
|
77
|
+
if File.exists?(file_path)
|
78
|
+
puts "Skipping #{locale} because a .po-file already exists in: #{file_path}"
|
79
|
+
next
|
80
|
+
end
|
81
|
+
|
82
|
+
FileUtils.mkdir_p(dir_path) unless File.exists?(dir_path)
|
83
|
+
|
84
|
+
project_name = Rails.application.class.parent_name
|
85
|
+
|
86
|
+
content = File.read("#{File.dirname(__FILE__)}/../../config/default.po")
|
87
|
+
content.gsub!("%{PROJECT_NAME}", project_name)
|
88
|
+
content.gsub!("%{LOCALE}", locale.to_s)
|
89
|
+
|
90
|
+
File.open(file_path, "w") do |fp|
|
91
|
+
fp.write(content)
|
92
|
+
end
|
93
|
+
|
94
|
+
puts "Created default config for #{locale} in: #{file_path}"
|
95
|
+
end
|
96
|
+
end
|
97
|
+
|
98
|
+
task "create_poedit_ruby_parser" do
|
99
|
+
setup_helper = GettextSimpleRails::SetupHelper
|
100
|
+
raise "POEdit config file could not be found in: #{setup_helper.poedit_config_path}" unless File.exists?(setup_helper.poedit_config_path)
|
101
|
+
|
102
|
+
if setup_helper.poedit_config_has_ruby_parser?
|
103
|
+
puts "Ruby parser already exists."
|
104
|
+
else
|
105
|
+
setup_helper.poedit_add_ruby_parser_to_config
|
106
|
+
puts "Added Ruby-parser to config."
|
107
|
+
end
|
108
|
+
|
109
|
+
if setup_helper.poedit_config_has_ruby_in_list_of_parsers?
|
110
|
+
puts "Ruby already in list of parsers."
|
111
|
+
else
|
112
|
+
setup_helper.poedit_config_add_ruby_to_list_of_parsers
|
113
|
+
puts "Added Ruby to list of parsers."
|
114
|
+
end
|
115
|
+
end
|
116
|
+
|
117
|
+
task "generate_static_translation_file" => :environment do
|
118
|
+
cache_handler = GettextSimpleRails::CacheHandler.new
|
119
|
+
cache_handler.write_static_translation_file
|
120
|
+
|
121
|
+
puts "Static translation file saved in #{cache_handler.static_cache_file_path}"
|
122
|
+
end
|
73
123
|
end
|
data/spec/active_record_spec.rb
CHANGED
@@ -2,18 +2,14 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe GettextSimpleRails::Translators::ActiveRecordTranslator do
|
4
4
|
before do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# Generate model translations so we can check them.
|
15
|
-
::Rake::Task["gettext_simple_rails:generate_translator_files"].invoke
|
16
|
-
end
|
5
|
+
# Make it possible to call the Rake task.
|
6
|
+
::Dummy::Application.load_tasks
|
7
|
+
|
8
|
+
# Clean up any existing translations.
|
9
|
+
FileUtils.rm_r(GettextSimpleRails.translation_dir) if File.exists?(GettextSimpleRails.translation_dir)
|
10
|
+
|
11
|
+
# Generate model translations so we can check them.
|
12
|
+
::Rake::Task["gettext_simple_rails:generate_translator_files"].execute
|
17
13
|
end
|
18
14
|
|
19
15
|
it "should generate translations for validations" do
|
@@ -0,0 +1,26 @@
|
|
1
|
+
require "spec_helper"
|
2
|
+
require "rake"
|
3
|
+
require "fileutils"
|
4
|
+
|
5
|
+
describe GettextSimpleRails::CacheHandler do
|
6
|
+
before do
|
7
|
+
::Dummy::Application.load_tasks
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should register when the cache is too old" do
|
11
|
+
static_translation_file_path = "#{Rails.root}/config/locales_gettext/static_translation_file.json"
|
12
|
+
File.unlink(static_translation_file_path) if File.exists?(static_translation_file_path)
|
13
|
+
::Rake::Task["gettext_simple_rails:generate_translator_files"].execute
|
14
|
+
::Rake::Task["gettext_simple_rails:generate_static_translation_file"].execute
|
15
|
+
static_file_time = File.mtime(static_translation_file_path)
|
16
|
+
|
17
|
+
cache_handler = GettextSimpleRails::CacheHandler.new
|
18
|
+
time = File.mtime(cache_handler.newest_po_file)
|
19
|
+
assert time < static_file_time
|
20
|
+
cache_handler.cache_file_too_old?.should eq false
|
21
|
+
|
22
|
+
sleep 1
|
23
|
+
FileUtils.touch(cache_handler.newest_po_file)
|
24
|
+
cache_handler.cache_file_too_old?.should eq true
|
25
|
+
end
|
26
|
+
end
|
@@ -2,18 +2,14 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe GettextSimpleRails::Translators::DateTranslator do
|
4
4
|
before do
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
# Generate model translations so we can check them.
|
15
|
-
::Rake::Task["gettext_simple_rails:generate_translator_files"].invoke
|
16
|
-
end
|
5
|
+
# Make it possible to call the Rake task.
|
6
|
+
::Dummy::Application.load_tasks
|
7
|
+
|
8
|
+
# Clean up any existing translations.
|
9
|
+
FileUtils.rm_r(GettextSimpleRails.translation_dir) if File.exists?(GettextSimpleRails.translation_dir)
|
10
|
+
|
11
|
+
# Generate model translations so we can check them.
|
12
|
+
::Rake::Task["gettext_simple_rails:generate_translator_files"].execute
|
17
13
|
end
|
18
14
|
|
19
15
|
it "generates month names translations" do
|
Binary file
|