diff --git a/packages/utils/src/Localization.php b/packages/utils/src/Localization.php index fd76e74..0e112b1 100755 --- a/packages/utils/src/Localization.php +++ b/packages/utils/src/Localization.php @@ -65,13 +65,17 @@ public function load_script_translation_file($file, $handle, $domain) { $domain ); - $locale = determine_locale(); if ( $domain === $packageDomain && !is_readable($useFile) && substr($useFile, 0, strlen($packagePath)) === $packagePath ) { // Collect data + $locale = $this->getLanguageFromFile($file); + if ($locale === false) { + return $file; + } + $folder = dirname($useFile); $use = $this->override($locale); $wp_scripts = wp_scripts(); @@ -85,6 +89,22 @@ public function load_script_translation_file($file, $handle, $domain) { return $file; } + /** + * Obtain language key from a file name. + * + * @param string $file + */ + public function getLanguageFromFile($file) { + $availableLanguages = get_available_languages(); + $availableLanguages[] = 'en_US'; + preg_match_all('/-(' . join('|', $availableLanguages) . ')-/m', basename($file), $matches, PREG_SET_ORDER, 0); + + if (count($matches) === 0) { + return false; + } + return $matches[0][1]; + } + /** * Allow language overrides so for example de_AT uses de_DE to avoid duplicate * .po files and management. This is for backend PHP files! diff --git a/packages/utils/test/phpunit/LocalizationTest.php b/packages/utils/test/phpunit/LocalizationTest.php index 8e56c91..ee0cb35 100755 --- a/packages/utils/test/phpunit/LocalizationTest.php +++ b/packages/utils/test/phpunit/LocalizationTest.php @@ -101,7 +101,7 @@ public function testLoadScriptTranslationFile() { $domain = PHPUNIT_TD; $parsedPackageInfo = [$file, $path, $domain, $domain]; $handle = 'phpunit-admin'; - $currentLocale = 'en_CA'; + $currentLocale = 'en_US'; $this->localization->shouldReceive('load_script_translation_file')->passthru(); $this->localization @@ -109,7 +109,12 @@ public function testLoadScriptTranslationFile() { ->with(Localization::$PACKAGE_INFO_FRONTEND, $file, $domain) ->andReturn($parsedPackageInfo); - WP_Mock::userFunction('determine_locale', ['return' => $currentLocale]); + $this->localization + ->shouldReceive('getLanguageFromFile') + ->once() + ->with($file) + ->andReturn($currentLocale); + WP_Mock::userFunction('path_join', ['return' => $path, 'args' => [PHPUNIT_PATH, Mockery::any()]]); redefine('is_readable', always(false)); @@ -130,6 +135,31 @@ public function testLoadScriptTranslationFile() { $this->addToAssertionCount(1); } + public function testLoadScriptTranslationFileNoMatchingLanguage() { + $file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit--phpunit-admin.json'; + $path = '/var/www/html/wp-content/plugins/phpunit/public/languages/json'; + $domain = PHPUNIT_TD; + $parsedPackageInfo = [$file, $path, $domain, $domain]; + $handle = 'phpunit-admin'; + + $this->localization->shouldReceive('load_script_translation_file')->passthru(); + $this->localization + ->shouldReceive('packageInfoParserForOverrides') + ->with(Localization::$PACKAGE_INFO_FRONTEND, $file, $domain) + ->andReturn($parsedPackageInfo); + + $this->localization + ->shouldReceive('getLanguageFromFile') + ->once() + ->with($file) + ->andReturn(false); + $this->localization->shouldNotReceive('override'); + + $actual = $this->localization->load_script_translation_file($file, $handle, $domain); + + $this->assertEquals($file, $actual); + } + public function testLoadScriptTranslationFileNotMatchingDomain() { $file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit-en_US-phpunit-admin.json'; $path = '/var/www/html/wp-content/plugins/phpunit/public/languages/json'; @@ -137,7 +167,7 @@ public function testLoadScriptTranslationFileNotMatchingDomain() { $anotherDomain = 'another-domain'; $parsedPackageInfo = [$file, $path, $domain, $domain]; $handle = 'phpunit-admin'; - $currentLocale = 'en_CA'; + $currentLocale = 'en_US'; $this->localization->shouldReceive('load_script_translation_file')->passthru(); $this->localization @@ -145,7 +175,6 @@ public function testLoadScriptTranslationFileNotMatchingDomain() { ->with(Localization::$PACKAGE_INFO_FRONTEND, $file, $anotherDomain) ->andReturn($parsedPackageInfo); - WP_Mock::userFunction('determine_locale', ['return' => $currentLocale]); WP_Mock::userFunction('path_join', ['return' => $path, 'args' => [PHPUNIT_PATH, Mockery::any()]]); $this->localization->shouldNotReceive('override'); @@ -169,7 +198,6 @@ public function testLoadScriptTranslationFileNotMatchingPlugin() { ->with(Localization::$PACKAGE_INFO_FRONTEND, $file, $domain) ->andReturn($parsedPackageInfo); - WP_Mock::userFunction('determine_locale', ['return' => $currentLocale]); WP_Mock::userFunction('path_join', [ 'return' => $path, 'args' => [PHPUNIT_PATH, Assets::$PUBLIC_JSON_I18N] @@ -184,6 +212,44 @@ public function testLoadScriptTranslationFileNotMatchingPlugin() { $this->assertEquals($file, $actual); } + public function testGetLanguageFromFile() { + $should = 'en_US'; + $file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit-en_US-phpunit-admin.json'; + + $this->localization->shouldReceive('getLanguageFromFile')->passthru(); + + WP_Mock::userFunction('get_available_languages', ['times' => 1, 'return' => ['de_DE', 'de_DE_formal']]); + + $actual = $this->localization->getLanguageFromFile($file); + + $this->assertEquals($should, $actual); + } + + public function testGetLanguageFromFileNoMatch() { + $file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit--phpunit-admin.json'; + + $this->localization->shouldReceive('getLanguageFromFile')->passthru(); + + WP_Mock::userFunction('get_available_languages', ['times' => 1, 'return' => ['de_DE', 'de_DE_formal']]); + + $actual = $this->localization->getLanguageFromFile($file); + + $this->assertFalse($actual); + } + + public function testGetLanguageFromFileNoInstalledLanguages() { + $should = 'en_US'; + $file = '/var/www/html/wp-content/plugins/phpunit/public/languages/json/phpunit-en_US-phpunit-admin.json'; + + $this->localization->shouldReceive('getLanguageFromFile')->passthru(); + + WP_Mock::userFunction('get_available_languages', ['times' => 1, 'return' => []]); + + $actual = $this->localization->getLanguageFromFile($file); + + $this->assertEquals($should, $actual); + } + public function testOverrideLoadTextdomain() { $mofile = '/var/www/html/wp-content/plugins/phpunit/languages/phpunit-en_US.mo'; $domain = PHPUNIT_TD;