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:
parent
ee10d207a1
commit
17b3e3e628
5 changed files with 52 additions and 2 deletions
|
@ -202,4 +202,17 @@ Feature: i18n Preview
|
||||||
When I go to "/es/index.html"
|
When I go to "/es/index.html"
|
||||||
Then I should see '"../stylesheets/site.css"'
|
Then I should see '"../stylesheets/site.css"'
|
||||||
When I go to "/es/hola.html"
|
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"
|
||||||
|
|
4
middleman-more/fixtures/i18n-default-app/locales/en.yml
Normal file
4
middleman-more/fixtures/i18n-default-app/locales/en.yml
Normal file
|
@ -0,0 +1,4 @@
|
||||||
|
---
|
||||||
|
en:
|
||||||
|
greetings: "Howdy"
|
||||||
|
hi: "Hello"
|
8
middleman-more/fixtures/i18n-default-app/locales/es.yml
Normal file
8
middleman-more/fixtures/i18n-default-app/locales/es.yml
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
---
|
||||||
|
es:
|
||||||
|
paths:
|
||||||
|
hello: "hola"
|
||||||
|
|
||||||
|
greetings: "Como Esta?"
|
||||||
|
hi: "Hola"
|
||||||
|
untranslated: "Buenos días"
|
|
@ -0,0 +1,5 @@
|
||||||
|
Default locale: <%= I18n.default_locale %>
|
||||||
|
Current locale: <%= I18n.locale %>
|
||||||
|
Fallbacks: <%= I18n.fallbacks[:en] %>
|
||||||
|
<%= t(:untranslated) %>
|
||||||
|
<%= t(:greetings) %>
|
|
@ -1,3 +1,6 @@
|
||||||
|
require "i18n"
|
||||||
|
require "i18n/backend/fallbacks"
|
||||||
|
|
||||||
module Middleman
|
module Middleman
|
||||||
module CoreExtensions
|
module CoreExtensions
|
||||||
|
|
||||||
|
@ -15,6 +18,17 @@ module Middleman
|
||||||
app.after_configuration do
|
app.after_configuration do
|
||||||
Localizer.new(self, options)
|
Localizer.new(self, options)
|
||||||
end
|
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
|
end
|
||||||
alias :included :registered
|
alias :included :registered
|
||||||
end
|
end
|
||||||
|
@ -43,6 +57,12 @@ module Middleman
|
||||||
@templates_dir = @options[:templates_dir] || "localizable"
|
@templates_dir = @options[:templates_dir] || "localizable"
|
||||||
@mount_at_root = @options.has_key?(:mount_at_root) ? @options[:mount_at_root] : langs.first
|
@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?
|
if !@app.build?
|
||||||
logger.info "== Locales: #{langs.join(", ")} (Default #{@mount_at_root})"
|
logger.info "== Locales: #{langs.join(", ")} (Default #{@mount_at_root})"
|
||||||
end
|
end
|
||||||
|
@ -117,7 +137,7 @@ module Middleman
|
||||||
langs.map do |lang|
|
langs.map do |lang|
|
||||||
::I18n.locale = 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, "")
|
path = resource.path.sub(@templates_dir, "")
|
||||||
|
|
||||||
# Build lang path
|
# Build lang path
|
||||||
|
|
Loading…
Reference in a new issue