Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ You can also use the long form methods:
=> "Hola"
>> T(:hello, locale: [:en, :fr, :es])
=> ["Hello", "Bonjour", "Hola"]
>> T(:hello, locale: '*')
=> ["Hello", "Hola", "Bonjour"] # Uses all available locales
```

**`scope`**
Expand Down Expand Up @@ -339,6 +341,10 @@ dates = [Date.new(2023, 12, 25), Date.new(2024, 1, 1)]
>> L(1234.56, locale: [:en, :es, :fr])
=> ["1,234.56", "1.234,56", "1 234,56"]

# Use '*' to automatically include all available locales
>> L(1234.56, locale: '*')
=> ["1,234.56", "1.234,56", "1 234,56"]

# Localize date across different locales
date = Date.new(2023, 8, 15)
>> L(date, locale: [:en, :es, :de])
Expand All @@ -347,6 +353,10 @@ date = Date.new(2023, 8, 15)
# Currency formatting across locales
>> L(999.99, as: :currency, locale: [:en, :es, :de])
=> ["999.99 $", "999,99 €", "999,99 €"]

# Currency formatting for all available locales
>> L(1000, as: :currency, locale: '*')
=> ["1,000 $", "1.000 €", nil] # nil when locale doesn't have currency format
```

**Combining multiple objects and locales**
Expand Down
10 changes: 10 additions & 0 deletions lib/mini_i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ def translate(key, options = {})
return if key.empty? || translations.empty?

return multiple_translate(key, options) if key.is_a?(Array)

options = expand_all_locales(options)
return multiple_locales(key, options) if options[:locale].is_a?(Array)

_locale = available_locale?(options[:locale]) || locale
Expand Down Expand Up @@ -172,5 +174,13 @@ def multiple_locales(key, options)
t(key, options.merge(locale: _locale))
end
end

def expand_all_locales(options)
if options[:locale] == '*'
options.merge(locale: available_locales)
else
options
end
end
end
end
2 changes: 2 additions & 0 deletions lib/mini_i18n/localization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ module Localization

def localize(object, options = {})
return multiple_localize(object, options) if object.is_a?(Array)

options = expand_all_locales(options)
return multiple_locales_localize(object, options) if options[:locale].is_a?(Array)

case object
Expand Down
19 changes: 19 additions & 0 deletions spec/localization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,5 +103,24 @@
end
end

describe 'all locales with *' do
let(:number) { 1234.56 }

before do
# Ensure built-in locale data is loaded for proper formatting
MiniI18n.available_locales = [:en, :es, :fr]
end

it 'localizes number for all locales' do
result = MiniI18n.l(number, locale: '*')
expect(result).to eq ['1,234.56', '1.234,56', '1 234,56']
end

it 'localizes with currency for all locales' do
result = MiniI18n.l(1000, as: :currency, locale: '*')
expect(result).to eq ['1,000 $', '1.000 €', nil]
end
end


end
5 changes: 5 additions & 0 deletions spec/mini_i18n_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,11 @@
expect(MiniI18n.t(:hello_interpolation, name: 'world', locale: [:en])).to eq ['hello world']
end

it "all locales with '*'" do
expect(MiniI18n.t(:hello, locale: '*')).to eq ['hello', 'hola', 'bonjour']
expect(MiniI18n.t(:hello_interpolation, name: 'world', locale: '*')).to eq ['hello world', 'hola world', 'bonjour world']
end

it "scope" do
expect(MiniI18n.t('hello', scope: :second_level)).to eq 'hello 2'
end
Expand Down