Skip to content

Provides a set of convenient helpers and active model additions for working with multiple languages

Notifications You must be signed in to change notification settings

symbols-worldwide/i18n_toolbox

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

i18n_toolbox Build Status

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:

  • Showing different images for different locales
  • Different validates_length_of values for different locales
  • Truncating text to different lengths depending on locale
  • Applying "possessive case" correctly with correct use of apostrophes for different languages (ie "Jack's Recipes", "Miles' Recipes", "Recettes de Julien")

Usage

image_tag

Serve up a different image depending on locale.

I18n.locale = :en
image_tag('logo.png', localize: true) # => /images/en/logo.png
I18n.locale = :ja
image_tag('logo.png', localize: true) # => /images/ja/logo.png

truncate

Truncate text that appears longer on screen (such as 2 byte Japanese) to avoid breaking layouts dependent on length of text.

config/locales/ja.yml

ja:
  i18n_toolbox:
    character_ratio: 0.5

Output:

I18n.locale = :en
truncate("2 byte characters are wider", length: 20, localize: true) # => "2 byte characters..." (20 chars)
I18n.locale = :ja
truncate("2 byte characters are wider", length: 20, localize: true) # => "2 byte ..." (10 chars)

validates_length_of

Have different validation lengths for different locales.

config/locales/ja.yml

ja:
  i18n_toolbox:
    character_ratio: 0.5

Model:

class Post < ActiveRecord::Base
  validates_length_of :title, maximum: 40, localize: true
  # ...
end

I18n.locale = :en
Post.new(title: 'English sentences use more chars').valid? # => true, length is under 40
I18n.locale = :ja
Post.new(title: 'Japanese sentences use fewer chars').valid? # => false, length is over 20

possessive

Often used for user page titles, such as "Michael's Activity Feed" etc.

config/locales/en.yml

en:
  i18n_toolbox:
    possessive: "%{owner}'s %{thing}"
    possessive_s: "%{owner}' %{thing}"

config/locales/fr.yml

fr:
  i18n_toolbox:
    possessive: "%{thing} de %{owner}"

Output:

I18n.locale = :en
possessive('Jack', 'Recipes') # => "Jack's Recipes"
possessive('Miles', 'Recipes') # => "Miles' Recipes"

I18n.locale = :fr
possessive('Julien', 'Recettes') # => "Recettes de Julien"
possessive('Jacques', 'Recettes') # => "Recettes de Jacques"

License

MIT License. Copyright 2012 Cookpad Pte http://cookpad.it

About

Provides a set of convenient helpers and active model additions for working with multiple languages

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Ruby 68.0%
  • JavaScript 23.4%
  • HTML 8.6%