gettext_simple_rails 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (65) hide show
  1. checksums.yaml +4 -4
  2. data/Rakefile +2 -2
  3. data/lib/gettext_simple_rails.rb +47 -0
  4. data/lib/gettext_simple_rails/i18n_injector.rb +73 -3
  5. data/lib/gettext_simple_rails/translators/active_admin_translator.rb +22 -0
  6. data/lib/gettext_simple_rails/translators/date_translator.rb +30 -0
  7. data/lib/gettext_simple_rails/translators/devise_translator.rb +9 -0
  8. data/lib/gettext_simple_rails/translators/simple_form_translator.rb +9 -0
  9. data/lib/gettext_simple_rails/version.rb +1 -1
  10. data/lib/tasks/gettext_simple_rails_tasks.rake +23 -0
  11. data/spec/date_translator_spec.rb +24 -0
  12. data/{test → spec}/dummy/README.rdoc +0 -0
  13. data/{test → spec}/dummy/Rakefile +0 -0
  14. data/{test → spec}/dummy/app/assets/javascripts/application.js +0 -0
  15. data/{test → spec}/dummy/app/assets/stylesheets/application.css +0 -0
  16. data/{test → spec}/dummy/app/controllers/application_controller.rb +0 -0
  17. data/{test → spec}/dummy/app/helpers/application_helper.rb +0 -0
  18. data/{test → spec}/dummy/app/models/role.rb +0 -0
  19. data/{test → spec}/dummy/app/models/user.rb +0 -0
  20. data/{test → spec}/dummy/app/views/layouts/application.html.erb +0 -0
  21. data/{test → spec}/dummy/bin/bundle +0 -0
  22. data/{test → spec}/dummy/bin/rails +0 -0
  23. data/{test → spec}/dummy/bin/rake +0 -0
  24. data/{test → spec}/dummy/config.ru +0 -0
  25. data/{test → spec}/dummy/config/application.rb +0 -0
  26. data/{test → spec}/dummy/config/boot.rb +0 -0
  27. data/{test → spec}/dummy/config/database.yml +0 -0
  28. data/{test → spec}/dummy/config/environment.rb +0 -0
  29. data/{test → spec}/dummy/config/environments/development.rb +0 -0
  30. data/{test → spec}/dummy/config/environments/production.rb +0 -0
  31. data/{test → spec}/dummy/config/environments/test.rb +0 -0
  32. data/{test → spec}/dummy/config/initializers/backtrace_silencers.rb +0 -0
  33. data/{test → spec}/dummy/config/initializers/cookies_serializer.rb +0 -0
  34. data/{test → spec}/dummy/config/initializers/filter_parameter_logging.rb +0 -0
  35. data/{test → spec}/dummy/config/initializers/inflections.rb +0 -0
  36. data/{test → spec}/dummy/config/initializers/mime_types.rb +0 -0
  37. data/{test → spec}/dummy/config/initializers/session_store.rb +0 -0
  38. data/{test → spec}/dummy/config/initializers/wrap_parameters.rb +0 -0
  39. data/{test → spec}/dummy/config/locales/da.yml +0 -0
  40. data/{test → spec}/dummy/config/locales/en.yml +0 -0
  41. data/{test → spec}/dummy/config/routes.rb +0 -0
  42. data/{test → spec}/dummy/config/secrets.yml +0 -0
  43. data/{test → spec}/dummy/db/development.sqlite3 +0 -0
  44. data/{test → spec}/dummy/db/migrate/20140410170418_create_users.rb +0 -0
  45. data/{test → spec}/dummy/db/migrate/20140411112622_create_roles.rb +0 -0
  46. data/{test → spec}/dummy/db/schema.rb +0 -0
  47. data/{test → spec}/dummy/db/test.sqlite3 +0 -0
  48. data/{test → spec}/dummy/lib/gettext_simple_rails/models/role_model_translations.rb +3 -0
  49. data/{test → spec}/dummy/lib/gettext_simple_rails/models/user_model_translations.rb +3 -0
  50. data/{test → spec}/dummy/log/development.log +0 -0
  51. data/{test → spec}/dummy/public/404.html +0 -0
  52. data/{test → spec}/dummy/public/422.html +0 -0
  53. data/{test → spec}/dummy/public/500.html +0 -0
  54. data/{test → spec}/dummy/public/favicon.ico +0 -0
  55. data/{test → spec}/dummy/test/fixtures/roles.yml +0 -0
  56. data/{test → spec}/dummy/test/fixtures/users.yml +0 -0
  57. data/{test → spec}/dummy/test/models/role_test.rb +0 -0
  58. data/{test → spec}/dummy/test/models/user_test.rb +0 -0
  59. data/{test/gettext_simple_rails_test.rb → spec/gettext_simple_rails_spec.rb} +5 -5
  60. data/spec/spec_helper.rb +45 -0
  61. data/{test → spec}/test_helper.rb +0 -0
  62. metadata +122 -105
  63. data/lib/gettext_simple_rails/simple_form_detector.rb +0 -9
  64. data/test/dummy/log/test.log +0 -630
  65. data/test/integration/navigation_test.rb +0 -10
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0e38c32669d7fd2af3f78ff0ae9c42ef6fc695b9
4
- data.tar.gz: ef1955873b59221d9fe492f46c3a38a41178c1da
3
+ metadata.gz: a7f781bf7138e3d82a21dc6c3ebb57bc2b1d7630
4
+ data.tar.gz: 7fc8c0216bf251ac18adc7b0502e81448b43de37
5
5
  SHA512:
6
- metadata.gz: ef56d940a537a34f4b4cd944cb9975a9bc81d446b77a0a2eb0f8e0f71d60dd0c55ba3f673c9715bb31b8f0a2443a42cdb977f639af54b22ffb9e62d9024e3525
7
- data.tar.gz: 4e5c63b37c29ca79ee3fd3d3369e03dda61c77b79dc5c7b0311cb5d7046fef1557fe092628ba8653e1946afbfb1c7d13e80bf8a1e3ec43ad75aea3fbb3b73be8
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("../test/dummy/Rakefile", __FILE__)
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 = 'test/**/*_test.rb'
29
+ t.pattern = 'spec/**/*_test.rb'
30
30
  t.verbose = false
31
31
  end
32
32
 
@@ -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 inject_model_translations(gettext_simple)
7
- i18n_backend = I18n.config.backend
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
@@ -0,0 +1,9 @@
1
+ class GettextSimpleRails::Translators::DeviseTranslator
2
+ def detected?
3
+ return ::Kernel.const_defined?("Devise")
4
+ end
5
+
6
+ def translations
7
+ return {}
8
+ end
9
+ end
@@ -0,0 +1,9 @@
1
+ class GettextSimpleRails::Translators::SimpleFormTranslator
2
+ def detected?
3
+ return ::Kernel.const_defined?("SimpleForm")
4
+ end
5
+
6
+ def translations
7
+ return {}
8
+ end
9
+ end
@@ -1,3 +1,3 @@
1
1
  module GettextSimpleRails
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  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
@@ -11,6 +11,9 @@ class GettextSimpleRails::UserModelTranslations
11
11
  puts _('models.attributes.role.user')
12
12
  end
13
13
 
14
+ def self.paperclip_attachments
15
+ end
16
+
14
17
  def self.model_name
15
18
  puts _('models.name.role.one')
16
19
  puts _('models.name.role.other')
@@ -12,6 +12,9 @@ class GettextSimpleRails::UserModelTranslations
12
12
  puts _('models.attributes.user.roles')
13
13
  end
14
14
 
15
+ def self.paperclip_attachments
16
+ end
17
+
15
18
  def self.model_name
16
19
  puts _('models.name.user.one')
17
20
  puts _('models.name.user.other')
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes