Merge pull request #8726 from open-sausages/pulls/4/extra-locale-formats

MINOR Update add_i18n_javascript to load lowercase-dash language files
This commit is contained in:
Maxime Rainville 2019-01-16 15:28:37 +13:00 committed by GitHub
commit 074cf04b60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 91 additions and 6 deletions

View File

@ -18,6 +18,7 @@ use SilverStripe\Core\Path;
use SilverStripe\Dev\Debug;
use SilverStripe\Dev\Deprecation;
use SilverStripe\i18n\i18n;
use SilverStripe\ORM\FieldType\DBField;
class Requirements_Backend
{
@ -1004,13 +1005,23 @@ class Requirements_Backend
$files = array();
$candidates = array(
'en.js',
'en_US.js',
i18n::getData()->langFromLocale(i18n::config()->get('default_locale')) . '.js',
i18n::config()->get('default_locale') . '.js',
i18n::getData()->langFromLocale(i18n::get_locale()) . '.js',
i18n::get_locale() . '.js',
'en',
'en_US',
i18n::getData()->langFromLocale(i18n::config()->get('default_locale')),
i18n::config()->get('default_locale'),
i18n::getData()->langFromLocale(i18n::get_locale()),
i18n::get_locale(),
strtolower(DBField::create_field('Locale', i18n::get_locale())->RFC1766()),
strtolower(DBField::create_field('Locale', i18n::config()->get('default_locale'))->RFC1766())
);
$candidates = array_map(
function ($candidate) {
return $candidate . '.js';
},
$candidates
);
foreach ($candidates as $candidate) {
$relativePath = Path::join($langDir, $candidate);
$absolutePath = Director::getAbsFile($relativePath);

View File

@ -6,6 +6,7 @@ use InvalidArgumentException;
use SilverStripe\Control\Director;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\i18n\i18n;
use SilverStripe\View\Requirements;
use SilverStripe\View\ArrayData;
use Silverstripe\Assets\Dev\TestAssetStore;
@ -1109,4 +1110,77 @@ EOS
}
return array();
}
public function testAddI18nJavascript()
{
/** @var Requirements_Backend $backend */
$backend = Injector::inst()->create(Requirements_Backend::class);
$this->setupRequirements($backend);
$backend->add_i18n_javascript('i18n');
$actual = $backend->getJavascript();
// English and English US should always be loaded no matter what
$this->assertArrayHasKey('i18n/en.js', $actual);
$this->assertArrayHasKey('i18n/en_US.js', $actual);
$this->assertArrayHasKey('i18n/en-us.js', $actual);
}
public function testAddI18nJavascriptWithDefaultLocale()
{
i18n::config()->set('default_locale', 'fr_CA');
/** @var Requirements_Backend $backend */
$backend = Injector::inst()->create(Requirements_Backend::class);
$this->setupRequirements($backend);
$backend->add_i18n_javascript('i18n');
$actual = $backend->getJavascript();
$this->assertArrayHasKey('i18n/en.js', $actual);
$this->assertArrayHasKey('i18n/en_US.js', $actual);
$this->assertArrayHasKey('i18n/en-us.js', $actual);
// Default locale should be loaded
$this->assertArrayHasKey('i18n/fr.js', $actual);
$this->assertArrayHasKey('i18n/fr_CA.js', $actual);
$this->assertArrayHasKey('i18n/fr-ca.js', $actual);
}
public function testAddI18nJavascriptWithMemberLocale()
{
i18n::set_locale('en_GB');
/** @var Requirements_Backend $backend */
$backend = Injector::inst()->create(Requirements_Backend::class);
$this->setupRequirements($backend);
$backend->add_i18n_javascript('i18n');
$actual = $backend->getJavascript();
// The current member's Locale as defined by i18n::get_locale should be loaded
$this->assertArrayHasKey('i18n/en.js', $actual);
$this->assertArrayHasKey('i18n/en_US.js', $actual);
$this->assertArrayHasKey('i18n/en-us.js', $actual);
$this->assertArrayHasKey('i18n/en-gb.js', $actual);
$this->assertArrayHasKey('i18n/en_GB.js', $actual);
}
public function testAddI18nJavascriptWithMissingLocale()
{
i18n::set_locale('fr_BE');
/** @var Requirements_Backend $backend */
$backend = Injector::inst()->create(Requirements_Backend::class);
$this->setupRequirements($backend);
$backend->add_i18n_javascript('i18n');
$actual = $backend->getJavascript();
// We don't have a file for French Belgium. Regular french should be loaded anyway.
$this->assertArrayHasKey('i18n/en.js', $actual);
$this->assertArrayHasKey('i18n/en_US.js', $actual);
$this->assertArrayHasKey('i18n/en-us.js', $actual);
$this->assertArrayHasKey('i18n/fr.js', $actual);
}
}

View File

View File

View File