i18n_toolbox 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ script: 'rake'
3
+ rvm:
4
+ - 1.9.2
5
+ - 1.9.3
data/Gemfile CHANGED
@@ -2,6 +2,3 @@ source "http://rubygems.org"
2
2
 
3
3
  # Specify your gem's dependencies in i18n_toolbox.gemspec
4
4
  gemspec
5
-
6
- gem "rails", "~> 3.0.7"
7
- gem "sqlite3-ruby"
data/README.md ADDED
@@ -0,0 +1,104 @@
1
+ # i18n_toolbox [![Build Status](https://secure.travis-ci.org/balvig/i18n_toolbox.png?branch=master)](https://travis-ci.org/balvig/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 Recipes", "Miles' Recipes", "Recettes de Julien")
9
+
10
+ ## Usage
11
+
12
+ ### image_tag
13
+
14
+ Serve up a different image depending on locale.
15
+
16
+ ```ruby
17
+ I18n.locale = :en
18
+ image_tag('logo.png', :localize => true) # => /images/en/logo.png
19
+ I18n.locale = :ja
20
+ image_tag('logo.png', :localize => true) # => /images/ja/logo.png
21
+ ```
22
+
23
+ ### truncate
24
+
25
+ Truncate text that appears longer on screen (such as 2 byte Japanese) to avoid breaking layouts dependent on length of text.
26
+
27
+ *config/locales/ja.yml*
28
+
29
+ ```yaml
30
+ ja:
31
+ i18n_toolbox:
32
+ character_ratio: 0.5
33
+ ```
34
+
35
+ *Output:*
36
+
37
+ ```ruby
38
+ I18n.locale = :en
39
+ truncate("2 byte characters are wider", :length => 20, :localize => true) # => "2 byte characters..." (20 chars)
40
+ I18n.locale = :ja
41
+ truncate("2 byte characters are wider", :length => 20, :localize => true) # => "2 byte ..." (10 chars)
42
+ ```
43
+
44
+ ### validates_length_of
45
+
46
+ Have different validation lengths for different locales.
47
+
48
+ *config/locales/ja.yml*
49
+
50
+ ```yaml
51
+ ja:
52
+ i18n_toolbox:
53
+ character_ratio: 0.5
54
+ ```
55
+
56
+ *Model:*
57
+ ```ruby
58
+ class Post < ActiveRecord::Base
59
+ validates_length_of :title, :maximum => 40, :localize => true
60
+ # ...
61
+ end
62
+
63
+ I18n.locale = :en
64
+ Post.new(:title => 'English sentences use more chars').valid? # length is under 40, returns true
65
+ I18n.locale = :ja
66
+ Post.new(:title => 'Japanese sentences use fewer chars').valid? # => length is over 20, returns false
67
+ ```
68
+
69
+ ### possessive
70
+
71
+ Often used for user page titles, such as "Michael's Activity Feed" etc.
72
+
73
+ *config/locales/en.yml*
74
+
75
+ ```yaml
76
+ en:
77
+ i18n_toolbox:
78
+ possessive: "%{owner}'s %{thing}"
79
+ possessive_s: "%{owner}' %{thing}"
80
+ ```
81
+
82
+ *config/locales/fr.yml*
83
+
84
+ ```yaml
85
+ fr:
86
+ i18n_toolbox:
87
+ possessive: "%{thing} de %{owner}"
88
+ ```
89
+
90
+ *Output:*
91
+
92
+ ```ruby
93
+ I18n.locale = :en
94
+ possessive('Jack','Recipes') # => "Jack's Recipes"
95
+ possessive('Miles','Recipes') # => "Miles' Recipes"
96
+
97
+ I18n.locale = :fr
98
+ possessive('Julien','Recettes') # => "Recettes de Julien"
99
+ possessive('Jacques','Recettes') # => "Recettes de Jacques"
100
+ ```
101
+
102
+ ## License
103
+
104
+ MIT License. Copyright 2012 Cookpad Pte http://cookpad.it
data/i18n_toolbox.gemspec CHANGED
@@ -18,4 +18,7 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+
22
+ s.add_dependency "rails", "~> 3"
23
+ s.add_development_dependency 'sqlite3'
21
24
  end
data/lib/i18n_toolbox.rb CHANGED
@@ -3,8 +3,8 @@ require 'i18n_toolbox/active_model/validations/length'
3
3
 
4
4
  module I18nToolbox
5
5
  ActionController::Base.helper(I18nHelper)
6
-
6
+
7
7
  def self.character_ratio
8
8
  I18n.t(:'i18n_toolbox.character_ratio', :default => 1)
9
9
  end
10
- end
10
+ end
@@ -2,11 +2,11 @@ module ActiveModel
2
2
  module Validations
3
3
  class LengthValidator < EachValidator
4
4
  def validate_each(record, attribute, value)
5
- value = options[:tokenizer].call(value) if value.kind_of?(String)
5
+ value = options[:tokenizer].call(value) if value.kind_of?(String) && options[:tokenizer].present?
6
6
 
7
7
  CHECKS.each do |key, validity_check|
8
8
  next unless check_value = options[key]
9
-
9
+
10
10
  check_value = check_value * I18nToolbox.character_ratio if options[:localize]
11
11
 
12
12
  value ||= [] if key == :maximum
@@ -25,4 +25,4 @@ module ActiveModel
25
25
  end
26
26
  end
27
27
  end
28
- end
28
+ end
@@ -4,7 +4,7 @@ module I18nToolbox
4
4
  source = "#{I18n.locale}/#{source}" if options.delete(:localize)
5
5
  super(source, options)
6
6
  end
7
-
7
+
8
8
  def possessive(owner, thing)
9
9
  t(owner.last == 's' ? :'i18n_toolbox.possessive_s' : :'i18n_toolbox.possessive', :owner => owner, :thing => thing, :default => :'i18n_toolbox.possessive') if owner.present?
10
10
  end
@@ -15,4 +15,4 @@ module I18nToolbox
15
15
  super(text, options)
16
16
  end
17
17
  end
18
- end
18
+ end
@@ -1,3 +1,3 @@
1
1
  module I18nToolbox
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
@@ -1,6 +1,7 @@
1
+ # encoding: UTF-8
1
2
  require 'test_helper'
2
3
 
3
- class I18nToolbox::I18nHelperTest < ActionView::TestCase
4
+ class I18nToolbox::I18nHelperTest < ActionView::TestCase
4
5
  test "image_tag given localize => true should return a localized image" do
5
6
  I18n.locale = :ja
6
7
  assert_match 'images/ja/logo.png', image_tag('logo.png', :localize => true)
@@ -8,17 +9,17 @@ class I18nToolbox::I18nHelperTest < ActionView::TestCase
8
9
  assert_match 'images/en/logo.png', image_tag('logo.png', :localize => true)
9
10
  assert_match 'images/logo.png', image_tag('logo.png', :localize => false)
10
11
  end
11
-
12
+
12
13
  test "image_tag should otherwise work as usual" do
13
14
  assert_match 'images/logo.png', image_tag('logo.png')
14
15
  end
15
-
16
+
16
17
  test "possessive should add localized 's" do
17
18
  I18n.backend = I18n::Backend::KeyValue.new({})
18
19
  I18n.backend.store_translations :en, :i18n_toolbox => {:possessive => "%{owner}'s %{thing}", :possessive_s => "%{owner}' %{thing}"}
19
20
  I18n.backend.store_translations :ja, :i18n_toolbox => {:possessive => "%{owner}の%{thing}"}
20
21
  I18n.backend.store_translations :fr, :i18n_toolbox => {:possessive => "%{thing} de %{owner}"}
21
-
22
+
22
23
  assert_nil possessive(nil,nil)
23
24
  I18n.locale = :en
24
25
  assert_equal "Bob's house", possessive('Bob','house')
@@ -30,12 +31,12 @@ class I18nToolbox::I18nHelperTest < ActionView::TestCase
30
31
  assert_equal "La maison de Bob", possessive('Bob','La maison')
31
32
  assert_equal "La maison de Miles", possessive('Miles','La maison')
32
33
  end
33
-
34
+
34
35
  test "truncate should respect different lengths for different locales" do
35
36
  I18n.backend = I18n::Backend::KeyValue.new({})
36
37
  I18n.backend.store_translations :en, :i18n_toolbox => {:character_ratio => 1}
37
38
  I18n.backend.store_translations :ja, :i18n_toolbox => {:character_ratio => 0.5}
38
-
39
+
39
40
  I18n.locale = :en
40
41
  assert_equal 'Top Gun is one of the best ...', truncate('Top Gun is one of the best movies ever made', :localize => true)
41
42
  I18n.locale = :ja
@@ -44,4 +45,4 @@ class I18nToolbox::I18nHelperTest < ActionView::TestCase
44
45
  I18n.locale = :da
45
46
  assert_equal 'Top Gun is one of the best ...', truncate('Top Gun is one of the best movies ever made', :localize => true)
46
47
  end
47
- end
48
+ end
metadata CHANGED
@@ -1,37 +1,60 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: i18n_toolbox
3
- version: !ruby/object:Gem::Version
4
- hash: 29
3
+ version: !ruby/object:Gem::Version
5
4
  prerelease:
6
- segments:
7
- - 0
8
- - 0
9
- - 1
10
- version: 0.0.1
5
+ version: 0.0.2
11
6
  platform: ruby
12
- authors:
7
+ authors:
13
8
  - Jens Balvig
14
9
  autorequire:
15
10
  bindir: bin
16
11
  cert_chain: []
17
-
18
- date: 2011-07-05 00:00:00 +09:00
19
- default_executable:
20
- dependencies: []
21
-
22
- description: Provides a set of convenient helpers and active model additions for working with multiple languages
23
- email:
12
+ date: 2012-12-06 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ type: :runtime
16
+ version_requirements: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ name: rails
23
+ prerelease: false
24
+ requirement: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '3'
30
+ - !ruby/object:Gem::Dependency
31
+ type: :development
32
+ version_requirements: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ name: sqlite3
39
+ prerelease: false
40
+ requirement: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Provides a set of convenient helpers and active model additions for working
47
+ with multiple languages
48
+ email:
24
49
  - jens@balvig.com
25
50
  executables: []
26
-
27
51
  extensions: []
28
-
29
52
  extra_rdoc_files: []
30
-
31
- files:
53
+ files:
32
54
  - .gitignore
55
+ - .travis.yml
33
56
  - Gemfile
34
- - README.rdoc
57
+ - README.md
35
58
  - Rakefile
36
59
  - i18n_toolbox.gemspec
37
60
  - lib/i18n_toolbox.rb
@@ -61,10 +84,6 @@ files:
61
84
  - test/rails_app/config/locales/en.yml
62
85
  - test/rails_app/config/routes.rb
63
86
  - test/rails_app/db/migrate/20110705081839_create_posts.rb
64
- - test/rails_app/log/development.log
65
- - test/rails_app/log/production.log
66
- - test/rails_app/log/server.log
67
- - test/rails_app/log/test.log
68
87
  - test/rails_app/public/404.html
69
88
  - test/rails_app/public/422.html
70
89
  - test/rails_app/public/500.html
@@ -78,41 +97,38 @@ files:
78
97
  - test/rails_app/public/stylesheets/.gitkeep
79
98
  - test/rails_app/script/rails
80
99
  - test/test_helper.rb
81
- has_rdoc: true
82
100
  homepage: https://github.com/balvig/i18n_toolbox
83
101
  licenses: []
84
-
85
102
  post_install_message:
86
103
  rdoc_options: []
87
-
88
- require_paths:
104
+ require_paths:
89
105
  - lib
90
- required_ruby_version: !ruby/object:Gem::Requirement
106
+ required_ruby_version: !ruby/object:Gem::Requirement
91
107
  none: false
92
- requirements:
93
- - - ">="
94
- - !ruby/object:Gem::Version
95
- hash: 3
96
- segments:
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ segments:
97
112
  - 0
98
- version: "0"
99
- required_rubygems_version: !ruby/object:Gem::Requirement
113
+ hash: -1023373613002778549
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
116
  none: false
101
- requirements:
102
- - - ">="
103
- - !ruby/object:Gem::Version
104
- hash: 3
105
- segments:
117
+ requirements:
118
+ - - ! '>='
119
+ - !ruby/object:Gem::Version
120
+ segments:
106
121
  - 0
107
- version: "0"
122
+ hash: -1023373613002778549
123
+ version: '0'
108
124
  requirements: []
109
-
110
125
  rubyforge_project: i18n_toolbox
111
- rubygems_version: 1.4.2
126
+ rubygems_version: 1.8.23
112
127
  signing_key:
113
128
  specification_version: 3
114
- summary: Provides a set of convenient helpers and active model additions for working with multiple languages
115
- test_files:
129
+ summary: Provides a set of convenient helpers and active model additions for working
130
+ with multiple languages
131
+ test_files:
116
132
  - test/active_model/validations/length_test.rb
117
133
  - test/helpers/i18n_helper_test.rb
118
134
  - test/rails_app/Rakefile
@@ -136,10 +152,6 @@ test_files:
136
152
  - test/rails_app/config/locales/en.yml
137
153
  - test/rails_app/config/routes.rb
138
154
  - test/rails_app/db/migrate/20110705081839_create_posts.rb
139
- - test/rails_app/log/development.log
140
- - test/rails_app/log/production.log
141
- - test/rails_app/log/server.log
142
- - test/rails_app/log/test.log
143
155
  - test/rails_app/public/404.html
144
156
  - test/rails_app/public/422.html
145
157
  - test/rails_app/public/500.html
data/README.rdoc DELETED
@@ -1,41 +0,0 @@
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
@@ -1 +0,0 @@
1
- DEPRECATION WARNING: config.action_view.debug_rjs will be removed in 3.1, from 3.1 onwards you will need to install prototype-rails to continue to use RJS templates . (called from /Users/jens/Documents/Projects/i18n_toolbox/test/rails_app/config/environment.rb:5)
File without changes
File without changes
File without changes