Fix language detection for .po formatters

The old regular expressions were only supporting two character language
identifiers. This commit allows languages to scale to things like
"en-GB".

Fixes 
This commit is contained in:
Sebastian Celis 2017-07-27 09:10:38 -05:00
parent 6686a64743
commit 1270ef2767
3 changed files with 12 additions and 6 deletions

View file

@ -16,7 +16,7 @@ module Twine
def determine_language_given_path(path)
path_arr = path.split(File::SEPARATOR)
path_arr.each do |segment|
match = /(..)\.po$/.match(segment)
match = /([a-z]{2}(-[A-Za-z]{2})?)\.po$/.match(segment)
return match[1] if match
end

View file

@ -18,10 +18,8 @@ module Twine
def determine_language_given_path(path)
path_arr = path.split(File::SEPARATOR)
path_arr.each do |segment|
match = /(..)\.po$/.match(segment)
if match
return match[1]
end
match = /([a-z]{2}(-[A-Za-z]{2})?)\.po$/.match(segment)
return match[1] if match
end
return

View file

@ -267,7 +267,6 @@ class TestJQueryFormatter < FormatterTest
end
class TestGettextFormatter < FormatterTest
def setup
super Twine::Formatters::Gettext
end
@ -290,6 +289,10 @@ class TestGettextFormatter < FormatterTest
assert_equal content('formatter_gettext.po'), formatter.format_file('en')
end
def test_deducts_language_and_region
language = "en-GB"
assert_equal language, @formatter.determine_language_given_path("#{language}.po")
end
end
class TestTizenFormatter < FormatterTest
@ -329,6 +332,11 @@ class TestDjangoFormatter < FormatterTest
formatter.twine_file = @twine_file
assert_equal content('formatter_django.po'), formatter.format_file('en')
end
def test_deducts_language_and_region
language = "en-GB"
assert_equal language, @formatter.determine_language_given_path("#{language}.po")
end
end
class TestFlashFormatter < FormatterTest