Tool to internationalize and localize your Merb/Sinatra/desktop Ruby application.
How To
Merb
-
Install merb_r18n gem:
sudo gem install merb_r18n
-
Add merb_r18n to your merb application in config/dependencies.rb:
dependency 'merb_r18n'
-
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
default_route
end
end
-
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
-
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>
-
Print localized time and numbers. For example:
<%= i18n.l @post.created_at, :date %>
-
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
-
Install sinatra-r18n gem:
sudo gem install sinatra-r18n
-
Create translations dir ./i18n/.
-
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
-
Add R18n to your Sinatra application:
require 'sinatra/r18n'
-
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
-
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)
-
Print localized time and numbers. For example:
i18n.l @post.created_at, :date
-
Print available translations. For example in HAML:
%ul
- i18n.translations.each_pair do |locale, title|
%li
%a{ href: "/#{locale}/" }= title
Desktop
-
Install r18n-desktop gem:
sudo gem install r18n-desktop
-
Create translations dir. For example: ./i18n/.
-
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
-
Add R18n to your application:
require 'r18n-desktop'
-
Load I18n object:
i18n = R18n.from_env 'translations/'
Or, if user can optional set locale manually:
i18n = R18n.from_env 'translations/', manual_locale
-
Use translation messages to user. For example:
i18n.file.add
i18n.file.delete('Test')
i18n.files(1)
i18n.files(12)
i18n.author('user')
i18n.l -12000.5
i18n.l Time.now
i18n.l Time.now, :date
i18n.l Time.now, '%B'
i18n.ok
i18n.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