R18n

Tool to internationalize and localize your Merb/Sinatra/desktop Ruby application.

R18n was developed by Andrey “A.I.” Sitnik and was licensed under GNU LGPL 3.

Features

How To

Merb

  1. Install merb_r18n gem:
    sudo gem install merb_r18n
  2. Add merb_r18n to your merb application in config/dependencies.rb:
    dependency 'merb_r18n'
  3. Add route to set locale manually in your config/router.rb. For example set available translations:
    Merb::Router.prepare do
      match('(/:locale)', :locale => /(en|ru)/) do
        # Your application routers.
        default_route
      end
    end
  4. Create translations file in app/i18n/. For example app/i18n/en.yml:
    post:
      add: Add post
      edit: Edit %1
    
    comments: !!pl
      0: No comments
      1: One comment
      n: %1 comments
  5. Use translation messages in view. For example:
    <%= link_to i18n.post.add, 'posts/add' %>
    <%= link_to i18n.post.edit(post.title), "posts/edit/#{@post.id}" %>
    <%= link_to i18n.delete, "posts/delete/#{@post.id}" %>
    
    <h1><%= i18n.comments(@post.comments.size) %></h1>
  6. Print localized time and numbers. For example:
    <%= i18n.l @post.created_at, :date %>
  7. Print available translations:
    <ul>
      <% i18n.translations.each_pair do |locale, title| %>
      <li><a href="/<%= locale %>/"><%= title %></a></li>
      <% end %>
    </ul>
    See about Merb Slises in merb_r18n gem documentation.

Sinatra

  1. Install sinatra-r18n gem:
    sudo gem install sinatra-r18n
  2. Create translations dir ./i18n/.
  3. Add file with translation. For example ./i18n/en.yml:
    post:
      friends: Post only for friends
      tags: Post tags: %1
    
    comments: !!pl
      0: No comments
      1: One comment
      n: %1 comments
  4. Add R18n to your Sinatra application:
    require 'sinatra/r18n'
  5. Add locale to your URLs. For example:
    get '/:locale/posts/:id' do
      @post = Post.find(params[:id])
      haml :post
    end
    Or you save locale in session, when user change it:
    before do
      session[:locale] = params[:locale] if params[:locale]
    end
  6. Use translation messages in view. For example in HAML:
    %p= i18n.post.friends
    %p= i18n.post.tags(@post.tags.join(', '))
    
    %h2= i18n.comments(@post.comments.size)
  7. Print localized time and numbers. For example:
    i18n.l @post.created_at, :date
  8. Print available translations. For example in HAML:
    %ul
    - i18n.translations.each_pair do |locale, title|
      %li
        %a{ href: "/#{locale}/" }= title

Desktop

  1. Install r18n-desktop gem:
    sudo gem install r18n-desktop
  2. Create translations dir. For example: ./i18n/.
  3. Add file with translation in some language. For example ./i18n/en.yml:
    file:
      add: Add file
      delete: Delete file %1
    
    files: !!pl
      0: No files
      1: One file
      n: %1 files
    
    author: !!proc |name| "This file was created by #{name.capitalize}"
  4. Add R18n to your application:
    require 'r18n-desktop'
  5. Load I18n object:
    i18n = R18n.from_env 'translations/'
    Or, if user can optional set locale manually:
    i18n = R18n.from_env 'translations/', manual_locale
  6. Use translation messages to user. For example:
    i18n.file.add             #=> "Add file"
    i18n.file.delete('Test')  #=> "Delete file Test"
    i18n.files(1)             #=> "One file"
    i18n.files(12)            #=> "12 files"
    i18n.author('user')       #=> "This file was created by User"
    
    i18n.l -12000.5           #=> "−12,000.5"
    
    i18n.l Time.now           #=> "Sun Sep 21 19:50:04 GMT 2008"
    i18n.l Time.now, :date    #=> "09/21/2008"
    i18n.l Time.now, '%B'     #=> "September"
    
    # Base translation
    i18n.ok                   #=> "OK"
    i18n.cancel               #=> "Cancel"

Sources

It is a free software and you can get and edit sources:

View online on GitHub or clone Git repository:

git clone git://github.com/ai/r18n.git