Module: Sinatra::GhettoI18n

Defined in:
lib/sinatra/ghetto_i18n.rb

Overview

Extension that provides simplified I18N for your Sinatra applications.

When you register this extension you gain access to 3 application settings, a couple of useful methods, and new options when definining routes and rendering views.

The idea behind this is that all the routes in your application are namespaced by the language you want to use.

So for example, if you want to provide the “/events” URL in english, spanish, and portuguese, using this extension you would have:

GET /en/events
GET /es/events
GET /pt/events

Then, when you render a view (by passing a symbol to erb etc), the view is automatically looked up in a way that will include the language.

This assumes you want a separate view file for each language. This means more work maintaining the site, yes, but for very simple websites this is often easier than keeping a translations file.

This also helps when you have two widely different languages in which the design is affected by the translation (for example, an LTR and a RTL language.)

Options =

When using this extension, you have access to the following settings:

:languages ==

You must set this to an enumerable that has the language codes your application supports. Any object that responds to #include? and #each works.

For example:

set :languages, ["en", "es", "pt"]
set :languages, "en" => "English", "es" => "Español"

(The rationale for this is that you might want to use a hash for setting names in order to build links to switch languages, but you can just pass an array if you don’t need that)

:default_language ==

A string with the 2-letter language code of the default language for your application. Defaults to English.

+:i18n_view_format: ==

The path to the internationalized view files. This should be a block that takes two arguments (the original view file, and the language) and must return a symbol.

For example:

set :i18n_view_format do |view, lang|
  :"#{lang}_#{view}"
end

That means when you render the “:some_view” view, you are actually rendering the “:en_some_view” if the user is browsing the English site, or the “:fr_some_view” file if the user is browsing the French site.

The default is lang/view, so your typical sinatra application will look like this (in case the languages you support are English and Spanish (“en” and “es”):

.
|- website.rb
\- views
   |- en
   |  |- view_a.erb
   |  \- view_b.erb
   \- es
      |- view_a.erb
      \- view_b.erb

Defined Under Namespace

Modules: Helpers

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.registered(app) ⇒ Object

:nodoc:



85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/sinatra/ghetto_i18n.rb', line 85

def self.registered(app) # :nodoc:
  app.helpers Sinatra::GhettoI18n::Helpers
  app.get("/", :skip_i18n => true) { redirect "/#{language}" }

  unless app.respond_to? :i18n_view_format
    app.set(:i18n_view_format) { |view, lang| :"#{lang}/#{view}" }
  end

  unless app.respond_to? :languages
    app.set(:languages, {})
  end

  unless app.respond_to? :default_language
    app.set(:default_language, "en")
  end
end

Instance Method Details

#check_language!Object

Define a condition that makes sure the language provided in the parameters matches the languages defined as an option.



120
121
122
# File 'lib/sinatra/ghetto_i18n.rb', line 120

def check_language!
  condition { self.class.languages.include?(params[:lang]) }
end

#home(&block) ⇒ Object

Define an internationalized GET route for the root. For example:

home do
  erb :home
end

is equivalent to defining:

get "/:lang", :skip_i18n => true do
  erb :home
end


113
114
115
116
# File 'lib/sinatra/ghetto_i18n.rb', line 113

def home(&block)
  check_language!
  route("GET", "/:lang", { :skip_i18n => true }, &block)
end

#route(method, path, options = {}, &block) ⇒ Object

:nodoc:



124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/sinatra/ghetto_i18n.rb', line 124

def route(method, path, options={}, &block) # :nodoc:
  return super if options.delete(:skip_i18n)

  path.gsub! /^\//, ''

  if %W(GET HEAD).include? method
    super method, path, options do
      redirect "/#{language}/#{path}"
    end
  end

  super method, "/:lang/#{path}", options, &block
end