Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

How toI18n with Sinatra #40

Merged
merged 1 commit into from

2 participants

@sdalu

No description provided.

@zzak zzak commented on the diff
development/i18n.md
@@ -0,0 +1,78 @@
+How can I internationalise my application? {#i18n}
@zzak Owner
zzak added a note

internationalize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zzak zzak commented on the diff
development/i18n.md
@@ -0,0 +1,78 @@
+How can I internationalise my application? {#i18n}
+------------------------------------------
+
+We will rely on the `i18n` gem to handle internationalisation
+of strings and objects, and to manage fallbacks on available locales
+
+ require 'i18n'
+ require 'i18n/backend/fallbacks'
+
+A little configuration need to be done on I18n so that:
@zzak Owner
zzak added a note

I like "The following configuration is necessary on I18n so that:"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zzak zzak commented on the diff
development/i18n.md
@@ -0,0 +1,78 @@
+How can I internationalise my application? {#i18n}
+------------------------------------------
+
+We will rely on the `i18n` gem to handle internationalisation
+of strings and objects, and to manage fallbacks on available locales
+
+ require 'i18n'
+ require 'i18n/backend/fallbacks'
+
+A little configuration need to be done on I18n so that:
+
+ * it can fallback on other locales if the requested one is not
+ available (ie: translation as not been done).
@zzak Owner
zzak added a note

"translation does not exist"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zzak zzak commented on the diff
development/i18n.md
((10 lines not shown))
+A little configuration need to be done on I18n so that:
+
+ * it can fallback on other locales if the requested one is not
+ available (ie: translation as not been done).
+ * all the transalations are read from YAML files located in the
+ `locales` directory
+
+````
+ configure
+ I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
+ I18n.load_path, Dir[File.join(settings.root, 'locales', '*.yml')]
+ I18n.backend.load_translations
+ end
+````
+
+Now we need to choose the locale that the user want. There are severals
@zzak Owner
zzak added a note

"that the user wants. There are several"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zzak zzak commented on the diff
development/i18n.md
((15 lines not shown))
+ `locales` directory
+
+````
+ configure
+ I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
+ I18n.load_path, Dir[File.join(settings.root, 'locales', '*.yml')]
+ I18n.backend.load_translations
+ end
+````
+
+Now we need to choose the locale that the user want. There are severals
+solutions (and some can even be mixed together): browser preference,
+spefic URL, dedicated subdomain, cookies/session management.
+Only the first three will be shown below:
+
+* Browser preference (will need `rack-contrib`)
@zzak Owner
zzak added a note

"requries rack-contrib"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zzak zzak commented on the diff
development/i18n.md
((41 lines not shown))
+ request.path_info = '/' + params[:splat ][0]
+ end
+````
+
+* Dedicated subdomain
+
+````
+ before do
+ if (locale = request.host.split('.')[0]) != 'www'
+ I18n.locale = locale
+ end
+ end
+````
+
+We have all the necessary information to deliver to the user
+texts/pages in its native language. And for that we will need to
@zzak Owner
zzak added a note

"in their native language"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zzak zzak commented on the diff
development/i18n.md
((44 lines not shown))
+
+* Dedicated subdomain
+
+````
+ before do
+ if (locale = request.host.split('.')[0]) != 'www'
+ I18n.locale = locale
+ end
+ end
+````
+
+We have all the necessary information to deliver to the user
+texts/pages in its native language. And for that we will need to
+select strings and templates according to the desired locale.
+
+Selection of localised strings/objects is easy as it only requires
@zzak Owner
zzak added a note

"localized strings/objects"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zzak zzak commented on the diff
development/i18n.md
((51 lines not shown))
+ end
+ end
+````
+
+We have all the necessary information to deliver to the user
+texts/pages in its native language. And for that we will need to
+select strings and templates according to the desired locale.
+
+Selection of localised strings/objects is easy as it only requires
+use of standard methods from `I18n`
+
+ I18n.t(:token)
+ I18n.l(Time.now)
+
+For rendering the templates matching the desired locale, we need to
+extend the `find_template` method. It need to select the first
@zzak Owner
zzak added a note

"It needs to select"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@zzak zzak referenced this pull request in sinatra/sinatra.github.com
Closed

Howto section on I18n with Sinatra #98

@sdalu

Feel free to apply all the wanted corrections

@zzak zzak merged commit 815a754 into sinatra:master
@zzak zzak referenced this pull request from a commit
@zzak zzak copy edits from #40 b59755c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 1, 2013
  1. @sdalu
This page is out of date. Refresh to see the latest.
Showing with 78 additions and 0 deletions.
  1. +78 −0 development/i18n.md
View
78 development/i18n.md
@@ -0,0 +1,78 @@
+How can I internationalise my application? {#i18n}
@zzak Owner
zzak added a note

internationalize

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+------------------------------------------
+
+We will rely on the `i18n` gem to handle internationalisation
+of strings and objects, and to manage fallbacks on available locales
+
+ require 'i18n'
+ require 'i18n/backend/fallbacks'
+
+A little configuration need to be done on I18n so that:
@zzak Owner
zzak added a note

I like "The following configuration is necessary on I18n so that:"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+ * it can fallback on other locales if the requested one is not
+ available (ie: translation as not been done).
@zzak Owner
zzak added a note

"translation does not exist"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ * all the transalations are read from YAML files located in the
+ `locales` directory
+
+````
+ configure
+ I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
+ I18n.load_path, Dir[File.join(settings.root, 'locales', '*.yml')]
+ I18n.backend.load_translations
+ end
+````
+
+Now we need to choose the locale that the user want. There are severals
@zzak Owner
zzak added a note

"that the user wants. There are several"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+solutions (and some can even be mixed together): browser preference,
+spefic URL, dedicated subdomain, cookies/session management.
+Only the first three will be shown below:
+
+* Browser preference (will need `rack-contrib`)
@zzak Owner
zzak added a note

"requries rack-contrib"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+
+````
+ use Rack::Locale
+````
+
+* Specific URL
+
+````
+ before '/:locale/*' do
+ I18n.locale = params[:locale]
+ request.path_info = '/' + params[:splat ][0]
+ end
+````
+
+* Dedicated subdomain
+
+````
+ before do
+ if (locale = request.host.split('.')[0]) != 'www'
+ I18n.locale = locale
+ end
+ end
+````
+
+We have all the necessary information to deliver to the user
+texts/pages in its native language. And for that we will need to
@zzak Owner
zzak added a note

"in their native language"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+select strings and templates according to the desired locale.
+
+Selection of localised strings/objects is easy as it only requires
@zzak Owner
zzak added a note

"localized strings/objects"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+use of standard methods from `I18n`
+
+ I18n.t(:token)
+ I18n.l(Time.now)
+
+For rendering the templates matching the desired locale, we need to
+extend the `find_template` method. It need to select the first
@zzak Owner
zzak added a note

"It needs to select"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+template matching the user locale (or at least one acceptable
+fallback). To help in the selection process templates stored
+in the `views` directory are suffixed by the name of the locale.
+
+ helpers do
+ def find_template(views, name, engine, &block)
+ I18n.fallbacks[I18n.locale].each { |locale|
+ super(views, "#{name}.#{locale}", engine, &block) }
+ super(views, name, engine, &block)
+ end
+ end
+
Something went wrong with that request. Please try again.