Index: /home/workspace/drupalhispano/sites/all/modules/l10n_server/l10n_community/l10n_community.install =================================================================== --- /home/workspace/drupalhispano/sites/all/modules/l10n_server/l10n_community/l10n_community.install (revision 224) +++ /home/workspace/drupalhispano/sites/all/modules/l10n_server/l10n_community/l10n_community.install (working copy) @@ -170,6 +170,28 @@ } /** + * Clean up starting and trailing spaces and new lines in translations + */ +function l10n_community_update_5003() { + $ret = array(); + // We just search for strings that begin or end with these characters + foreach (array('\n', ' ') as $char) { + $result = db_query("SELECT s.sid, s.value, t.tid, t.translation FROM {l10n_community_string} s INNER JOIN {l10n_community_translation} t ON s.sid = t.sid WHERE s.value LIKE '%s%%' OR s.value LIKE '%%%s' OR t.translation LIKE '%s%%' OR t.translation LIKE '%%%s'", $char, $char, $char, $char); + while ($string = db_fetch_object($result)) { + if ($string->translation) { + $trimmed = l10n_community_trim($string->translation, $string->value); + if ($trimmed != $string->translation) { + db_query("UPDATE {l10n_community_translation} SET translation = '%s' WHERE tid = %d", $trimmed, $string->tid); + drupal_set_message("Fixed string $string->sid: ". check_plain(substr($string->translation, 0, 50))); + } + } + } + } + + return $ret; +} + +/** * Implementation of hook_uninstall(). */ function l10n_community_uninstall() { Index: /home/workspace/drupalhispano/sites/all/modules/l10n_server/l10n_community/l10n_community.module =================================================================== --- /home/workspace/drupalhispano/sites/all/modules/l10n_server/l10n_community/l10n_community.module (revision 225) +++ /home/workspace/drupalhispano/sites/all/modules/l10n_server/l10n_community/l10n_community.module (working copy) @@ -666,10 +666,11 @@ */ function l10n_community_target_save($sid, $translation, $langcode, $uid, $suggestion, &$inserted, &$updated, &$unchanged, &$suggested) { - // Look for an existing active translation. - $existing_string = db_fetch_object(db_query("SELECT sid, tid, translation FROM {l10n_community_translation} WHERE sid = %d AND language = '%s' AND is_suggestion = 0 AND is_active = 1", $sid, $langcode)); - - if (isset($existing_string->sid)) { + // Look for an existing active translation and grab source string. + $existing_string = db_fetch_object(db_query("SELECT s.value, t.sid, t.tid, t.translation FROM {l10n_community_string} s LEFT JOIN {l10n_community_translation} t ON s.sid = t.sid WHERE s.sid = %d AND (t.sid IS NULL OR t.language = '%s' AND t.is_suggestion = 0 AND t.is_active = 1)", $sid, $langcode)); + $translation = l10n_community_trim($translation, $existing_string->value); + + if (!empty($existing_string->sid)) { // We have an active translation. if ($existing_string->translation != $translation) { // And what we should save now is different. @@ -708,6 +709,23 @@ } /** + * Special trimming, make spacing and new lines the same in translation as in the source + * + * @param $translation + * Translation string + * @param $source + * Source string + * @return + * Translation string with the right beginning and ending chars + */ +function l10n_community_trim($translation, $source) { + $matches = array(); + $chars = '\n\s\r\t'; + preg_match("/^([$chars]*).*[^$chars]([$chars]*)\$/", $source, $matches); + return $matches[1].trim($translation).$matches[2]; +} + +/** * Set a message based on the number of translations changed. * * Used by both the save and import process.