forked from pkp/pkp-lib
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
pkp#10616 Add ThemePlugin function to get localized theme option value
- Loading branch information
Showing
3 changed files
with
117 additions
and
63 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
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,99 @@ | ||
<?php | ||
|
||
/** | ||
* @file classes/core/traits/LocalizedData.php | ||
* | ||
* Copyright (c) 2014-2024 Simon Fraser University | ||
* Copyright (c) 2000-2024 John Willinsky | ||
* Distributed under the GNU GPL v3. For full terms see the file docs/COPYING. | ||
* | ||
* @class LocalizedData | ||
* | ||
* @ingroup core_traits | ||
* | ||
* @brief A trait for getting localized data from assoc arrays | ||
* | ||
*/ | ||
|
||
namespace PKP\core\traits; | ||
|
||
use APP\core\Application; | ||
use PKP\facades\Locale; | ||
|
||
trait LocalizedData | ||
{ | ||
/** @var Conversion table for locales */ | ||
public array $_localesTable = [ | ||
'be@cyrillic' => 'be', | ||
'bs' => 'bs_Latn', | ||
'fr_FR' => 'fr', | ||
'nb' => 'nb_NO', | ||
'sr@cyrillic' => 'sr_Cyrl', | ||
'sr@latin' => 'sr_Latn', | ||
'uz@cyrillic' => 'uz', | ||
'uz@latin' => 'uz_Latn', | ||
'zh_CN' => 'zh_Hans', | ||
]; | ||
|
||
/** | ||
* Get a piece of data, localized to the current | ||
* locale if possible. | ||
* | ||
* @param array $data An assoc array with localized data, where each | ||
* key is the localeKey. Example: ['en' => 'Journal', 'de' => 'Zeitschrift'] | ||
*/ | ||
protected function getBestLocalizedData(array $data, ?string $preferredLocale = null, ?string &$selectedLocale = null): mixed | ||
{ | ||
foreach ($this->getLocalePrecedence($preferredLocale) as $locale) { | ||
if (!empty($data[$locale])) { | ||
$selectedLocale = $locale; | ||
return $data[$locale]; | ||
} | ||
} | ||
|
||
// Fallback: Get the first available piece of data. | ||
foreach ($data as $locale => $dataValue) { | ||
if (!empty($dataValue)) { | ||
$selectedLocale = $locale; | ||
return $dataValue; | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* Get the locale precedence order for object data in the following order | ||
* | ||
* 1. Preferred Locale if provided | ||
* 2. User's current local | ||
* 3. Object's default locale if set | ||
* 4. Context's primary locale if context available | ||
* 5. Site's primary locale | ||
*/ | ||
public function getLocalePrecedence(?string $preferredLocale = null): array | ||
{ | ||
$request = Application::get()->getRequest(); | ||
|
||
return array_unique( | ||
array_filter([ | ||
$preferredLocale ?? Locale::getLocale(), | ||
$this->_localesTable[$preferredLocale ?? Locale::getLocale()] ?? null, | ||
$this->getDefaultLocale(), | ||
$request->getContext()?->getPrimaryLocale(), | ||
$request->getSite()->getPrimaryLocale(), | ||
]) | ||
); | ||
} | ||
|
||
/** | ||
* Get the default locale | ||
* | ||
* Override this method in the object which uses this trait, if the object | ||
* has a default locale. Most objects don't have a default locale. | ||
*/ | ||
public function getDefaultLocale(): ?string | ||
{ | ||
return null; | ||
} | ||
} |
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