summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--i18n_string/i18n_string.inc93
-rw-r--r--i18n_string/i18n_string.module82
2 files changed, 85 insertions, 90 deletions
diff --git a/i18n_string/i18n_string.inc b/i18n_string/i18n_string.inc
index 4395594..7668420 100644
--- a/i18n_string/i18n_string.inc
+++ b/i18n_string/i18n_string.inc
@@ -194,11 +194,14 @@ class i18n_string_default {
* Get source string provided a string object.
*
* @param $context
- * Context string or object.
+ * Context array or string object.
* @return
- * Context object if it exists.
+ * String object if it exists.
*/
- protected function get_source($i18nstring) {
+ public function get_source($i18nstring) {
+ if (!is_object($i18nstring)) {
+ $i18nstring = $this->build_string($i18nstring, NULL);
+ }
// Search the database using lid if we've got it or textgroup, context otherwise
$query = db_select('locales_source', 's')->fields('s');
if (!empty($i18nstring->lid)) {
@@ -210,7 +213,7 @@ class i18n_string_default {
}
// Speed up the query, we just need one row
$source = $query->range(0, 1)->execute()->fetchObject();
- // Update cached map
+ // Update cached map if we've got a context
$this->string_map[$i18nstring->context] = $source ? $source->lid : FALSE;
return $source;
}
@@ -618,10 +621,7 @@ class i18n_string_default {
* String translation for this language
*/
function update_translation($context, $langcode, $translation) {
- $i18nstring = (object) array(
- 'context' => implode(':', $context),
- );
- if ($source = $this->get_source($i18nstring)) {
+ if ($source = $this->get_source($context)) {
$source->language = $langcode;
$source->translation = $translation;
$this->save_translation($source);
@@ -767,72 +767,25 @@ function i18n_string_l10n_client_save_string() {
$textgroup = !empty($_POST['textgroup']) ? $_POST['textgroup'] : 'default';
// Default textgroup will be handled by l10n_client module
if ($textgroup == 'default') {
- l10n_client_save_string();
- }
- elseif (isset($_POST['source']) && isset($_POST['target']) && !empty($_POST['form_token']) && drupal_valid_token($_POST['form_token'], 'l10n_client_form')) {
- $translation = new stdClass();
- $translation->language = $language->language;
- $translation->source = $_POST['source'];
- $translation->translation = $_POST['target'];
- $translation->textgroup = $textgroup;
- i18n_string_save_translation($translation);
+ return l10n_client_save_string();
}
- }
-}
-
-/**
- * Import translation for a given textgroup.
- *
- * @TODO Check string format properly
- *
- * This will update multiple strings if there are duplicated ones
- *
- * @param $translation
- * Srtring object with textgroup, langcode, source, translation, format properties
- * @return
- * The number of strings updated
- */
-function i18n_string_save_translation($translation) {
- include_once 'includes/locale.inc';
-
- $query = db_select('locales_source', 's')
- ->fields('s', array('lid'))
- ->fields('i', array('format'))
- ->condition('s.source', $translation->source)
- ->condition('s.textgroup', $translation->textgroup);
- $query->leftJoin('i18n_string', 'i', 's.lid = i.lid');
- $result = $query->execute()->fetchAll(PDO::FETCH_OBJ);
-
- $count = 0;
- foreach ($result as $source) {
- // If we have a format, check format access. Otherwise do regular check.
- if ($source->format ? filter_access($source->format) : locale_string_is_safe($translation->translation)) {
- $exists = (bool) db_select('locales_target', 'l')
- ->fields('l', array('lid'))
- ->condition('lid', $source->lid)
- ->condition('language', $translation->langcode)
- ->execute()
- ->fetchColumn();
- if (!$exists) {
- // No translation in this language.
- db_insert('locales_target')
- ->fields(array(
- 'lid' => $source->lid,
- 'language' => $translation->langcode,
- 'translation' => $translation->translation,
- ))
- ->execute();
+ elseif (isset($_POST['source']) && isset($_POST['target']) && !empty($_POST['context']) && !empty($_POST['form_token']) && drupal_valid_token($_POST['form_token'], 'l10n_client_form')) {
+ $name = $textgroup . ':' . $_POST['context'];
+ $result = i18n_string_translation_update($name, $_POST['target'], $language->language, $_POST['source']);
+ if ($result) {
+ $message = theme('l10n_client_message', array('message' => t('Translation saved locally for user defined string.'), 'level' => WATCHDOG_INFO));
+ }
+ elseif ($result === FALSE) {
+ $message = theme('l10n_client_message', array('message' => t('Not saved due to insufficient permissions.')));
}
else {
- // Translation exists, overwrite
- db_update('locales_target')
- ->fields(array('translation' => $translation->translation))
- ->condition('language', $translation->langcode)
- ->condition('lid', $source->lid)
- ->execute();
+ $message = theme('l10n_client_message', array('message' => t('Not saved due to source string missing.')));
}
- $count ++;
}
+ else {
+ $message = theme('l10n_client_message', array('message' => t('Not saved due to missing form values.')));
+ }
+ drupal_json_output($message);
+ exit;
}
- return $count;
}
diff --git a/i18n_string/i18n_string.module b/i18n_string/i18n_string.module
index b7391d9..e38e9b2 100644
--- a/i18n_string/i18n_string.module
+++ b/i18n_string/i18n_string.module
@@ -164,6 +164,13 @@ function i18n_string_hook_info() {
/**
* Implements hook_form_FORM_ID_alter()
*/
+function i18n_string_form_l10n_client_form_alter(&$form, &$form_state) {
+ $form['#action'] = url('i18n_string/save');
+}
+
+/**
+ * Implements hook_form_FORM_ID_alter()
+ */
function i18n_string_form_locale_translate_edit_form_alter(&$form, &$form_state) {
// Restrict filter permissions and handle validation and submission for i18n strings
$context = db_select('i18n_string', 'i18ns')
@@ -235,11 +242,8 @@ function i18n_string_translate_edit_form_validate($form, &$form_state) {
$copy_state['values']['textgroup'] = 'default';
locale_translate_edit_form_validate($form, $copy_state);
}
- else {
- $formats = filter_formats();
- if (!filter_access($formats[$context->format])) {
- form_set_error('translations', t('You are not allowed to translate or edit texts with this text format.'));
- }
+ elseif (!i18n_string_translate_access($context)) {
+ form_set_error('translations', t('You are not allowed to translate or edit texts with this text format.'));
}
}
@@ -272,6 +276,14 @@ function i18n_string_locale_translate_import_form_submit($form, &$form_state) {
}
/**
+ * Get source string object.
+ */
+function i18n_string_get_source($name) {
+ list($textgroup, $context) = i18n_string_context($name);
+ return i18n_string_textgroup($textgroup)->get_source($context);
+}
+
+/**
* Check if translation is required for this language code.
*
* Translation is required when default language is different from the given
@@ -847,7 +859,6 @@ function i18n_string_object_translate_page($object_type, $object, $langcode = NU
/**
* Update / create translation for a certain source.
*
- //* Therefore it tries to update the source string,
*
* @param $name
* Array or string concatenated with ':' that contains textgroup and string context
@@ -855,26 +866,57 @@ function i18n_string_object_translate_page($object_type, $object, $langcode = NU
* Translation string for this language code
* @param $langcode
* The language code to translate to a language other than what is used to display the page.
- */
-function i18n_string_translation_update($name, $translation, $langcode) {
+ * @param $source_string
+ * Optional source string, just in case it needs to be created.
+ *
+ * @return mixed
+ * Source string object if update was successful.
+ * Null if source string not found.
+ * FALSE if use doesn't have permission to edit this translation.
+ */
+function i18n_string_translation_update($name, $translation, $langcode, $source_string = NULL) {
if (is_array($translation)) {
return i18n_string_multiple('translation_update', $name, $translation, $langcode);
}
- else {
- list ($textgroup, $context) = i18n_string_context($name);
- $langcode_default = language_default('language');
- // It's the default language so we should update the string source as well.
- if ($langcode == $langcode_default) {
- i18n_string_update($name, $translation);
- }
- elseif ($source = i18n_string_textgroup($textgroup)->update_translation($context, $langcode, $translation)) {
+ elseif ($source = i18n_string_get_source($name)) {
+ if (i18n_string_translation_validate($source, $translation)) {
+ if ($langcode == language_default('language')) {
+ // It's the default language so we should update the string source as well.
+ i18n_string_update($name, $translation);
+ }
+ else {
+ list ($textgroup, $context) = i18n_string_context($name);
+ i18n_string_textgroup($textgroup)->update_translation($context, $langcode, $translation);
+ }
return $source;
}
else {
- // We don't have a source string, so we need to create it with this translation. Ugly but...
- // Both source and translation will be the same string.
- i18n_string_update($name, $translation);
- return i18n_string_textgroup($textgroup)->update_translation($context, $langcode, $translation);
+ // We cannot update this string because of its input format.
+ return FALSE;
}
}
+ elseif ($source_string) {
+ // We don't have a source in the database, so we need to create it, but only if we've got the source too.
+ // Note this string won't have any format.
+ i18n_string_update($name, $source_string);
+ return i18n_string_translation_update($name, $translation, $langcode);
+ }
+ else {
+ return NULL;
+ }
+}
+
+/**
+ * Validate translation and check user access to input format
+ */
+function i18n_string_translation_validate($i18nstring, $translation) {
+ if (!empty($i18nstring->format)) {
+ // If we've got a text format, just need to check user access to it.
+ return i18n_string_translate_access($i18nstring);
+ }
+ else {
+ // If not text format use standard locale validation.
+ // Note: looks like locale.inc is included by locale_init() ?!
+ return locale_string_is_safe($translation);
+ }
} \ No newline at end of file