Include I18n::Fallbacks and set the default I18n locale to our default locale.

Thus, when a translation is missing in the current locale, fall back to the default locale. See https://github.com/svenfuchs/i18n/wiki/Fallbacks for more info and configuration settings available to users.

Also, this change adds a "t" helper that proxies to I18n.t, just like Rails, for more concise translating.
This commit is contained in:
Ben Hollis 2013-04-06 17:21:56 -07:00
parent ee10d207a1
commit 17b3e3e628
5 changed files with 52 additions and 2 deletions

View file

@ -202,4 +202,17 @@ Feature: i18n Preview
When I go to "/es/index.html"
Then I should see '"../stylesheets/site.css"'
When I go to "/es/hola.html"
Then I should see '"../stylesheets/site.css"'
Then I should see '"../stylesheets/site.css"'
Scenario: Missing translations fall back to the default locale
Given a fixture app "i18n-default-app"
And a file named "config.rb" with:
"""
activate :i18n, :mount_at_root => :es
"""
Given the Server is running at "i18n-default-app"
When I go to "/en/"
Then I should see "Default locale: es"
Then I should see "Current locale: en"
Then I should see "Buenos días"
Then I should see "Howdy"

View file

@ -0,0 +1,4 @@
---
en:
greetings: "Howdy"
hi: "Hello"

View file

@ -0,0 +1,8 @@
---
es:
paths:
hello: "hola"
greetings: "Como Esta?"
hi: "Hola"
untranslated: "Buenos días"

View file

@ -0,0 +1,5 @@
Default locale: <%= I18n.default_locale %>
Current locale: <%= I18n.locale %>
Fallbacks: <%= I18n.fallbacks[:en] %>
<%= t(:untranslated) %>
<%= t(:greetings) %>

View file

@ -1,3 +1,6 @@
require "i18n"
require "i18n/backend/fallbacks"
module Middleman
module CoreExtensions
@ -15,6 +18,17 @@ module Middleman
app.after_configuration do
Localizer.new(self, options)
end
app.helpers do
def t(*args)
::I18n.t(*args)
end
end
# See https://github.com/svenfuchs/i18n/wiki/Fallbacks
unless options[:no_fallbacks]
::I18n::Backend::Simple.send(:include, I18n::Backend::Fallbacks)
end
end
alias :included :registered
end
@ -43,6 +57,12 @@ module Middleman
@templates_dir = @options[:templates_dir] || "localizable"
@mount_at_root = @options.has_key?(:mount_at_root) ? @options[:mount_at_root] : langs.first
::I18n.default_locale = @mount_at_root
# Reset fallbacks to fall back to our new default
if ::I18n.respond_to? :fallbacks
::I18n.fallbacks = ::I18n::Locale::Fallbacks.new
end
if !@app.build?
logger.info "== Locales: #{langs.join(", ")} (Default #{@mount_at_root})"
end
@ -117,7 +137,7 @@ module Middleman
langs.map do |lang|
::I18n.locale = lang
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id)
localized_page_id = ::I18n.t("paths.#{page_id}", :default => page_id, :fallback => [])
path = resource.path.sub(@templates_dir, "")
# Build lang path