Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 15 additions & 0 deletions lib/mini_i18n/localization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ module Localization
DAYS_MONTHS_REGEX = /%[aAbB]/

def localize(object, options = {})
return multiple_localize(object, options) if object.is_a?(Array)
return multiple_locales_localize(object, options) if options[:locale].is_a?(Array)

case object
when Numeric
localize_number(object, options)
Expand Down Expand Up @@ -70,5 +73,17 @@ def localize_string(string, options)

object && localize(object, options)
end

def multiple_localize(objects, options)
objects.map do |obj|
localize(obj, options)
end
end

def multiple_locales_localize(object, options)
options[:locale].map do |_locale|
localize(object, options.merge(locale: _locale))
end
end
end
end
68 changes: 68 additions & 0 deletions spec/localization_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,72 @@
expect(MiniI18n.l(125.5, as: :distance)).to eq 'Distance -> 125.5 miles'
end
end

describe 'multiple objects' do
let(:date) { Date.new(2018, 8, 7) }
let(:number) { 1234.56 }

it 'localizes multiple objects with same options' do
result = MiniI18n.l([date, number])
expect(result).to eq ['Tuesday 07, August, 2018', '1,234.56']
end

it 'localizes multiple objects with specific locale' do
result = MiniI18n.l([number, 9000], locale: :es)
expect(result).to eq ['1.234,56', '9.000']
end

it 'localizes multiple objects with different formats' do
result = MiniI18n.l([date, time], format: :short)
expect(result).to eq ['07 Aug 18', '07 Aug 18 - 22:30']
end

it 'localizes multiple objects with as option' do
result = MiniI18n.l([1000, 2000], as: :currency)
expect(result).to eq ['1,000 $', '2,000 $']
end
end

describe 'multiple locales' do
let(:number) { 1234.56 }

it 'localizes same object for multiple locales' do
result = MiniI18n.l(number, locale: [:en, :es])
expect(result).to eq ['1,234.56', '1.234,56']
end

it 'localizes date for multiple locales' do
date = Date.new(2018, 8, 7)
result = MiniI18n.l(date, locale: [:en, :es])
expect(result).to eq ['Tuesday 07, August, 2018', '7/8/2018']
end

it 'localizes time for multiple locales' do
result = MiniI18n.l(time, locale: [:en, :es])
expect(result).to eq ['Tue 07, August, 2018 - 22:30', 'mar 7 de agosto de 2018 - 22:30']
end

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

it 'localizes with format for multiple locales' do
date = Date.new(2018, 8, 7)
result = MiniI18n.l(date, format: :short, locale: [:en, :es])
expect(result).to eq ['07 Aug 18', '7 ago 18']
end
end

describe 'alias method l' do
it 'works with multiple objects' do
result = MiniI18n.l([1000, 2000])
expect(result).to eq ['1,000', '2,000']
end

it 'works with multiple locales' do
result = MiniI18n.l(1000, locale: [:en, :es])
expect(result).to eq ['1,000', '1.000']
end
end
end
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
require 'simplecov'
SimpleCov.start { add_filter 'spec/' }
# require 'simplecov'
# SimpleCov.start { add_filter 'spec/' }

require "mini_i18n"

Expand Down