From 3a4c0635353375811b2fb8cd0b17420bd2acfd19 Mon Sep 17 00:00:00 2001 From: Thomas Reynolds Date: Sun, 30 Sep 2012 13:17:01 -0700 Subject: [PATCH] Reload i18n files when they change, closes #616 --- CHANGELOG.md | 1 + middleman-more/features/i18n_preview.feature | 32 ++++++++++++++++++- .../middleman-more/core_extensions/i18n.rb | 20 +++++++++--- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 09b996cd..dd68a5a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ Master === * Bundled Normalize.css updated to 2.0.1 * Fixed encoding extension activation +* Reload i18n on file changes (#616) 3.0.5 ==== diff --git a/middleman-more/features/i18n_preview.feature b/middleman-more/features/i18n_preview.feature index 28be0482..81360e23 100644 --- a/middleman-more/features/i18n_preview.feature +++ b/middleman-more/features/i18n_preview.feature @@ -18,7 +18,37 @@ Feature: i18n Preview Then I should see "Como Esta?" When I go to "/es/hola.html" Then I should see "Hola World" - + + Scenario: A template changes i18n during preview + Given a fixture app "i18n-test-app" + And a file named "config.rb" with: + """ + activate :i18n + """ + Given the Server is running at "i18n-test-app" + And the file "locales/en.yml" has the contents + """ + --- + en: + greetings: "Howdy" + hi: "Hello" + """ + When I go to "/" + Then I should see "Howdy" + When I go to "/hello.html" + Then I should see "Hello World" + When the file "locales/en.yml" has the contents + """ + --- + en: + greetings: "How You Doin" + hi: "Sup" + """ + When I go to "/" + Then I should see "How You Doin" + When I go to "/hello.html" + Then I should see "Sup World" + Scenario: Running localize with the alt path config Given a fixture app "i18n-test-app" And a file named "config.rb" with: diff --git a/middleman-more/lib/middleman-more/core_extensions/i18n.rb b/middleman-more/lib/middleman-more/core_extensions/i18n.rb index 0978dfa2..eb23e70f 100644 --- a/middleman-more/lib/middleman-more/core_extensions/i18n.rb +++ b/middleman-more/lib/middleman-more/core_extensions/i18n.rb @@ -13,10 +13,12 @@ module Middleman # Needed for helpers as well app.after_configuration do - ::I18n.load_path += Dir[File.join(root, locales_dir, "*.yml")] + locales_glob = File.join(locales_dir, "*.yml"); + + ::I18n.load_path += Dir[File.join(root, locales_glob)] ::I18n.reload! - Localizer.new(self, options) + Localizer.new(self, locales_glob, options) end end alias :included :registered @@ -27,8 +29,9 @@ module Middleman attr_reader :app delegate :logger, :to => :app - def initialize(app, options={}) + def initialize(app, locales_glob, options={}) @app = app + @locales_glob = locales_glob @maps = {} @options = options @@ -62,11 +65,20 @@ module Middleman :i18n, self ) + + @app.files.changed(&method(:on_file_changed)) + @app.files.deleted(&method(:on_file_changed)) + end + + def on_file_changed(file) + if File.fnmatch(@locales_glob, file) + ::I18n.reload! + end end def langs @options[:langs] || begin - Dir[File.join(@app.root, @app.locales_dir, "*.yml")].map { |file| + Dir[File.join(@app.root, @locales_glob)].map { |file| File.basename(file).gsub(".yml", "") }.sort.map(&:to_sym) end