Skip to content

Commit

Permalink
Use translation for default language, if exists
Browse files Browse the repository at this point in the history
There is an assumption in the code that for the default language a translation is not used.

However, the features around draft versions are all set up around
translations, so it becomes necessary to create translations
even for the default language. This change ensures they are
used if they exist.

Although we have a lot of complexity in the translation framework
on the rendering side I think assuming that where stuff is configured
people configured it to be used is a good underlying principle.
  • Loading branch information
eileenmcnaughton committed May 22, 2023
1 parent 6924a00 commit 52f8265
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion CRM/Core/BAO/Translation.php
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ public static function hook_civicrm_apiWrappers(&$wrappers, $apiRequest): void {
}

$communicationLanguage = \Civi\Core\Locale::detect()->nominal;
if ($communicationLanguage === Civi::settings()->get('lcMessages')) {
if (!$communicationLanguage || !self::isTranslate($communicationLanguage)) {
return;
}

Expand All @@ -205,6 +205,34 @@ public static function hook_civicrm_apiWrappers(&$wrappers, $apiRequest): void {
}
}

/**
* Should the translation process be followed.
*
* It can be short-circuited if there we are in the site default language and
* it is not translated.
*
* @param string $communicationLanguage
*
* @return bool
*/
protected static function isTranslate(string $communicationLanguage): bool {
if ($communicationLanguage !== Civi::settings()->get('lcMessages')) {
return TRUE;
}
if (!isset(\Civi::$statics[__CLASS__]['translate_main'][$communicationLanguage])) {
// The code had an assumption that you would not translate the primary language.
// However, the UI is such that the features (approval flow) so it makes sense
// to translation the default site language as well. If we can see sites are
// doing this then let's treat the main locale like any other locale
\Civi::$statics[__CLASS__]['translate_main'] = (bool) CRM_Core_DAO::singleValueQuery(
'SELECT COUNT(*) FROM civicrm_translation WHERE language = %1 LIMIT 1', [
1 => [$communicationLanguage, 'String'],
]
);
}
return \Civi::$statics[__CLASS__]['translate_main'];
}

/**
* @param \Civi\Api4\Generic\AbstractAction $apiRequest
* @return array translated fields.
Expand Down

0 comments on commit 52f8265

Please sign in to comment.