Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add Sinatra helper

  • Loading branch information...
commit 2d90864af61c11cb0672dd7b558d9000caa6597f 1 parent c9a2301
@ai ai authored
View
23 sinatra-r18n/README.rdoc
@@ -37,18 +37,35 @@ information.
require 'sinatra'
require 'sinatra/r18n'
-4. Use translation messages in view. For example in HAML:
+4. Set default locale (if it isn’t English):
+
+ set :default_locale, 'ru'
+
+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)
-5. Print localized time and numbers. For example:
+7. Print localized time and numbers. For example:
i18n.l @post.created_at, :date
-6. Print available translation. For example in HAML:
+8. Print available translation. For example in HAML:
%ul
- i18n.translations.each_pair do |locale, title|
View
56 sinatra-r18n/lib/sinatra/r18n.rb
@@ -1 +1,57 @@
# encoding: utf-8
+=begin
+Sinatra extension to i18n support.
+
+Copyright (C) 2009 Andrey “A.I.” Sitnik <andrey@sitnik.ru>
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+=end
+
+require 'rubygems'
+require 'sinatra/base'
+
+gem 'r18n-core', '~>0.2'
+require 'r18n-core'
+
+module Sinatra
+ module R18n
+ module Helpers
+ # Return tool for i18n support. It will be R18n::I18n object, see it
+ # documentation for more information.
+ def i18n
+ unless @i18n
+ ::R18n::I18n.default = options.default_locale
+
+ locales = ::R18n::I18n.parse_http(request.env['HTTP_ACCEPT_LANGUAGE'])
+ if params[:locale]
+ locales.insert(0, params[:locale])
+ elsif session[:locale]
+ locales.insert(0, session[:locale])
+ end
+
+ @i18n = ::R18n::I18n.new(locales, options.translations)
+ end
+ @i18n
+ end
+ end
+
+ def self.registered(app)
+ app.helpers Helpers
+ app.set :default_locale, 'en'
+ app.set :translations, Proc.new { File.join(app.root, 'i18n/') }
+ end
+ end
+
+ register R18n
+end
View
19 sinatra-r18n/spec/app/app.rb
@@ -0,0 +1,19 @@
+# encoding: utf-8
+require 'sinatra'
+
+get '/:locale/posts/:name' do
+ @post = params[:name]
+ erb :post
+end
+
+get '/:locale/posts/:name/comments' do
+ i18n.post.comments(3).to_s
+end
+
+get '/locale' do
+ i18n.locale['title']
+end
+
+get '/locales' do
+ i18n.translations.map { |i| i.join(': ') }.join("; ")
+end
View
6 sinatra-r18n/spec/app/i18n/en.yml
@@ -0,0 +1,6 @@
+post:
+ title: Post %1
+ comments: !!pl
+ 0: No comments
+ 1: One comment
+ n: %1 comments
View
2  sinatra-r18n/spec/app/i18n/ru.yml
@@ -0,0 +1,2 @@
+post:
+ title: Запись %1
View
1  sinatra-r18n/spec/app/views/post.erb
@@ -0,0 +1 @@
+<h1><%= i18n.post.title(@post) %></h1>
View
42 sinatra-r18n/spec/sinatra-r18n_spec.rb
@@ -1 +1,43 @@
# encoding: utf-8
+require File.join(File.dirname(__FILE__), 'spec_helper')
+
+describe Sinatra::R18n do
+
+ it "should translate messages" do
+ get '/ru/posts/1'
+ response.should be_ok
+ response.body.should == "<h1>Запись 1</h1>\n"
+ end
+
+ it "should use translations from default locale" do
+ get '/ru/posts/1/comments'
+ response.should be_ok
+ response.body.should == '3 comments'
+ end
+
+ it "should use default locale" do
+ set :default_locale, 'ru'
+ get '/locale'
+ response.should be_ok
+ response.body.should == 'Русский'
+ end
+
+ it "should autodetect user locale" do
+ get '/locale', :env => {'HTTP_ACCEPT_LANGUAGE' => 'ru,en;q=0.9'}
+ response.should be_ok
+ response.body.should == 'Русский'
+ end
+
+ it "should use locale from session" do
+ get '/locale', :env => { 'rack.session' => { :locale => 'ru' } }
+ response.should be_ok
+ response.body.should == 'Русский'
+ end
+
+ it "should return locales list" do
+ get '/locales'
+ response.should be_ok
+ response.body.should == 'ru: Русский; en: English'
+ end
+
+end
View
7 sinatra-r18n/spec/spec_helper.rb
@@ -1,2 +1,9 @@
# encoding: utf-8
require File.join(File.dirname(__FILE__), '../lib/sinatra/r18n')
+require File.join(File.dirname(__FILE__), 'app/app')
+
+require 'spec/interop/test'
+require 'sinatra/test'
+Test::Unit::TestCase.send :include, Sinatra::Test
+
+set :environment, :test
Please sign in to comment.
Something went wrong with that request. Please try again.