gettext_simple_rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (67) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/Rakefile +34 -0
  4. data/app/assets/javascripts/gettext_simple_rails/application.js +13 -0
  5. data/app/assets/stylesheets/gettext_simple_rails/application.css +15 -0
  6. data/app/controllers/gettext_simple_rails/application_controller.rb +4 -0
  7. data/app/helpers/gettext_simple_rails/application_helper.rb +4 -0
  8. data/app/views/layouts/gettext_simple_rails/application.html.erb +14 -0
  9. data/config/routes.rb +2 -0
  10. data/lib/gettext_simple_rails.rb +14 -0
  11. data/lib/gettext_simple_rails/engine.rb +5 -0
  12. data/lib/gettext_simple_rails/i18n_injector.rb +69 -0
  13. data/lib/gettext_simple_rails/model_inspector.rb +66 -0
  14. data/lib/gettext_simple_rails/version.rb +3 -0
  15. data/lib/tasks/gettext_simple_rails_tasks.rake +41 -0
  16. data/test/dummy/README.rdoc +28 -0
  17. data/test/dummy/Rakefile +6 -0
  18. data/test/dummy/app/assets/javascripts/application.js +13 -0
  19. data/test/dummy/app/assets/stylesheets/application.css +15 -0
  20. data/test/dummy/app/controllers/application_controller.rb +5 -0
  21. data/test/dummy/app/helpers/application_helper.rb +2 -0
  22. data/test/dummy/app/models/role.rb +3 -0
  23. data/test/dummy/app/models/user.rb +3 -0
  24. data/test/dummy/app/views/layouts/application.html.erb +14 -0
  25. data/test/dummy/bin/bundle +3 -0
  26. data/test/dummy/bin/rails +4 -0
  27. data/test/dummy/bin/rake +4 -0
  28. data/test/dummy/config.ru +4 -0
  29. data/test/dummy/config/application.rb +23 -0
  30. data/test/dummy/config/boot.rb +5 -0
  31. data/test/dummy/config/database.yml +25 -0
  32. data/test/dummy/config/environment.rb +5 -0
  33. data/test/dummy/config/environments/development.rb +37 -0
  34. data/test/dummy/config/environments/production.rb +83 -0
  35. data/test/dummy/config/environments/test.rb +39 -0
  36. data/test/dummy/config/initializers/backtrace_silencers.rb +7 -0
  37. data/test/dummy/config/initializers/cookies_serializer.rb +3 -0
  38. data/test/dummy/config/initializers/filter_parameter_logging.rb +4 -0
  39. data/test/dummy/config/initializers/inflections.rb +16 -0
  40. data/test/dummy/config/initializers/mime_types.rb +4 -0
  41. data/test/dummy/config/initializers/session_store.rb +3 -0
  42. data/test/dummy/config/initializers/wrap_parameters.rb +14 -0
  43. data/test/dummy/config/locales/da.yml +2 -0
  44. data/test/dummy/config/locales/en.yml +23 -0
  45. data/test/dummy/config/routes.rb +4 -0
  46. data/test/dummy/config/secrets.yml +22 -0
  47. data/test/dummy/db/development.sqlite3 +0 -0
  48. data/test/dummy/db/migrate/20140410170418_create_users.rb +11 -0
  49. data/test/dummy/db/migrate/20140411112622_create_roles.rb +10 -0
  50. data/test/dummy/db/schema.rb +38 -0
  51. data/test/dummy/db/test.sqlite3 +0 -0
  52. data/test/dummy/lib/gettext_simple_rails/models/role_model_translations.rb +18 -0
  53. data/test/dummy/lib/gettext_simple_rails/models/user_model_translations.rb +19 -0
  54. data/test/dummy/log/development.log +26 -0
  55. data/test/dummy/log/test.log +630 -0
  56. data/test/dummy/public/404.html +67 -0
  57. data/test/dummy/public/422.html +67 -0
  58. data/test/dummy/public/500.html +66 -0
  59. data/test/dummy/public/favicon.ico +0 -0
  60. data/test/dummy/test/fixtures/roles.yml +9 -0
  61. data/test/dummy/test/fixtures/users.yml +11 -0
  62. data/test/dummy/test/models/role_test.rb +7 -0
  63. data/test/dummy/test/models/user_test.rb +7 -0
  64. data/test/gettext_simple_rails_test.rb +58 -0
  65. data/test/integration/navigation_test.rb +10 -0
  66. data/test/test_helper.rb +15 -0
  67. metadata +217 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 8f86be4cc31cf06bfca2ded7ec383931acf7bbee
4
+ data.tar.gz: b08add1067098dfc01d24c2a9eebd25e56f47006
5
+ SHA512:
6
+ metadata.gz: 8b13ef7dcfa94085a1d76f4575a35c91f59a946a6a750c54d28f5d484777b619bc7661e2d11ac19e5a293e827e9368b60398ef85f863a89661a62abe1dfa065b
7
+ data.tar.gz: 6470cd0e81c04a29d14f46f4cd6a1422ec20b90d52c43c1e992e466a939f449dd7e2621e59c154514511826dfcd312e278068853d28df8b032d62468984830a7
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2014 YOURNAME
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,34 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'GettextSimpleRails'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.rdoc')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+
22
+ Bundler::GemHelper.install_tasks
23
+
24
+ require 'rake/testtask'
25
+
26
+ Rake::TestTask.new(:test) do |t|
27
+ t.libs << 'lib'
28
+ t.libs << 'test'
29
+ t.pattern = 'test/**/*_test.rb'
30
+ t.verbose = false
31
+ end
32
+
33
+
34
+ task default: :test
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,4 @@
1
+ module GettextSimpleRails
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module GettextSimpleRails
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>GettextSimpleRails</title>
5
+ <%= stylesheet_link_tag "gettext_simple_rails/application", media: "all" %>
6
+ <%= javascript_include_tag "gettext_simple_rails/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
data/config/routes.rb ADDED
@@ -0,0 +1,2 @@
1
+ GettextSimpleRails::Engine.routes.draw do
2
+ end
@@ -0,0 +1,14 @@
1
+ require "gettext_simple_rails/engine"
2
+ require "string-cases"
3
+
4
+ module GettextSimpleRails
5
+ def self.const_missing(name)
6
+ require "#{::File.dirname(__FILE__)}/gettext_simple_rails/#{::StringCases.camel_to_snake(name)}"
7
+ raise LoadError, "Still not loaded: '#{name}'." unless ::GettextSimpleRails.const_defined?(name)
8
+ return ::GettextSimpleRails.const_get(name)
9
+ end
10
+
11
+ def self.translation_dir
12
+ return "#{Rails.root}/lib/gettext_simple_rails"
13
+ end
14
+ end
@@ -0,0 +1,5 @@
1
+ module GettextSimpleRails
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace GettextSimpleRails
4
+ end
5
+ end
@@ -0,0 +1,69 @@
1
+ class GettextSimpleRails::I18nInjector
2
+ def initialize(args = {})
3
+ @debug = args[:debug]
4
+ end
5
+
6
+ def inject_model_translations(gettext_simple)
7
+ i18n_backend = I18n.config.backend
8
+
9
+ GettextSimpleRails::ModelInspector.model_classes do |inspector|
10
+ model = {}
11
+ attributes = {}
12
+
13
+ data = {
14
+ "activerecord" => {
15
+ "models" => {
16
+ inspector.snake_name => model
17
+ },
18
+ "attributes" => {
19
+ inspector.snake_name => attributes
20
+ }
21
+ }
22
+ }
23
+
24
+ I18n.available_locales.each do |locale|
25
+ locale = locale.to_s
26
+
27
+ unless gettext_simple.locale_exists?(locale)
28
+ debug "Skipping locale because it hasn't been loaded into GettextSimple: #{locale}" if @debug
29
+ next
30
+ end
31
+
32
+ debug "Loading locale: #{locale}" if @debug
33
+
34
+ one_translation = gettext_simple.translate_with_locale(locale, inspector.gettext_key_one)
35
+ if one_translation != inspector.gettext_key_one
36
+ model["one"] = one_translation
37
+ end
38
+
39
+ other_translation = gettext_simple.translate_with_locale(locale, inspector.gettext_key_other)
40
+ if other_translation != inspector.gettext_key_other
41
+ model["other"] = other_translation
42
+ end
43
+
44
+ inspector.attributes do |attribute|
45
+ attribute_translation = gettext_simple.translate_with_locale(locale, attribute.gettext_key)
46
+ if attribute_translation != attribute.gettext_key
47
+ debug "Translating #{inspector.snake_name}.#{attribute.name} to #{attribute_translation}" if @debug
48
+ attributes[attribute.name.to_s] = attribute_translation
49
+ else
50
+ debug "Skipping #{inspector.snake_name}.#{attribute.name}" if @debug
51
+ end
52
+ end
53
+
54
+ inspector.relationships do |name, reflection|
55
+ relationship_translation = gettext_simple.translate_with_locale(locale, inspector.relationship_gettext_key(name))
56
+ if relationship_translation != inspector.relationship_gettext_key(name)
57
+ attributes[name.to_s] = relationship_translation
58
+ end
59
+ end
60
+
61
+ i18n_backend.store_translations(locale.to_sym, data)
62
+ end
63
+ end
64
+ end
65
+
66
+ def debug(str)
67
+ $stderr.puts str if @debug
68
+ end
69
+ end
@@ -0,0 +1,66 @@
1
+ class GettextSimpleRails::ModelInspector
2
+ def self.model_classes
3
+ clazzes = []
4
+
5
+ ::Rails.application.eager_load!
6
+
7
+ ::Object.constants.each do |clazz|
8
+ clazz = clazz.to_s.constantize
9
+ next unless clazz.class == Class
10
+ next unless clazz < ActiveRecord::Base
11
+ yield ::GettextSimpleRails::ModelInspector.new(clazz)
12
+ end
13
+ end
14
+
15
+ attr_reader :clazz
16
+
17
+ def initialize(clazz)
18
+ @clazz = clazz
19
+ end
20
+
21
+ def attributes
22
+ @clazz.attribute_names.each do |attribute_name|
23
+ yield ::GettextSimpleRails::ModelInspector::Attribute.new(self, attribute_name)
24
+ end
25
+ end
26
+
27
+ def snake_name
28
+ return ::StringCases.camel_to_snake(clazz.name)
29
+ end
30
+
31
+ def gettext_key
32
+ return "models.name.#{snake_name}"
33
+ end
34
+
35
+ def gettext_key_one
36
+ return "#{gettext_key}.one"
37
+ end
38
+
39
+ def gettext_key_other
40
+ return "#{gettext_key}.other"
41
+ end
42
+
43
+ # TODO: Maybe this should yield a ModelInspector::Relationship instead?
44
+ def relationships
45
+ @clazz.reflections.each do |key, reflection|
46
+ yield key, reflection
47
+ end
48
+ end
49
+
50
+ def relationship_gettext_key(name)
51
+ return "models.attributes.#{snake_name}.#{name}"
52
+ end
53
+
54
+ class Attribute
55
+ attr_reader :name
56
+
57
+ def initialize(clazz_inspector, name)
58
+ @clazz_inspector = clazz_inspector
59
+ @name = name
60
+ end
61
+
62
+ def gettext_key
63
+ return "models.attributes.#{@clazz_inspector.snake_name}.#{@name}"
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ module GettextSimpleRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,41 @@
1
+ # desc "Explaining what the task does"
2
+ # task :gettext_simple_rails do
3
+ # # Task goes here
4
+ # end
5
+
6
+ namespace :gettext_simple_rails do
7
+ task "generate_model_translation_files" => :environment do
8
+ GettextSimpleRails::ModelInspector.model_classes do |inspector|
9
+ translation_path = "#{GettextSimpleRails.translation_dir}/models/#{inspector.snake_name}_model_translations.rb"
10
+ FileUtils.mkdir_p(File.dirname(translation_path)) unless File.exists?(File.dirname(translation_path))
11
+
12
+ File.open(translation_path, "w") do |fp|
13
+ fp.puts "class GettextSimpleRails::UserModelTranslations"
14
+ fp.puts " def self.attribute_translations"
15
+
16
+ inspector.attributes do |attribute|
17
+ fp.puts " puts _('#{attribute.gettext_key}')"
18
+ end
19
+
20
+ fp.puts " end"
21
+
22
+ fp.puts ""
23
+ fp.puts " def self.relationship_translations"
24
+
25
+ inspector.relationships do |name, relationship|
26
+ fp.puts " puts _('#{inspector.relationship_gettext_key(name)}')"
27
+ end
28
+
29
+ fp.puts " end"
30
+
31
+ fp.puts ""
32
+ fp.puts " def self.model_name"
33
+ fp.puts " puts _('#{inspector.gettext_key}.one')"
34
+ fp.puts " puts _('#{inspector.gettext_key}.other')"
35
+ fp.puts " end"
36
+
37
+ fp.puts "end"
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,28 @@
1
+ == README
2
+
3
+ This README would normally document whatever steps are necessary to get the
4
+ application up and running.
5
+
6
+ Things you may want to cover:
7
+
8
+ * Ruby version
9
+
10
+ * System dependencies
11
+
12
+ * Configuration
13
+
14
+ * Database creation
15
+
16
+ * Database initialization
17
+
18
+ * How to run the test suite
19
+
20
+ * Services (job queues, cache servers, search engines, etc.)
21
+
22
+ * Deployment instructions
23
+
24
+ * ...
25
+
26
+
27
+ Please feel free to use a different markup language if you do not plan to run
28
+ <tt>rake doc:app</tt>.
@@ -0,0 +1,6 @@
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/sstephenson/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,15 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -0,0 +1,5 @@
1
+ class ApplicationController < ActionController::Base
2
+ # Prevent CSRF attacks by raising an exception.
3
+ # For APIs, you may want to use :null_session instead.
4
+ protect_from_forgery with: :exception
5
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,3 @@
1
+ class Role < ActiveRecord::Base
2
+ belongs_to :user
3
+ end
@@ -0,0 +1,3 @@
1
+ class User < ActiveRecord::Base
2
+ has_many :roles
3
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %>
6
+ <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>