Permalink
Browse files

Add standards_for and standards_for_language to LanguageCodes.

  • Loading branch information...
1 parent 42e76ae commit c9db75d423220c70546768950bba8fa2d6dfbd86 @KL-7 KL-7 committed Aug 14, 2012
Showing with 72 additions and 18 deletions.
  1. +19 −9 lib/twitter_cldr/shared/language_codes.rb
  2. +53 −9 spec/shared/language_codes_spec.rb
@@ -9,33 +9,43 @@ module LanguageCodes
LANGUAGE_CODES_DUMP_PATH = File.join(TwitterCldr::RESOURCES_DIR, 'shared', 'language_codes_table.dump')
- VALID_STANDARDS = [:bcp_47, :iso_639_1, :iso_639_2, :iso_639_3, :name]
+ NAME_STANDARD = :name # fake standard, mostly for internal use
+
+ VALID_STANDARDS = [:bcp_47, :iso_639_1, :iso_639_2, :iso_639_3, NAME_STANDARD]
class << self
def languages
- resource[:name].keys
+ resource[NAME_STANDARD].keys
end
def valid_standard?(standard)
VALID_STANDARDS.include?(standard.to_sym)
end
- def valid_code?(standard, code)
+ def valid_code?(code, standard)
resource[validate_standard(standard)].has_key?(code.to_sym)
end
def convert(code, from_and_to = {})
from, to = extract_from_and_to_options(from_and_to)
- resource[from.to_sym].fetch(code.to_sym, {})[to.to_sym]
+ resource[from].fetch(code.to_sym, {})[to]
+ end
+
+ def from_language(language, standard)
+ convert(language, :from => NAME_STANDARD, :to => standard)
+ end
+
+ def to_language(code, standard)
+ convert(code, :from => standard, :to => NAME_STANDARD).to_s
end
- def from_language_name(name, standard)
- convert(name, :from => :name, :to => standard)
+ def standards_for(code, standard)
+ resource[validate_standard(standard)].fetch(code.to_sym, {}).keys - [NAME_STANDARD] # exclude fake NAME_STANDARD standard
end
- def to_language_name(code, standard)
- convert(code, :from => standard, :to => :name).to_s
+ def standards_for_language(language)
+ standards_for(language, NAME_STANDARD)
end
private
@@ -55,7 +65,7 @@ def validate_standard(standard)
raise ArgumentError, "standard can't be nil" if standard.nil?
raise ArgumentError, "#{standard.inspect} is not a valid standard name" unless valid_standard?(standard)
- standard
+ standard.to_sym
end
end
@@ -40,15 +40,19 @@
describe '#valid_code?' do
it 'returns true if the code is present in the given standard' do
- LanguageCodes.valid_code?(:iso_639_2, :spa).should be_true
+ LanguageCodes.valid_code?(:spa, :iso_639_2).should be_true
end
it 'returns false if the code is not present in the given standard' do
- LanguageCodes.valid_code?(:iso_639_2, :foo).should be_false
+ LanguageCodes.valid_code?(:foo, :iso_639_2).should be_false
end
it 'raises exception if the standard is invalid' do
- lambda { LanguageCodes.valid_code?(:foobar, :es) }.should raise_exception(ArgumentError, ':foobar is not a valid standard name')
+ lambda { LanguageCodes.valid_code?(:es, :foobar) }.should raise_exception(ArgumentError, ':foobar is not a valid standard name')
+ end
+
+ it 'accepts strings' do
+ LanguageCodes.valid_code?('spa', 'iso_639_2').should be_true
end
end
@@ -95,23 +99,63 @@
end
end
- describe '#from_language_name' do
+ describe '#from_language' do
it 'returns language code by language name' do
- LanguageCodes.from_language_name('Spanish', :iso_639_2).should == :spa
+ LanguageCodes.from_language(:Spanish, :iso_639_2).should == :spa
+ end
+
+ it 'accepts strings' do
+ LanguageCodes.from_language('Spanish', 'iso_639_2').should == :spa
end
it 'raises exception when standard is invalid' do
- lambda { LanguageCodes.from_language_name('Spanish', :foobar) }.should raise_exception(':foobar is not a valid standard name')
+ lambda { LanguageCodes.from_language(:Spanish, :foobar) }.should raise_exception(':foobar is not a valid standard name')
end
end
- describe '#to_language_name' do
+ describe '#to_language' do
it 'returns language name as a string by language code' do
- LanguageCodes.to_language_name(:es, :iso_639_1).should == 'Spanish'
+ LanguageCodes.to_language(:es, :iso_639_1).should == 'Spanish'
+ end
+
+ it 'accepts strings' do
+ LanguageCodes.to_language('es', 'iso_639_1').should == 'Spanish'
+ end
+
+ it 'raises exception when standard is invalid' do
+ lambda { LanguageCodes.to_language(:es, :foobar) }.should raise_exception(':foobar is not a valid standard name')
+ end
+ end
+
+ describe '#standards_for' do
+ let(:spanish_standards) { [:bcp_47, :iso_639_1, :iso_639_2, :iso_639_3] }
+
+ it 'returns an array of standards that are available for conversion from a given code' do
+ LanguageCodes.standards_for(:es, :bcp_47).should =~ spanish_standards
+ end
+
+ it 'accepts string' do
+ LanguageCodes.standards_for('es', 'bcp_47').should =~ spanish_standards
end
it 'raises exception when standard is invalid' do
- lambda { LanguageCodes.to_language_name(:es, :foobar) }.should raise_exception(':foobar is not a valid standard name')
+ lambda { LanguageCodes.standards_for(:es, :foobar) }.should raise_exception(':foobar is not a valid standard name')
+ end
+ end
+
+ describe '#standards_for_language' do
+ let(:spanish_standards) { [:bcp_47, :iso_639_1, :iso_639_2, :iso_639_3] }
+
+ it 'returns an array of standards that have a code for a given language' do
+ LanguageCodes.standards_for_language(:Spanish).should =~ spanish_standards
+ end
+
+ it 'accepts string' do
+ LanguageCodes.standards_for_language('Spanish').should =~ spanish_standards
+ end
+
+ it 'returns empty array for unknown languages' do
+ LanguageCodes.standards_for_language('FooBar').should == []
end
end
end

0 comments on commit c9db75d

Please sign in to comment.