Updates translation associated to a specific locale source.
string $lid: The Locale ID.
string $target_language: Target language to update translation.
string $translation: Translation value.
bool Success or not updating the locale translation.
protected function updateTranslation($lid, $target_language, $translation) {
$languages = locale_translatable_language_list('name', TRUE);
if (!$lid || !array_key_exists($target_language, $languages) || !$translation) {
return FALSE;
}
$exists = \Drupal::database()
->query("SELECT COUNT(lid) FROM {locales_target} WHERE lid = :lid AND language = :language", array(
':lid' => $lid,
':language' => $target_language,
))
->fetchField();
// @todo Only singular strings are managed here, we should take care of
// plural information of processed string.
if (!$exists) {
$fields = array(
'lid' => $lid,
'language' => $target_language,
'translation' => $translation,
'customized' => LOCALE_CUSTOMIZED,
);
\Drupal::database()
->insert('locales_target')
->fields($fields)
->execute();
}
else {
$fields = array(
'translation' => $translation,
'customized' => LOCALE_CUSTOMIZED,
);
\Drupal::database()
->update('locales_target')
->fields($fields)
->condition('lid', $lid)
->condition('language', $target_language)
->execute();
}
// Clear locale caches.
_locale_invalidate_js($target_language);
\Drupal::cache()
->delete('locale:' . $target_language);
return TRUE;
}