i18n_toolbox 0.0.1

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.
Files changed (50) hide show
  1. data/.gitignore +7 -0
  2. data/Gemfile +7 -0
  3. data/README.rdoc +41 -0
  4. data/Rakefile +12 -0
  5. data/i18n_toolbox.gemspec +21 -0
  6. data/lib/i18n_toolbox/active_model/validations/length.rb +28 -0
  7. data/lib/i18n_toolbox/helpers/i18n_helper.rb +18 -0
  8. data/lib/i18n_toolbox/version.rb +3 -0
  9. data/lib/i18n_toolbox.rb +10 -0
  10. data/test/active_model/validations/length_test.rb +33 -0
  11. data/test/helpers/i18n_helper_test.rb +47 -0
  12. data/test/rails_app/Rakefile +7 -0
  13. data/test/rails_app/app/controllers/application_controller.rb +3 -0
  14. data/test/rails_app/app/helpers/application_helper.rb +2 -0
  15. data/test/rails_app/app/models/post.rb +2 -0
  16. data/test/rails_app/app/views/layouts/application.html.erb +14 -0
  17. data/test/rails_app/config/application.rb +44 -0
  18. data/test/rails_app/config/boot.rb +10 -0
  19. data/test/rails_app/config/database.yml +18 -0
  20. data/test/rails_app/config/environment.rb +5 -0
  21. data/test/rails_app/config/environments/development.rb +26 -0
  22. data/test/rails_app/config/environments/production.rb +49 -0
  23. data/test/rails_app/config/environments/test.rb +35 -0
  24. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  25. data/test/rails_app/config/initializers/inflections.rb +10 -0
  26. data/test/rails_app/config/initializers/mime_types.rb +5 -0
  27. data/test/rails_app/config/initializers/secret_token.rb +7 -0
  28. data/test/rails_app/config/initializers/session_store.rb +8 -0
  29. data/test/rails_app/config/locales/en.yml +5 -0
  30. data/test/rails_app/config/routes.rb +58 -0
  31. data/test/rails_app/config.ru +4 -0
  32. data/test/rails_app/db/migrate/20110705081839_create_posts.rb +14 -0
  33. data/test/rails_app/log/development.log +1 -0
  34. data/test/rails_app/log/production.log +0 -0
  35. data/test/rails_app/log/server.log +0 -0
  36. data/test/rails_app/log/test.log +0 -0
  37. data/test/rails_app/public/404.html +26 -0
  38. data/test/rails_app/public/422.html +26 -0
  39. data/test/rails_app/public/500.html +26 -0
  40. data/test/rails_app/public/favicon.ico +0 -0
  41. data/test/rails_app/public/javascripts/application.js +2 -0
  42. data/test/rails_app/public/javascripts/controls.js +965 -0
  43. data/test/rails_app/public/javascripts/dragdrop.js +974 -0
  44. data/test/rails_app/public/javascripts/effects.js +1123 -0
  45. data/test/rails_app/public/javascripts/prototype.js +6001 -0
  46. data/test/rails_app/public/javascripts/rails.js +191 -0
  47. data/test/rails_app/public/stylesheets/.gitkeep +0 -0
  48. data/test/rails_app/script/rails +6 -0
  49. data/test/test_helper.rb +23 -0
  50. metadata +155 -0
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ .DS_Store
6
+ test/debug.log
7
+ test/i18n_toolbox_plugin.sqlite3.db
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in i18n_toolbox.gemspec
4
+ gemspec
5
+
6
+ gem "rails", "~> 3.0.7"
7
+ gem "sqlite3-ruby"
data/README.rdoc ADDED
@@ -0,0 +1,41 @@
1
+ == i18n_toolbox
2
+
3
+ I18n_toolbox is a collection of helpers and active_model additions that solve basic problems that kept showing up when working with multiple language sites:
4
+
5
+ * Showing different images for different locales
6
+ * Different validates_length_of values for different locales
7
+ * Truncating text to different lengths depending on locale
8
+ * Applying "possessive case" correctly with correct use of apostrophes for different languages (ie "Jack's Homepage, Miles' Homepage, Miles")
9
+
10
+ == Usage
11
+
12
+ === image_tag
13
+
14
+ TBA
15
+
16
+ === truncate
17
+
18
+ TBA
19
+
20
+ === validates_length_of
21
+
22
+ TBA
23
+
24
+ === possessive
25
+
26
+ TBA
27
+
28
+ ja:
29
+ i18n_toolbox:
30
+ character_ratio: 0.5
31
+
32
+
33
+ == Additional information
34
+
35
+ === Maintainers
36
+
37
+ * Jens Balvig (http://github.com/balvig)
38
+
39
+ == License
40
+
41
+ MIT License. Copyright 2011 Cookpad Pte http://cookpad.it
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ task :default => :test
6
+
7
+ desc 'Run unit tests.'
8
+ Rake::TestTask.new(:test) do |t|
9
+ t.libs << 'test'
10
+ t.test_files = FileList['test/**/*_test.rb']
11
+ t.verbose = true
12
+ end
@@ -0,0 +1,21 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "i18n_toolbox/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "i18n_toolbox"
7
+ s.version = I18nToolbox::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Jens Balvig"]
10
+ s.email = ["jens@balvig.com"]
11
+ s.homepage = "https://github.com/balvig/i18n_toolbox"
12
+ s.summary = %q{Provides a set of convenient helpers and active model additions for working with multiple languages}
13
+ s.description = %q{Provides a set of convenient helpers and active model additions for working with multiple languages}
14
+
15
+ s.rubyforge_project = "i18n_toolbox"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+ end
@@ -0,0 +1,28 @@
1
+ module ActiveModel
2
+ module Validations
3
+ class LengthValidator < EachValidator
4
+ def validate_each(record, attribute, value)
5
+ value = options[:tokenizer].call(value) if value.kind_of?(String)
6
+
7
+ CHECKS.each do |key, validity_check|
8
+ next unless check_value = options[key]
9
+
10
+ check_value = check_value * I18nToolbox.character_ratio if options[:localize]
11
+
12
+ value ||= [] if key == :maximum
13
+
14
+ value_length = value.respond_to?(:length) ? value.length : value.to_s.length
15
+ next if value_length.send(validity_check, check_value)
16
+
17
+ errors_options = options.except(*RESERVED_OPTIONS)
18
+ errors_options[:count] = check_value
19
+
20
+ default_message = options[MESSAGES[key]]
21
+ errors_options[:message] ||= default_message if default_message
22
+
23
+ record.errors.add(attribute, MESSAGES[key], errors_options)
24
+ end
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,18 @@
1
+ module I18nToolbox
2
+ module I18nHelper
3
+ def image_tag(source, options = {})
4
+ source = "#{I18n.locale}/#{source}" if options.delete(:localize)
5
+ super(source, options)
6
+ end
7
+
8
+ def possessive(owner, thing)
9
+ t(owner.last == 's' ? :'i18n_toolbox.possessive_s' : :'i18n_toolbox.possessive', :owner => owner, :thing => thing, :default => :'i18n_toolbox.possessive') if owner.present?
10
+ end
11
+
12
+ def truncate(text, options = {})
13
+ options.reverse_merge!(:length => 30)
14
+ options[:length] = options[:length] * I18nToolbox.character_ratio if options.delete(:localize)
15
+ super(text, options)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module I18nToolbox
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,10 @@
1
+ require 'i18n_toolbox/helpers/i18n_helper'
2
+ require 'i18n_toolbox/active_model/validations/length'
3
+
4
+ module I18nToolbox
5
+ ActionController::Base.helper(I18nHelper)
6
+
7
+ def self.character_ratio
8
+ I18n.t(:'i18n_toolbox.character_ratio', :default => 1)
9
+ end
10
+ end
@@ -0,0 +1,33 @@
1
+ require 'test_helper'
2
+
3
+ class I18nToolbox::LengthValidatorTest < ActiveSupport::TestCase
4
+ I18n.backend = I18n::Backend::KeyValue.new({})
5
+ I18n.backend.store_translations :en, :i18n_toolbox => {:character_ratio => 1}
6
+ I18n.backend.store_translations :ja, :i18n_toolbox => {:character_ratio => 0.5}
7
+
8
+ class Post < ActiveRecord::Base
9
+ validates_length_of :title, :maximum => 20, :localize => true
10
+ validates_length_of :body, :maximum => 20
11
+ end
12
+
13
+ test "validates_length_of maximum should change depending on locale if localize => true" do
14
+
15
+ I18n.locale = :en
16
+ assert !Post.new(:title => 'This is a long sentence').valid?
17
+ assert Post.new(:title => 'Short sentence').valid?
18
+
19
+ I18n.locale = :ja
20
+ assert !Post.new(:title => 'Short sentence').valid?
21
+ assert Post.new(:title => 'Short!').valid?
22
+ end
23
+
24
+ test "validates_length_of maximum should work normally if localize => false" do
25
+ I18n.locale = :en
26
+ assert Post.new(:body => 'Short sentence').valid?
27
+
28
+ I18n.locale = :ja
29
+ assert Post.new(:body => 'Short sentence').valid?
30
+ end
31
+
32
+ #should work with range too
33
+ end
@@ -0,0 +1,47 @@
1
+ require 'test_helper'
2
+
3
+ class I18nToolbox::I18nHelperTest < ActionView::TestCase
4
+ test "image_tag given localize => true should return a localized image" do
5
+ I18n.locale = :ja
6
+ assert_match 'images/ja/logo.png', image_tag('logo.png', :localize => true)
7
+ I18n.locale = :en
8
+ assert_match 'images/en/logo.png', image_tag('logo.png', :localize => true)
9
+ assert_match 'images/logo.png', image_tag('logo.png', :localize => false)
10
+ end
11
+
12
+ test "image_tag should otherwise work as usual" do
13
+ assert_match 'images/logo.png', image_tag('logo.png')
14
+ end
15
+
16
+ test "possessive should add localized 's" do
17
+ I18n.backend = I18n::Backend::KeyValue.new({})
18
+ I18n.backend.store_translations :en, :i18n_toolbox => {:possessive => "%{owner}'s %{thing}", :possessive_s => "%{owner}' %{thing}"}
19
+ I18n.backend.store_translations :ja, :i18n_toolbox => {:possessive => "%{owner}の%{thing}"}
20
+ I18n.backend.store_translations :fr, :i18n_toolbox => {:possessive => "%{thing} de %{owner}"}
21
+
22
+ assert_nil possessive(nil,nil)
23
+ I18n.locale = :en
24
+ assert_equal "Bob's house", possessive('Bob','house')
25
+ assert_equal "Miles' house", possessive('Miles','house')
26
+ I18n.locale = :ja
27
+ assert_equal "Bobの家", possessive('Bob','家')
28
+ assert_equal "Milesの家", possessive('Miles','家')
29
+ I18n.locale = :fr
30
+ assert_equal "La maison de Bob", possessive('Bob','La maison')
31
+ assert_equal "La maison de Miles", possessive('Miles','La maison')
32
+ end
33
+
34
+ test "truncate should respect different lengths for different locales" do
35
+ I18n.backend = I18n::Backend::KeyValue.new({})
36
+ I18n.backend.store_translations :en, :i18n_toolbox => {:character_ratio => 1}
37
+ I18n.backend.store_translations :ja, :i18n_toolbox => {:character_ratio => 0.5}
38
+
39
+ I18n.locale = :en
40
+ assert_equal 'Top Gun is one of the best ...', truncate('Top Gun is one of the best movies ever made', :localize => true)
41
+ I18n.locale = :ja
42
+ assert_equal "トップガンは今まで見た映画の中でも特に印象に残るものの...", truncate('トップガンは今まで見た映画の中でも特に印象に残るものの一つです')
43
+ assert_equal "トップガンは今まで見た映...", truncate('トップガンは今まで見た映画の中でも特に印象に残るものの一つです', :localize => true)
44
+ I18n.locale = :da
45
+ assert_equal 'Top Gun is one of the best ...', truncate('Top Gun is one of the best movies ever made', :localize => true)
46
+ end
47
+ end
@@ -0,0 +1,7 @@
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
+ require 'rake'
6
+
7
+ Dummy::Application.load_tasks
@@ -0,0 +1,3 @@
1
+ class ApplicationController < ActionController::Base
2
+ protect_from_forgery
3
+ end
@@ -0,0 +1,2 @@
1
+ module ApplicationHelper
2
+ end
@@ -0,0 +1,2 @@
1
+ class Post < ActiveRecord::Base
2
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>Dummy</title>
5
+ <%= stylesheet_link_tag :all %>
6
+ <%= javascript_include_tag :defaults %>
7
+ <%= csrf_meta_tag %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,44 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ require "active_model/railtie"
4
+ require "active_record/railtie"
5
+ require "action_controller/railtie"
6
+ require "action_view/railtie"
7
+ require "action_mailer/railtie"
8
+
9
+ Bundler.require
10
+
11
+ module Dummy
12
+ class Application < Rails::Application
13
+ # Settings in config/environments/* take precedence over those specified here.
14
+ # Application configuration should go into files in config/initializers
15
+ # -- all .rb files in that directory are automatically loaded.
16
+
17
+ # Custom directories with classes and modules you want to be autoloadable.
18
+ # config.autoload_paths += %W(#{config.root}/extras)
19
+
20
+ # Only load the plugins named here, in the order given (default is alphabetical).
21
+ # :all can be used as a placeholder for all plugins not explicitly named.
22
+ # config.plugins = [ :exception_notification, :ssl_requirement, :all ]
23
+
24
+ # Activate observers that should always be running.
25
+ # config.active_record.observers = :cacher, :garbage_collector, :forum_observer
26
+
27
+ # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone.
28
+ # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC.
29
+ # config.time_zone = 'Central Time (US & Canada)'
30
+
31
+ # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded.
32
+ # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s]
33
+ # config.i18n.default_locale = :de
34
+
35
+ # JavaScript files you want as :defaults (application.js is always included).
36
+ # config.action_view.javascript_expansions[:defaults] = %w(jquery rails)
37
+
38
+ # Configure the default encoding used in templates for Ruby 1.9.
39
+ config.encoding = "utf-8"
40
+
41
+ # Configure sensitive parameters which will be filtered from the log file.
42
+ config.filter_parameters += [:password]
43
+ end
44
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,18 @@
1
+ # SQLite version 3.x
2
+ # gem install sqlite3
3
+ development:
4
+ adapter: sqlite3
5
+ database: db/development.sqlite3
6
+ pool: 5
7
+ timeout: 5000
8
+
9
+ # Warning: The database defined as "test" will be erased and
10
+ # re-generated from your development database when you run "rake".
11
+ # Do not set this db to the same as development or production.
12
+ test:
13
+ adapter: sqlite3
14
+ database: ":memory:"
15
+
16
+ production:
17
+ adapter: sqlite3
18
+ database: ":memory:"
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,26 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # In the development environment your application's code is reloaded on
5
+ # every request. This slows down response time but is perfect for development
6
+ # since you don't have to restart the webserver when you make code changes.
7
+ config.cache_classes = false
8
+
9
+ # Log error messages when you accidentally call methods on nil.
10
+ config.whiny_nils = true
11
+
12
+ # Show full error reports and disable caching
13
+ config.consider_all_requests_local = true
14
+ config.action_view.debug_rjs = true
15
+ config.action_controller.perform_caching = false
16
+
17
+ # Don't care if the mailer can't send
18
+ config.action_mailer.raise_delivery_errors = false
19
+
20
+ # Print deprecation notices to the Rails logger
21
+ config.active_support.deprecation = :log
22
+
23
+ # Only use best-standards-support built into browsers
24
+ config.action_dispatch.best_standards_support = :builtin
25
+ end
26
+
@@ -0,0 +1,49 @@
1
+ Dummy::Application.configure do
2
+ # Settings specified here will take precedence over those in config/application.rb
3
+
4
+ # The production environment is meant for finished, "live" apps.
5
+ # Code is not reloaded between requests
6
+ config.cache_classes = true
7
+
8
+ # Full error reports are disabled and caching is turned on
9
+ config.consider_all_requests_local = false
10
+ config.action_controller.perform_caching = true
11
+
12
+ # Specifies the header that your server uses for sending files
13
+ config.action_dispatch.x_sendfile_header = "X-Sendfile"
14
+
15
+ # For nginx:
16
+ # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect'
17
+
18
+ # If you have no front-end server that supports something like X-Sendfile,
19
+ # just comment this out and Rails will serve the files
20
+
21
+ # See everything in the log (default is :info)
22
+ # config.log_level = :debug
23
+
24
+ # Use a different logger for distributed setups
25
+ # config.logger = SyslogLogger.new
26
+
27
+ # Use a different cache store in production
28
+ # config.cache_store = :mem_cache_store
29
+
30
+ # Disable Rails's static asset server
31
+ # In production, Apache or nginx will already do this
32
+ config.serve_static_assets = false
33
+
34
+ # Enable serving of images, stylesheets, and javascripts from an asset server
35
+ # config.action_controller.asset_host = "http://assets.example.com"
36
+
37
+ # Disable delivery errors, bad email addresses will be ignored
38
+ # config.action_mailer.raise_delivery_errors = false
39
+
40
+ # Enable threaded mode
41
+ # config.threadsafe!
42
+
43
+ # Enable locale fallbacks for I18n (makes lookups for any locale fall back to
44
+ # the I18n.default_locale when a translation can not be found)
45
+ config.i18n.fallbacks = true
46
+
47
+ # Send deprecation notices to registered listeners
48
+ config.active_support.deprecation = :notify
49
+ end