Hi
I suggest that the author check whether the translation field is null or not, instead of checking if empty or not.
I encountered an issue with submitting string translation. This is the error message I got:
PDOException: SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry "lang-code-lid-0" for key 'PRIMARY': INSERT INTO {locales_target} (lid, translation, language) VALUES (:db_insert_placeholder_0, :db_insert_placeholder_1, :db_insert_placeholder_2); Array ( [:db_insert_placeholder_0] => lid [:db_insert_placeholder_1] => translation text [:db_insert_placeholder_2] => lang-code ) in locale_translate_edit_form_submit() (line 1213 of /mnt/www/html/powerintdev/docroot/modules/locale/locale.admin.inc).
I looked into my database and found that this particular data already exist but the translation is an empty string
| lid | translation | language | plid | plural | i18n_status |
+-------+-------------+----------+------+--------+-------------+
| lid | | lang-code | 0 | 0 | 1 |
I checked the code in locale_translate_edit_form_submit(). it checks if the translation field in the locales_target table is empty, if yes then insert the translation. In my case, the translation text is an empty string (but the data row does exist), so when using !empty($translation), the result is no, according to http://us1.php.net/empty (an empty string is considered to be empty)
, then db_insert is used and it generate error of "duplicate entry" because "insert" is supposed to add new data, not update existing data. So, I suggest that the author check whether the translation field is null or not, instead of checking if empty or not. Or use
$translation = db_query("SELECT Count (*) FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid, ':language' => $key))->fetchField();
if ($translation) {
db_update('locales_target').....
else (
db_insert('locales_target').....
This is the original code:
$translation = db_query("SELECT translation FROM {locales_target} WHERE lid = :lid AND language = :language", array(':lid' => $lid, ':language' => $key))->fetchField();
if (!empty($translation)) {
db_update('locales_target')
->fields(array(
'translation' => $value,
))
->condition('lid', $lid)
->condition('language', $key)
->execute();
}
else {
db_insert('locales_target')
->fields(array(
'lid' => $lid,
'translation' => $value,
'language' => $key,
))
->execute();
}
API page: https://api.drupal.org/api/drupal/modules%21locale%21locale.module/7
Enter a descriptive title (above) relating to locale.module, then describe the problem you have found:
Comment | File | Size | Author |
---|---|---|---|
#2 | drupal-fix_empty_translation_error-2201615-2.patch | 1.69 KB | hgoto |
#2 | drupal-fix_empty_translation_error-2201615-2-test_only.patch | 1 KB | hgoto |
Comments
Comment #1
Stanislav_B CreditAttribution: Stanislav_B commentedI faced with this issue too when try translate strings from "Path breadcrumbs" module. Thank you Pearl that help to resolve this issue for me.
I just replace
if (!empty($translation))
toif (is_string($translation) || !empty($translation))
I think this issue related to this - https://www.drupal.org/node/2669614
I am surprised how core issue opened more than two years when change one line code is enough to fix it
Comment #2
hgoto CreditAttribution: hgoto as a volunteer and at Studio Umi commentedThank you for sharing and confirming the issue. I also think this should be fixed.
Surely this occurs if I change the database cell value but it seems to be impossible to register such an empty string row in the locales_target table through the admin UI. How did you insert an empty target string in the table?
Anyway. For the next step, I created a patch for this following the suggested ideas. This contains a test, too. I'd like someone to review this.
(I changed the version because this exists in 7.x-dev.)
Comment #4
hgoto CreditAttribution: hgoto as a volunteer and at Studio Umi commentedI checked if D8 has the same issue and found it doesn't.
The D8 version of
locale_translate_edit_form_submit()
isTranslateEditForm::submitForm()
and it checks the existence of the record properly.Comment #5
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedRTBC, looks good to me.
Test looks good to.
Comment #6
stefan.r CreditAttribution: stefan.r commentedComment #7
stefan.r CreditAttribution: stefan.r commentedComment #8
Fabianx CreditAttribution: Fabianx as a volunteer and at Tag1 Consulting commentedI think Stefan should commit that as I RTBC'ed it.
Comment #10
stefan.r CreditAttribution: stefan.r commentedCommitted and pushed to 7.x, thanks!
Comment #11
hgoto CreditAttribution: hgoto as a volunteer and at Studio Umi commentedThank you!
Comment #12
stefan.r CreditAttribution: stefan.r commented