-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from michieldewit/master
Load all default fallbacks for a specific locale, by including all prefix subpatterns
- Loading branch information
Showing
4 changed files
with
97 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
lib/i18n_country_translations/locale_files_pattern_generator.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
module I18nCountryTranslations | ||
|
||
# Generates patterns for locale files, bases on a list of supported locales | ||
class LocaleFilesPatternGenerator | ||
|
||
attr_reader :base_pattern, :extension | ||
|
||
def initialize(base_pattern, extension = '.yml') | ||
@base_pattern = base_pattern | ||
@extension = extension | ||
end | ||
|
||
# Generates a glob file pattern for the specified list of locales (i.e. IETF language tags) | ||
def pattern_from(locales) | ||
locales = Array(locales || []) | ||
locales = locales.map { |locale| subpatterns_from locale }.flatten | ||
pattern = locales.blank? ? '*' : "{#{locales.join ','}}" | ||
"#{base_pattern}#{pattern}#{extension}" | ||
end | ||
|
||
protected | ||
|
||
# Generates subpatterns for the specified locale (i.e. IETF language tag). | ||
# Subpatterns are all more generic variations of a locale. | ||
# E.g. subpatterns for en-US are en-US and en. Subpatterns for az-Latn-IR are az-Latn-IR, az-Latn and az | ||
def subpatterns_from(locale) | ||
parts = locale.to_s.split('-') | ||
parts.map.with_index { |part,index| parts[0..index].join('-') } | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
62 changes: 62 additions & 0 deletions
62
spec/lib/i18n_country_translations/locale_files_pattern_generator_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
require 'i18n_country_translations/locale_files_pattern_generator' | ||
|
||
describe I18nCountryTranslations::LocaleFilesPatternGenerator do | ||
|
||
context "with given locale" do | ||
let(:generator) { I18nCountryTranslations::LocaleFilesPatternGenerator.new('base/', '.yml') } | ||
|
||
def test_pattern(pattern, sample) | ||
if File.const_defined? 'FNM_EXTGLOB' | ||
# Use proper way to test, by calling fnmatch and actually executing the pattern. | ||
# This only works in Ruby 2.1.0 and upwards | ||
File.fnmatch(pattern, sample, File::FNM_EXTGLOB) | ||
else | ||
# Roughly approximate pattern matching using regular expressions. Only recognizes the braces { } syntax. | ||
# Other glob syntax (e.g. * and ** is not recognized). So keep test cases simple! | ||
pattern = '^' + pattern.split(/\{(.+?)\}/).each_with_index.map do |part, index| | ||
if index.even? | ||
# Pattern part outside of { } | ||
Regexp.escape(part) | ||
else | ||
'(' + part.split(',').map { |option| Regexp.escape(option) }.join('|') + ')' | ||
end | ||
end.join + '$' | ||
|
||
# Test against regular expression pattern | ||
(sample =~ Regexp.new(pattern)) == 0 | ||
end | ||
end | ||
|
||
it "generates a pattern for simple locales" do | ||
locales = ['en', 'fr'] | ||
pattern = generator.pattern_from(locales) | ||
|
||
# Make sure pattern matches locales | ||
locales.each do |locale| | ||
expect(test_pattern(pattern, "base/#{locale}.yml")).to be true | ||
end | ||
|
||
# Make sure pattern does not match other locales | ||
['nl', 'en-US', 'fra', 'cen'].each do |locale| | ||
expect(test_pattern(pattern, "base/#{locale}.yml")).to be false | ||
end | ||
end | ||
|
||
it "generates a pattern for locales with tags" do | ||
locales = ['en-US', 'az-Latn-IR'] | ||
sublocales = ['en', 'en-US', 'az', 'az-Latn', 'az-Latn-IR'] | ||
pattern = generator.pattern_from(locales) | ||
|
||
# Make sure pattern matches locales | ||
sublocales.each do |locale| | ||
expect(test_pattern(pattern, "base/#{locale}.yml")).to be true | ||
end | ||
|
||
# Make sure pattern does not match other locales | ||
['en-AU', 'az-IR'].each do |locale| | ||
expect(test_pattern(pattern, "base/#{locale}.yml")).to be false | ||
end | ||
end | ||
end | ||
|
||
end |