Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Locale.all and all_locales_options #109

Merged
merged 5 commits into from
Feb 15, 2018
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
4 changes: 1 addition & 3 deletions app/helpers/solidus_i18n/locale_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,8 @@ def available_locales_options
Config.available_locales.map { |locale| locale_presentation(locale) }
end

# Need to manually add en to the array because the en.yml was moved from
# this project. solidusio/solidus now has those keys.
def all_locales_options
SolidusI18n::Locale.all.map { |locale| locale_presentation(locale) }.push(['English (EN)', :en])
SolidusI18n::Locale.all.map { |locale| locale_presentation(locale) }
end

private
Expand Down
2 changes: 1 addition & 1 deletion config/locales/en-IN.yml
Original file line number Diff line number Diff line change
Expand Up @@ -631,7 +631,7 @@ en-IN:
available_locales:
language:
localization_settings:
this_file_language: English (UK)
this_file_language: English (IN)
icon: Icon
identifier:
image: Image
Expand Down
2 changes: 1 addition & 1 deletion config/locales/es-MX.yml
Original file line number Diff line number Diff line change
Expand Up @@ -636,7 +636,7 @@ es-MX:
available_locales: Traduciones Disponibles
language: Idioma
localization_settings: Ajustes de traducciones
this_file_language:
this_file_language: Castellano (MX)
icon: Icono
identifier:
image: Imagen
Expand Down
10 changes: 3 additions & 7 deletions lib/solidus_i18n/locale.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
module SolidusI18n
class Locale
class << self
def all
Dir["#{dir}/*.yml"].map { |f| File.basename(f, '.yml').to_sym }
end

def dir
File.join(File.dirname(__FILE__), '/../../config/locales')
def self.all
I18n.available_locales.select do |locale|
I18n.t(:spree, locale: locale, fallback: false, default: nil)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice and sweet 👌

end
end
end
Expand Down
35 changes: 35 additions & 0 deletions spec/helpers/locale_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'

RSpec.describe SolidusI18n::LocaleHelper do
describe '#all_locales_options' do
subject { all_locales_options }

it 'includes en' do
is_expected.to include(["English (US)", :en])
end

it 'includes ja' do
is_expected.to include(["日本語 (ja-JP)", :ja])
end

describe 'locales' do
subject { all_locales_options.map(&:last) }

it 'includes each locale only once' do
is_expected.to match_array(subject.uniq)
end

it 'should match Locale.all' do
is_expected.to match_array SolidusI18n::Locale.all
end
end

describe 'locale presentation' do
subject { all_locales_options.map(&:first) }

it 'should all be unique' do
is_expected.to match_array(subject.uniq)
end
end
end
end
53 changes: 53 additions & 0 deletions spec/lib/solidus_i18n/locale_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
require 'spec_helper'

RSpec.describe SolidusI18n::Locale do
describe '.all' do
subject { SolidusI18n::Locale.all }

it "Contains all available Solidus locales" do
# Add to this list when adding/removing locales
expect(subject).to match_array %i[
en
zh-CN
cs
zh-TW
it
nl
da
tr
id
ro
pt-BR
ja
es
fr
de
ru
uk
ko
pt
et
sk
pl
nb
fa
fi
en-NZ
en-IN
en-AU
bg
en-GB
de-CH
es-MX
es-CL
th
ca
vi
sv
es-EC
lv
sl-SI
]
end
end
end