Module: Sinatra::I18nSupport

Defined in:
lib/sinatra/support/i18nsupport.rb

Overview

I18n support.

require 'sinatra/support/i18nsupport'

class Main < Sinatra::Base
  register Sinatra::I18nSupport
  load_locales './config/locales'
  set :default_locale, 'fr'  # Optional; defaults to 'en'
end

Be sure that you have the I18n gem. Use gem install i18n, or if you’re using Bundler:

# Gemfile
gem "i18n"

Then put your locale YAML files into ./config/locales (whatever path you use for #load_locales:

# config/locales/en.yml
en:
  an_article: "An article"
  create: "Create"
  delete: "Delete"

Helpers

t - Translates something.

<h3><%= t('article.an_article') %></h3>
<h5><%= t('article.delete', name: @article.to_s) %></h5>

l - Localizes something.

<%= l(Time.now) %>
<%= l(Time.now, format: :short) %>

current_locale - Returns the current locale name.

<script>
  window.locale = <%= current_locale.inspect %>;
</script>

available_locales - A list of available locales.

<% if available_locales.include?(:es) %>
  <a href="/locales/es">en Espanol</a>
<% end %>

Changing locales

Set session[:locale] to the locale name.

get '/locales/:locale' do |locale|
  not_found  unless locales.include?(locale)
  session[:locale] = locale
end

If you want to override the way of checking for the current locale, simply redefine the ‘current_locale` helper:

helpers do
  def current_locale
    current_user.locale || session[:locale] || settings.default_locale
  end
end

Locale files

This gem does not ship with default options for time, date and such. You may want to get those from the Rails-I18n project: github.com/svenfuchs/rails-i18n/tree/master/rails/locale

Using a different backend

Instead of calling #load_locales, just load the right I18n backend using the I18n gem.

You can also just use I18n.store_translations if you still want to use the default simple I18n backend.

Settings

default_locale

The locale to use by default. Defaults to en.

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object



87
88
89
90
91
# File 'lib/sinatra/support/i18nsupport.rb', line 87

def self.registered(app)
  require 'i18n'
  app.set :default_locale, 'en'
  app.helpers Helpers
end

Instance Method Details

#load_locales(path) ⇒ Object

Loads the locales in the given path.



94
95
96
97
98
# File 'lib/sinatra/support/i18nsupport.rb', line 94

def load_locales(path)
  Dir[File.join(path, '*.yml')].each do |file|
    I18n.backend.load_translations file
  end
end