Commit 611af51d authored by catch's avatar catch

Issue #532512 by Sutharsan: Follow-up for plural string storage upgrade path.

parent efc984df
...@@ -386,88 +386,90 @@ function locale_update_8005() { ...@@ -386,88 +386,90 @@ function locale_update_8005() {
} }
$plural_lids = array_unique($plural_lids); $plural_lids = array_unique($plural_lids);
// Look up all translations for these source strings. Ordering by language if (!empty($plural_lids)) {
// will group the strings by language, the 'plid' order will get the // Look up all translations for these source strings. Ordering by language
// strings in singular/plural order and 'plural' will get them in precise // will group the strings by language, the 'plid' order will get the
// sequential order needed. // strings in singular/plural order and 'plural' will get them in precise
$results = db_query("SELECT s.lid, s.source, t.translation, t.plid, t.plural, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.lid IN (:lids) ORDER BY t.language, t.plid, t.plural", array(':lids' => $plural_lids)); // sequential order needed.
$results = db_query("SELECT s.lid, s.source, t.translation, t.plid, t.plural, t.language FROM {locales_source} s LEFT JOIN {locales_target} t ON s.lid = t.lid WHERE s.lid IN (:lids) ORDER BY t.language, t.plid, t.plural", array(':lids' => $plural_lids));
// Collect the strings into an array and combine values as we go.
$strings = array(); // Collect the strings into an array and combine values as we go.
$parents_to_sources = array(); $strings = array();
$remove_lids = array(); $parents_to_sources = array();
foreach ($results as $child) { $remove_lids = array();
$strings[$child->language][$child->lid] = array( foreach ($results as $child) {
'source' => array($child->source), $strings[$child->language][$child->lid] = array(
'translation' => array($child->translation), 'source' => array($child->source),
); 'translation' => array($child->translation),
);
if (empty($child->plid)) {
// Non-children strings point to themselves as parents. This makes it if (empty($child->plid)) {
// easy to look up the utmost parents for any plurals. // Non-children strings point to themselves as parents. This makes it
$parents_to_sources[$child->lid] = $child->lid; // easy to look up the utmost parents for any plurals.
} $parents_to_sources[$child->lid] = $child->lid;
else {
// Children strings point to their utmost parents. Because we get data
// in PLID order, we can ensure that all previous parents have data now,
// so we can just copy the parent's data about their parent, etc.
$parents_to_sources[$child->lid] = $parents_to_sources[$child->plid];
// Append translation to the utmost parent's translation string.
$utmost_parent = &$strings[$child->language][$parents_to_sources[$child->plid]];
// Drop the Drupal-specific numbering scheme from the end of plural
// formulas.
$utmost_parent['translation'][] = str_replace('@count[' . $child->plural .']', '@count', $child->translation);
if (count($utmost_parent['source']) < 2) {
// Append source to the utmost parent's source string only if it is the
// plural variant. Further Drupal specific plural variants are not to be
// retained for source strings.
$utmost_parent['source'][] = $child->source;
} }
else {
// Children strings point to their utmost parents. Because we get data
// in PLID order, we can ensure that all previous parents have data now,
// so we can just copy the parent's data about their parent, etc.
$parents_to_sources[$child->lid] = $parents_to_sources[$child->plid];
// Append translation to the utmost parent's translation string.
$utmost_parent = &$strings[$child->language][$parents_to_sources[$child->plid]];
// Drop the Drupal-specific numbering scheme from the end of plural
// formulas.
$utmost_parent['translation'][] = str_replace('@count[' . $child->plural .']', '@count', $child->translation);
if (count($utmost_parent['source']) < 2) {
// Append source to the utmost parent's source string only if it is the
// plural variant. Further Drupal specific plural variants are not to be
// retained for source strings.
$utmost_parent['source'][] = $child->source;
}
// All plural variant LIDs are to be removed with their translations. // All plural variant LIDs are to be removed with their translations.
// Only the singular LIDs will be kept. // Only the singular LIDs will be kept.
$remove_lids[] = $child->lid; $remove_lids[] = $child->lid;
}
} }
}
// Do updates for all source strings and all translations. // Do updates for all source strings and all translations.
$updated_sources = array(); $updated_sources = array();
foreach ($strings as $langcode => $translations) { foreach ($strings as $langcode => $translations) {
foreach($translations as $lid => $translation) { foreach($translations as $lid => $translation) {
if (!in_array($lid, $updated_sources)) { if (!in_array($lid, $updated_sources)) {
// Only update source string if not yet updated. We merged these within // Only update source string if not yet updated. We merged these within
// the translation lookups because plural information was only avilable // the translation lookups because plural information was only avilable
// with the translation, but we don't need to save it again for every // with the translation, but we don't need to save it again for every
// language. // language.
db_update('locales_source') db_update('locales_source')
->fields(array( ->fields(array(
'source' => implode(LOCALE_PLURAL_DELIMITER, $translation['source']), 'source' => implode(LOCALE_PLURAL_DELIMITER, $translation['source']),
)) ))
->condition('lid', $lid)
->execute();
$updated_sources[] = $lid;
}
db_update('locales_target')
->fields(array(
'translation' => implode(LOCALE_PLURAL_DELIMITER, $translation['translation']),
))
->condition('lid', $lid) ->condition('lid', $lid)
->condition('language', $langcode)
->execute(); ->execute();
$updated_sources[] = $lid;
} }
db_update('locales_target')
->fields(array(
'translation' => implode(LOCALE_PLURAL_DELIMITER, $translation['translation']),
))
->condition('lid', $lid)
->condition('language', $langcode)
->execute();
} }
}
// Remove all plural LIDs from source and target. only keep those which were // Remove all plural LIDs from source and target. only keep those which were
// originally used for the singular strings (now updated to contain the // originally used for the singular strings (now updated to contain the
// serialized version of plurals). // serialized version of plurals).
$remove_lids = array_unique($remove_lids); $remove_lids = array_unique($remove_lids);
db_delete('locales_source') db_delete('locales_source')
->condition('lid', $remove_lids, 'IN') ->condition('lid', $remove_lids, 'IN')
->execute(); ->execute();
db_delete('locales_target') db_delete('locales_target')
->condition('lid', $remove_lids, 'IN') ->condition('lid', $remove_lids, 'IN')
->execute(); ->execute();
}
// Drop the primary key because it contains 'plural'. // Drop the primary key because it contains 'plural'.
db_drop_primary_key('locales_target'); db_drop_primary_key('locales_target');
......
...@@ -136,4 +136,18 @@ class LanguageUpgradePathTestCase extends UpgradePathTestCase { ...@@ -136,4 +136,18 @@ class LanguageUpgradePathTestCase extends UpgradePathTestCase {
$domains = locale_language_negotiation_url_domains(); $domains = locale_language_negotiation_url_domains();
$this->assertTrue($domains['ca'] == $language_domain, t('Language domain for Catalan properly upgraded.')); $this->assertTrue($domains['ca'] == $language_domain, t('Language domain for Catalan properly upgraded.'));
} }
/**
* Tests upgrading translations without plurals.
*/
public function testLanguageNoPluralsUpgrade() {
// Remove all plural translations from the database.
db_delete('locales_target')->condition('plural', 0, '<>')->execute();
$this->assertTrue($this->performUpgrade(), t('The upgrade was completed successfully.'));
// Check if locale_update_8005() is succesfully completed by checking
// whether index 'plural' has been removed.
$this->assertFalse(db_index_exists('locales_target', 'plural'), t('Translations without plurals upgraded.'));
}
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment