To make your pages available to a wider audience, localization is absolutly neccesary. Nitro provides you with some simple mechanismn for translating your application.
Changelog: 24.05.06: Update to comply to new nitro (transformation_pipeline)
First, in your run.rb, you have to change the compiler-pipeline so it includes Localization - this was never added to the standard-pipeline and the compilers are still being thought over, so for now, do it like that :)
Also, to initialize the localization you have to add a hash. This really invites you to save your localization in an external YAML-file so that other people can edit it quite simple too.
locales = {
:en => {
'hello world' => "Hello world",
'hello' => "hello",
:Hello => "Hello",
:world => "world",
:welcome => 'Welcome on my Homepage',
:exclamation_mark => "!"
},
:de => {
'hello world' => "Hallo Welt",
'hello' => "hallo",
:Hello => "Hallo",
:world => "Welt",
:welcome => 'Willkommen auf meiner Homepage!',
:exclamation_mark => "!"
}
}
Glue::Localization.add(locales)
module Nitro
class Controller
ann :self, :transformation_pipeline => [
StaticInclude, Morphing, Elements,
Markup, ScriptCompiler, Localization,
Cleanup
]
end
end
Your Controller should look like:
class MainController < Nitro::Controller wrap LocalizationAspect, :pre => :localize @lc = Glue::Localization.get def toggle_locales session[:LOCALE] = ((session[:LOCALE] == :de) ? :en : :de) redirect_referer end end
And add a little link to your skin/template that points to locales_toggle so you can... yeah, toggle your locales. If you want to use locales in your Controller you can access it through the @lc-hash that holds your current locales depending on the content of session[:LOCALE]
def index @title = @lc[:welcome] end
and your Templates can use it via the double-brackets [[]] which looks like this (using Symbols):
[[:Hello]] [[:world]][[:exclamation_mark]]
Then you will be rewarded by:
session[:LOCALE] = :en @lc[:world] #=> "Hello world!" session[:LOCALE] = :de @lc[:world] #=> "Hallo Welt!"
Using Strings:
[[hello world]]
Will get you
session[:LOCALE] = :en "Hello world!" session[:LOCALE] = :de "Hallo Welt!"
Also you can use both symbols or strings as Hash-keys, you should use what you like more, using strings you can have a more natural feeling in the templates since you can put spaces between the words, Symbols would be the right choice if you have a huge application with lots of requests since it has some performance-gains.
Check "Symbols are not immutable Strings":http://onestepback.org/index.cgi/Tech/Ruby/SymbolsAreNotImmutableStrings.red to find out more about Symbols vs. Strings