From cd8cbeb216a516577d231ef46e407ef0d5e32b42 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Fri, 5 Aug 2022 03:22:58 -0700 Subject: [PATCH] APIv4 - Promote option `$language` from DAOActionTrait to AbstractAction --- Civi/Api4/Generic/AbstractAction.php | 27 +++++++++++++++++++++ Civi/Api4/Generic/Traits/DAOActionTrait.php | 11 ++------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/Civi/Api4/Generic/AbstractAction.php b/Civi/Api4/Generic/AbstractAction.php index 9347394ada..51c76450b7 100644 --- a/Civi/Api4/Generic/AbstractAction.php +++ b/Civi/Api4/Generic/AbstractAction.php @@ -32,6 +32,8 @@ use Civi\Api4\Utils\ReflectionUtils; * @method bool getDebug() * @method $this setChain(array $chain) * @method array getChain() + * @method $this setLanguage(string|null $language) + * @method string|null getLanguage() */ abstract class AbstractAction implements \ArrayAccess { @@ -44,6 +46,20 @@ abstract class AbstractAction implements \ArrayAccess { */ protected $version = 4; + /** + * Preferred language (optional) + * + * This option will notify major localization subsystems (`ts()`, multilingual, etc) + * about which locale should be used for composing/formatting messaging. + * + * This indicates the preferred language. The effective language is determined + * by `Civi\Core\Locale::negotiate($preferredLanguage)`. + * + * @var string + * @optionsCallback getPreferredLanguageOptions + */ + protected $language; + /** * Additional api requests - will be called once per result. * @@ -564,4 +580,15 @@ abstract class AbstractAction implements \ArrayAccess { } } + /** + * Get available preferred languages. + * + * @return array + */ + protected function getPreferredLanguageOptions(): array { + $languages = \CRM_Contact_BAO_Contact::buildOptions('preferred_language'); + ksort($languages); + return array_keys($languages); + } + } diff --git a/Civi/Api4/Generic/Traits/DAOActionTrait.php b/Civi/Api4/Generic/Traits/DAOActionTrait.php index 4e7dd548c4..047cdd26c2 100644 --- a/Civi/Api4/Generic/Traits/DAOActionTrait.php +++ b/Civi/Api4/Generic/Traits/DAOActionTrait.php @@ -19,20 +19,13 @@ use Civi\Api4\Utils\CoreUtil; use Civi\Api4\Utils\ReflectionUtils; /** - * @method string getLanguage() - * @method $this setLanguage(string $language) + * Common properties and helper-methods used for DB-oriented actions. */ trait DAOActionTrait { /** - * Specify the language to use if this is a multi-lingual environment. - * - * E.g. "en_US" or "fr_CA" - * - * @var string + * @var array */ - protected $language; - private $_maxWeights = []; /** -- 2.25.1