Merge pull request #853 from bhollis/i18n
Include I18n::Fallbacks and set the default I18n locale to our default locale.
This commit is contained in:
commit
53bddf8304
|
@ -203,3 +203,16 @@ Feature: i18n Preview
|
|||
Then I should see '"../stylesheets/site.css"'
|
||||
When I go to "/es/hola.html"
|
||||
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 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
|
||||
|
|
Loading…
Reference in a new issue