diff --git a/i18n_block/i18n_block.inc b/i18n_block/i18n_block.inc index a8a5469259f3c76c0c10ef6a185044547a0f7952..664edd0a2b468a43d440c4c4b9b136f5ad0197d9 100644 --- a/i18n_block/i18n_block.inc +++ b/i18n_block/i18n_block.inc @@ -17,13 +17,13 @@ class i18n_block_object extends i18n_string_object_wrapper { /** * Get object strings for translation */ - public function get_properties() { + protected function build_properties() { if ($this->object->module == 'block' && !isset($this->object->body)) { $block = (object) block_custom_block_get($this->object->delta); $this->object->body = $block->body; $this->object->format = $block->format; } - return parent::get_properties(); + return parent::build_properties(); } /** * Translation mode for object diff --git a/i18n_field/i18n_field.inc b/i18n_field/i18n_field.inc index fba38568d492b4886f6336e42e28f692f4dff012..1c6ecca10714c1919b2b6eaebd62fc2419ebe9b5 100644 --- a/i18n_field/i18n_field.inc +++ b/i18n_field/i18n_field.inc @@ -34,8 +34,8 @@ class i18n_field extends i18n_string_object_wrapper { /** * Get translatable properties */ - public function get_properties() { - $properties = parent::get_properties(); + protected function build_properties() { + $properties = parent::build_properties(); $object = $this->object; // For select fields field:field_name if (!empty($object['settings']['allowed_values']) && i18n_field_type_info($object['type'], 'translate_options')) { @@ -49,21 +49,6 @@ class i18n_field extends i18n_string_object_wrapper { } return $properties; } - - /** - * Translate object properties. - * - * @param $object - * Field info array - * @param $options - * Translation options, the only require done is 'langcode' - * @return object or array - * Translated object or array. - */ - protected function translate_properties($field, $options) { - $field = parent::translate_properties($field, $options); - return $field; - } } /** @@ -96,8 +81,8 @@ class i18n_field_instance extends i18n_string_object_wrapper { /** * Get translatable properties */ - public function get_properties() { - $properties = parent::get_properties(); + protected function build_properties() { + $properties = parent::build_properties(); $object = $this->object; $field = field_info_field($object['field_name']); // Only for text field types @@ -109,18 +94,20 @@ class i18n_field_instance extends i18n_string_object_wrapper { return $properties; } - /** - * Translate object properties. + /** + * Set field translation for object. + * + * Mot often, this is a direct field set, but sometimes fields may have different formats. * * @param $object - * Field instance array - * @param $options - * Translation options, the only require done is 'langcode' - * @return object or array - * Translated object or array. + * A clone of the object or array. Field instance. */ - protected function translate_properties($instance, $options) { - $instance = parent::translate_properties($instance, $options); - return $instance; + protected function translate_properties($object, $translations, $options) { + if (isset($translations['default_value'])) { + // Render string without applying format + $object['default_value'][0]['value'] = $translations['default_value']->format_translation($options['langcode'], array('sanitize' => FALSE) + $options); + unset($translations['default_value']); + } + return parent::translate_properties($object, $translations, $options); } } \ No newline at end of file diff --git a/i18n_field/i18n_field.module b/i18n_field/i18n_field.module index cc34eb0c7d773c81b5265c32565ebda7cd952dd5..3dfa1b1d1ffd557c7e7276998c0c2b07681ec755 100644 --- a/i18n_field/i18n_field.module +++ b/i18n_field/i18n_field.module @@ -231,6 +231,7 @@ function i18n_field_field_widget_form($form, $form_state, $field, $instance, $la global $language; // The field language may affect some variables (default) but not others (description will be in current page language) $i18n_langcode = empty($element['#language']) || $element['#language'] == LANGUAGE_NONE ? $language->language : $element['#language']; + $delta = $element['#delta']; // Translate field title if set if (!empty($instance['label']) && !empty($element['#title'])) { @@ -265,7 +266,7 @@ function i18n_field_field_attach_view_alter(&$output, $context) { $element = &$output[$field_name]; if (!empty($element['#entity_type']) && !empty($element['#field_name']) && !empty($element['#bundle'])) { $instance = field_read_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']); - + // Translate field title if set if (!empty($instance['label'])) { $element['#title'] = i18n_field_translate_property($instance, 'label'); @@ -364,9 +365,6 @@ function i18n_field_translate_property($instance, $property, $langcode = NULL) { // For performance reasons, we translate the whole instance once, which is cached. $instance = i18n_string_object_translate('field_instance', $instance, array('langcode' => $langcode)); return $instance[$property]; - /* - return i18n_string_translate(array('field', $instance['field_name'], $instance['bundle'], $property), $instance[$property], array('langcode' => $langcode)); - */ } /** diff --git a/i18n_string/i18n_string.inc b/i18n_string/i18n_string.inc index 847f7d068ccb44b4d680eff73eb1d5462989f3e3..ec16e8a1051b6a6c506f3d1ea4ec600cac0dcd79 100644 --- a/i18n_string/i18n_string.inc +++ b/i18n_string/i18n_string.inc @@ -60,7 +60,8 @@ class i18n_string_object { $this->objectid = $parts ? array_shift($parts) : ''; $this->objectkey = (int)$this->objectid; // Remaining elements glued again with ':' - $this->property = $parts ? implode(':', $parts) : ''; + $this->property = $parts ? implode(':', $parts) : ''; + return $this; } /** * Get source string @@ -76,6 +77,24 @@ class i18n_string_object { return ''; } } + /** + * Set source string + * + * @param $string + * Plain string or array with 'string', 'format', etc... + */ + public function set_string($string) { + if (is_array($string)) { + $this->string = isset($string['string']) ? $string['string'] : NULL; + if (isset($string['format'])) { + $this->format = $string['format']; + } + } + else { + $this->string = $string; + } + return $this; + } /** * Get translation to language from string object */ @@ -109,6 +128,7 @@ class i18n_string_object { $string = $translation; } $this->translations[$langcode] = $string; + return $this; } /** @@ -139,7 +159,8 @@ class i18n_string_object { } $options += array('suffix' => ''); $options['suffix'] .= ' [' . implode(':', $info) . ']'; - } + } + // Finally, apply options, filters, callback, etc... return i18n_string_format($string, $options); } @@ -679,14 +700,14 @@ class i18n_string_textgroup_default { // Remap translations using property field foreach ($strings as $key => $source) { if (isset($translations[$key])) { - $translations[$key]->string = $source; + $translations[$key]->set_string($source); } else { // Not found any string for this property, create it to map in the response // But make sure we set this language's translation to FALSE so we don't search again $newcontext = $context; $newcontext[$property] = $key; - $translations[$key] = $this->build_string($newcontext, $source); + $translations[$key] = $this->build_string($newcontext)->set_string($source); } } return $translations; @@ -775,30 +796,36 @@ class i18n_string_object_wrapper extends i18n_object_wrapper { */ public function get_properties() { if (!isset($this->properties)) { - list($string_type, $object_id) = $this->get_string_context(); - $object_keys = array( - $this->get_textgroup(), - $string_type, - $object_id, - ); - $strings = array(); - foreach ($this->get_string_info('properties') as $field => $info) { - $info = is_array($info) ? $info : array('title' => $info); - $field_name = isset($info['field']) ? $info['field'] : $field; - $value = $this->get_field($field_name); - $strings[$this->get_textgroup()][$string_type][$object_id][$field] = array( - 'string' => is_array($value) || isset($info['empty']) && $value === $info['empty'] ? NULL : $value, - 'title' => $info['title'], - 'format' => isset($info['format']) ? $this->get_field($info['format']) : NULL, - 'name' => array_merge($object_keys, array($field)), - ); - } - // Call hook_i18n_string_list_TEXTGROUP_alter(), last chance for modules - drupal_alter('i18n_string_list_' . $this->get_textgroup(), $strings, $this->type, $this->object); - $this->properties = $strings; + $this->properties = $this->build_properties(); } return $this->properties; } + /** + * Build properties from object. + */ + protected function build_properties() { + list($string_type, $object_id) = $this->get_string_context(); + $object_keys = array( + $this->get_textgroup(), + $string_type, + $object_id, + ); + $strings = array(); + foreach ($this->get_string_info('properties') as $field => $info) { + $info = is_array($info) ? $info : array('title' => $info); + $field_name = isset($info['field']) ? $info['field'] : $field; + $value = $this->get_field($field_name); + $strings[$this->get_textgroup()][$string_type][$object_id][$field] = array( + 'string' => is_array($value) || isset($info['empty']) && $value === $info['empty'] ? NULL : $value, + 'title' => $info['title'], + 'format' => isset($info['format']) ? $this->get_field($info['format']) : NULL, + 'name' => array_merge($object_keys, array($field)), + ); + } + // Call hook_i18n_string_list_TEXTGROUP_alter(), last chance for modules + drupal_alter('i18n_string_list_' . $this->get_textgroup(), $strings, $this->type, $this->object); + return $strings; + } /** * Get object info */ @@ -853,7 +880,7 @@ class i18n_string_object_wrapper extends i18n_object_wrapper { /** * Translate object. * - * Translations are cached so it runs only one per language. + * Translations are cached so it runs only once per language. * * @return object/array * A clone of the object with its properties translated. @@ -862,12 +889,33 @@ class i18n_string_object_wrapper extends i18n_object_wrapper { $options['langcode'] = $langcode = isset($options['langcode']) ? $options['langcode'] : i18n_langcode(); // We may have it already translated. As objects are statically cached, translations are too. if (!isset($this->translations[$langcode])) { + // Clone object or array so we don't affect the original one. $object = is_object($this->object) ? clone $this->object : $this->object; - $this->translations[$langcode] = $this->translate_properties($object, $options); + $this->translations[$langcode] = $this->translate_object($object, $options); } return $this->translations[$langcode]; } + /** + * Set field translation for object. + * + * Mot often, this is a direct field set, but sometimes fields may have different formats. + * + * @param $object + * A clone of the object or array + */ + protected function translate_object($object, $options) { + if ($strings = $this->get_strings()) { + $context = $this->get_string_context(); + $context[] = '*'; + $translations = $this->textgroup()->multiple_translate($context, $strings, $options); + } + else { + $translations = array(); + } + return $this->translate_properties($object, $translations, $options); + } + /** * Translate object properties. * @@ -878,22 +926,16 @@ class i18n_string_object_wrapper extends i18n_object_wrapper { * @return object or array * Translated object or array. */ - protected function translate_properties($object, $options) { - if ($strings = $this->get_strings()) { - $context = $this->get_string_context(); - $context[] = '*'; - $translations = $this->textgroup()->multiple_translate($context, $strings, $options); - // Clone real objects, mark translated... - foreach ($translations as $field => $i18nstring) { - $translated = $i18nstring->format_translation($options['langcode'], $options); - if (is_object($object)) { - $object->$field = $translated; - } - elseif (is_array($object)) { - $object[$field] = $translated; - } + protected function translate_properties($object, $translations, $options) { + foreach ($translations as $field_name => $i18nstring) { + $translation = $i18nstring->format_translation($options['langcode'], $options); + if (is_object($object)) { + $object->$field_name = $translation; + } + elseif (is_array($object)) { + $object[$field_name] = $translation; } } - return $object; + return $object; } }