diff --git a/CHANGELOG.md b/CHANGELOG.md index 503dc44..eacb6aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 3.2.0 + +* Introduce e.g. `fallback: [:sv]` to explicitly specify fallbacks. + ## 3.1.6 Make [license (MIT)](LICENSE.txt) easier to autodetect. diff --git a/README.md b/README.md index f0300c2..abf18ef 100644 --- a/README.md +++ b/README.md @@ -94,6 +94,8 @@ You can specify e.g. `translates :title, fallback: false` to never fall back and You can specify e.g. `translates :title, fallback: :any` to fall back first to the default locale, then to any other locale. +You can specify e.g. `translates :title, fallback: [:sv]` to explicitly declare fallbacks as an array of any length. + You can override the default fallback strategy with a parameter passed to the reader: `post.title(fallback: :any)`. If you need to declare the default locale fallback, do `post.title(fallback: :default)`. diff --git a/lib/traco/locale_fallbacks.rb b/lib/traco/locale_fallbacks.rb index e4e4a15..626689f 100644 --- a/lib/traco/locale_fallbacks.rb +++ b/lib/traco/locale_fallbacks.rb @@ -24,6 +24,7 @@ def [](current_locale) when ANY_FALLBACK then [ current_locale, @default_locale, *@available_locales ].uniq when NO_FALLBACK then [ current_locale ] when DEFAULT_FIRST_FALLBACK then [ @default_locale, *@available_locales ].uniq + when Array then fallback_option else raise "Unknown fallback." # Should never get here. end end @@ -33,6 +34,8 @@ def [](current_locale) def validate_option(fallback_option) if OPTIONS.include?(fallback_option) fallback_option + elsif fallback_option.is_a?(Array) + fallback_option else valids = OPTIONS.map(&:inspect).join(", ") raise ArgumentError.new("Unsupported fallback: #{fallback_option.inspect} (expected one of #{valids})") diff --git a/lib/traco/version.rb b/lib/traco/version.rb index a1d9bb2..684f0a5 100644 --- a/lib/traco/version.rb +++ b/lib/traco/version.rb @@ -1,3 +1,3 @@ module Traco - VERSION = "3.1.6" + VERSION = "3.2.0" end diff --git a/spec/locale_fallbacks_spec.rb b/spec/locale_fallbacks_spec.rb index ad9bca5..aebc0d1 100644 --- a/spec/locale_fallbacks_spec.rb +++ b/spec/locale_fallbacks_spec.rb @@ -35,6 +35,13 @@ end end + context "with an explicit list of locales" do + it "returns only those locales" do + subject = Traco::LocaleFallbacks.new([ :en ]) + expect(subject[:sv]).to eq [ :en ] + end + end + context "with the ':default_first' option" do it "returns default locale, then remaining available locales alphabetically" do I18n.default_locale = :uk