0.01
No release in over 3 years
A Sinatra extension that provides i18n support to translate your web application. It is just a wrapper for R18n core library. It has nice Ruby-style syntax, filters, flexible locales, custom loaders, translation support for any classes, time and number localization, several user language support, agnostic core package with out-of-box support for Rails, Sinatra and desktop applications.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Development

~> 0.5.0
~> 0.7.1
~> 1.1
~> 3.10
~> 1.6
~> 0.5.1
~> 0.21.0
~> 0.11.0

Runtime

~> 5.0
>= 1.3, < 3
 Project Readme

Sinatra R18n Plugin

Cirrus CI - Base Branch Build Status Codecov branch Code Climate Depfu License Gem

Sinatra extension which provides i18n support to translate your web application.

It is a wrapper for R18n core library. See R18n documentation for more information.

Features

  • Nice Ruby-style syntax.
  • Filters.
  • Flexible locales.
  • Custom translations loaders.
  • Translation support for any classes.
  • Time and number localization.
  • Several user language support.

How To

  1. Create translations dir ./i18n/.

  2. Add file with translation to ./i18n/ with language code in file name (for example, en.yml for English or en-us.yml USA English dialect). For example, ./i18n/en.yml:

    post:
      friends: Post only for friends
      tags: Post tags are %1
    
    comments: !!pl
      0: No comments
      1: One comment
      n: '%1 comments'
    
    html: !!html
      <b>Don't escape HTML</b>
  3. Add R18n to your Sinatra application:

    require 'sinatra/r18n'

    If your application inherits from Sinatra::Base also add:

    class YourApp < Sinatra::Base
      register Sinatra::R18n
      set :root, __dir__
  4. Add locale to your URLs. For example:

    get '/:locale/posts/:id' do
      @post = Post.find(params[:id])
      haml :post
    end

    Or save locale in session, when user change it:

    before do
      session[:locale] = params[:locale] if params[:locale]
    end

    Warning: such hooks have to be before register Sinatra::R18n since R18n version 5 has no more lazy evaluation and initializes immediately. See #3 or specs / test application.

  5. Use translation messages in views. For example in HAML:

    %p= t.post.friends
    %p= t.post.tags(@post.tags.join(', '))
    
    %h2= t.comments(@post.comments.size)
  6. Print localized time and numbers. For example:

    l @post.created_at, :human
  7. Print available translations. For example in HAML:

    %ul
      - r18n.available_locales.each do |locale|
        %li
          %a( href="/#{locale.code}/" )= locale.title

Configuration

You can change default locale and translations dir:

R18n::I18n.default = 'ru'
R18n.default_places { './translations' }

License

R18n is licensed under the GNU Lesser General Public License version 3. You can read it in LICENSE file or in www.gnu.org/licenses/lgpl-3.0.html.

Author

Andrey “A.I.” Sitnik andrey@sitnik.ru