Download:
Download(18 MB, 11:11)
Alternativer Download für iPod & Apple TV(18.3 MB, 11:11)
Resourcen:
- Episode 138: I18n
- i18n
- i18n-active_record
- Redis
- Crafting Rails Applications
- Full episode source code
Quellcode:
[bash]
rails g controller translations index
brew install redis
[/bash]
[ruby]
# Gemfile
gem ‚redis‘
# config/initializers/i18n_backend.rb
TRANSLATION_STORE = Redis.new
I18n.backend = I18n::Backend::Chain.new(I18n::Backend::KeyValue.new(TRANSLATION_STORE), I18n.backend)
# translations_controller.rb
def index
@translations = TRANSLATION_STORE
end
def create
I18n.backend.store_translations(params[:locale], {params[:key] => params[:value]}, :escape => false)
redirect_to translations_url, :notice => "Added translation"
end
[/ruby]
[html]
<!– home/index.html.erb –>
<h1><%= t "welcome" %></h1>
<!– translations/index.html.erb –>
<h1>Translations</h1>
<ul>
<% @translations.keys.each do |key| %>
<li><%= key %>: <%= @translations[key] %></li>
<% end %>
</ul>
<h2>Add Translation</h2>
<%= form_tag translations_path do %>
<p>
<%= label_tag :locale %><br />
<%= text_field_tag :locale %>
</p>
<p>
<%= label_tag :key %><br />
<%= text_field_tag :key %>
</p>
<p>
<%= label_tag :value %><br />
<%= text_field_tag :value %>
</p>
<p><%= submit_tag "Submit" %></p>
<% end %>
[/html]
[text]
# config/locales/en.yml
en:
welcome: "Welcome"
[/text]