Skip to content

Commit

Permalink
3.2.0: can specify fallback array like [:sv]
Browse files Browse the repository at this point in the history
  • Loading branch information
henrik committed Apr 14, 2016
1 parent aee2545 commit f5619d0
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 1 deletion.
4 changes: 4 additions & 0 deletions 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.
Expand Down
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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)`.
Expand Down
3 changes: 3 additions & 0 deletions lib/traco/locale_fallbacks.rb
Expand Up @@ -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
Expand All @@ -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})")
Expand Down
2 changes: 1 addition & 1 deletion lib/traco/version.rb
@@ -1,3 +1,3 @@
module Traco
VERSION = "3.1.6"
VERSION = "3.2.0"
end
7 changes: 7 additions & 0 deletions spec/locale_fallbacks_spec.rb
Expand Up @@ -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
Expand Down

0 comments on commit f5619d0

Please sign in to comment.