diff --git a/middleman-more/features/i18n_preview.feature b/middleman-more/features/i18n_preview.feature index bfc591ef..ebcf5f5e 100644 --- a/middleman-more/features/i18n_preview.feature +++ b/middleman-more/features/i18n_preview.feature @@ -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"' \ No newline at end of file + 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" diff --git a/middleman-more/fixtures/i18n-default-app/locales/en.yml b/middleman-more/fixtures/i18n-default-app/locales/en.yml new file mode 100644 index 00000000..91519342 --- /dev/null +++ b/middleman-more/fixtures/i18n-default-app/locales/en.yml @@ -0,0 +1,4 @@ +--- +en: + greetings: "Howdy" + hi: "Hello" \ No newline at end of file diff --git a/middleman-more/fixtures/i18n-default-app/locales/es.yml b/middleman-more/fixtures/i18n-default-app/locales/es.yml new file mode 100644 index 00000000..68f75889 --- /dev/null +++ b/middleman-more/fixtures/i18n-default-app/locales/es.yml @@ -0,0 +1,8 @@ +--- +es: + paths: + hello: "hola" + + greetings: "Como Esta?" + hi: "Hola" + untranslated: "Buenos días" \ No newline at end of file diff --git a/middleman-more/fixtures/i18n-default-app/source/localizable/index.html.erb b/middleman-more/fixtures/i18n-default-app/source/localizable/index.html.erb new file mode 100644 index 00000000..e033169c --- /dev/null +++ b/middleman-more/fixtures/i18n-default-app/source/localizable/index.html.erb @@ -0,0 +1,5 @@ +Default locale: <%= I18n.default_locale %> +Current locale: <%= I18n.locale %> +Fallbacks: <%= I18n.fallbacks[:en] %> +<%= t(:untranslated) %> +<%= t(:greetings) %> diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index 6321ed34..8f8630a0 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -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