You are here

class i18n_string_object_wrapper in Internationalization 7

String object wrapper

Hierarchy

Expanded class hierarchy of i18n_string_object_wrapper

1 string reference to 'i18n_string_object_wrapper'
i18n_string_i18n_object_info_alter in i18n_string/i18n_string.module
Implements hook_i18n_object_info_alter().

File

i18n_string/i18n_string.inc, line 1110
API for internationalization strings

View source
class i18n_string_object_wrapper extends i18n_object_wrapper {

  // Text group object
  protected $textgroup;

  // Properties for translation
  protected $properties;

  /**
   * Get object strings for translation
   *
   * This will return a simple array of string objects, indexed by full string name.
   *
   * @param $options
   *   Array with processing options.
   *   - 'empty', whether to return empty strings, defaults to FALSE.
   */
  public function get_strings($options = array()) {
    $options += array(
      'empty' => FALSE,
    );
    $strings = array();
    foreach ($this
      ->get_properties() as $textgroup => $textgroup_list) {
      foreach ($textgroup_list as $type => $type_list) {
        foreach ($type_list as $object_id => $object_list) {
          foreach ($object_list as $key => $string) {
            if ($options['empty'] || !empty($string['string'])) {

              // Build string object, that will trigger static caches everywhere.
              $i18nstring = i18n_string_textgroup($textgroup)
                ->build_string(array(
                $type,
                $object_id,
                $key,
              ))
                ->set_string($string);
              $strings[$i18nstring
                ->get_name()] = $i18nstring;
            }
          }
        }
      }
    }
    return $strings;
  }

  /**
   * Get object translatable properties
   *
   * This will return a big array indexed by textgroup, object type, object id and string key.
   * Each element is an array with string information, and may have these properties:
   * - 'string', the string itself, will be NULL if the object doesn't have that string
   * - 'format', string format when needed
   * - 'title', string readable name
   */
  public function get_properties() {
    if (!isset($this->properties)) {
      $this->properties = $this
        ->build_properties();

      // Call hook_i18n_string_list_TEXTGROUP_alter(), last chance for modules
      drupal_alter('i18n_string_list_' . $this
        ->get_textgroup(), $this->properties, $this->type, $this->object);
    }
    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', array()) 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);
      if (is_array($value) && isset($value['value'])) {
        $format = isset($value['format']) ? $value['format'] : NULL;
        $value = $value['value'];
      }
      else {
        $format = isset($info['format']) ? $this
          ->get_field($info['format']) : NULL;
      }
      $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' => $format,
        'name' => array_merge($object_keys, array(
          $field,
        )),
      );
    }
    return $strings;
  }

  /**
   * Get string context
   */
  public function get_string_context() {
    return array(
      $this
        ->get_string_info('type'),
      $this
        ->get_key(),
    );
  }

  /**
   * Get translate path for object
   *
   * @param $langcode
   * 	 Language code if we want ti for a specific language
   */
  public function get_translate_path($langcode = NULL) {
    $replacements = array(
      '%i18n_language' => $langcode ? $langcode : '',
    );
    if ($path = $this
      ->get_string_info('translate path')) {
      return $this
        ->path_replace($path, $replacements);
    }
    elseif ($path = $this
      ->get_info('translate tab')) {

      // If we've got a translate tab path, we just add language to it
      return $this
        ->path_replace($path . '/%i18n_language', $replacements);
    }
  }

  /**
   * Translation mode for object
   */
  public function get_translate_mode() {
    return !$this
      ->get_langcode() ? I18N_MODE_LOCALIZE : I18N_MODE_NONE;
  }

  /**
   * Get textgroup name
   */
  public function get_textgroup() {
    return $this
      ->get_string_info('textgroup');
  }

  /**
   * Get textgroup object
   */
  protected function textgroup() {
    if (!isset($this->textgroup)) {
      $this->textgroup = i18n_string_textgroup($this
        ->get_textgroup());
    }
    return $this->textgroup;
  }

  /**
   * Translate object.
   *
   * Translations are cached so it runs only once per language.
   *
   * @return object/array
   *   A clone of the object with its properties translated.
   */
  public function translate($langcode, $options = array()) {

    // We may have it already translated. As objects are statically cached, translations are too.
    if (!isset($this->translations[$langcode])) {
      $this->translations[$langcode] = $this
        ->translate_object($langcode, $options);
    }
    return $this->translations[$langcode];
  }

  /**
   * Translate access (localize strings)
   */
  protected function localize_access() {

    // We could check also whether the object has strings to translate:
    //   && $this->get_strings(array('empty' => TRUE))
    // However it may be better to display the 'No available strings' message
    // for the user to have a clue of what's going on. See i18n_string_translate_page_object()
    return user_access('translate interface') && user_access('translate user-defined strings');
  }

  /**
   * Translate all properties for object.
   *
   * On top of object strings we search for all textgroup:type:objectid:* properties
   *
   * @param $langcode
   *   A clone of the object or array
   */
  protected function translate_object($langcode, $options) {

    // Clone object or array so we don't affect the original one.
    $object = is_object($this->object) ? clone $this->object : $this->object;

    // Get object strings for translatable properties.
    if ($strings = $this
      ->get_strings()) {

      // We preload some of the property translations with a single query.
      if ($context = $this
        ->get_translate_context($langcode, $options)) {
        $found = $this
          ->textgroup()
          ->multiple_translation_search($context, $langcode);
      }

      // Replace all strings in object.
      foreach ($strings as $i18nstring) {
        $this
          ->translate_field($object, $i18nstring, $langcode, $options);
      }
    }
    return $object;
  }

  /**
   * Context to be pre-loaded before translation.
   */
  protected function get_translate_context($langcode, $options) {

    // One-query translation of all textgroup:type:objectid:* properties
    $context = $this
      ->get_string_context();
    $context[] = '*';
    return $context;
  }

  /**
   * Translate object property.
   *
   * Mot often, this is a direct field set, but sometimes fields may have different formats.
   */
  protected function translate_field(&$object, $i18nstring, $langcode, $options) {
    $field_name = $i18nstring->property;
    $translation = $i18nstring
      ->format_translation($langcode, $options);
    if (is_object($object)) {
      $object->{$field_name} = $translation;
    }
    elseif (is_array($object)) {
      $object[$field_name] = $translation;
    }
  }

  /**
   * Remove all strings for this object.
   */
  public function strings_remove($options = array()) {
    $result = array();
    foreach ($this
      ->load_strings() as $key => $string) {
      $result[$key] = $string
        ->remove($options);
    }
    return _i18n_string_result_count($result);
  }

  /**
   * Update all strings for this object.
   */
  public function strings_update($options = array()) {
    $options += array(
      'empty' => TRUE,
      'update' => TRUE,
    );
    $result = array();
    $existing = $this
      ->load_strings();

    // Update object strings
    foreach ($this
      ->get_strings($options) as $key => $string) {
      $result[$key] = $string
        ->update($options);
      unset($existing[$key]);
    }

    // Delete old existing strings.
    foreach ($existing as $key => $string) {
      $result[$key] = $string
        ->remove($options);
    }
    return _i18n_string_result_count($result);
  }

  /**
   * Load all existing strings for this object.
   */
  public function load_strings() {
    list($type, $id) = $this
      ->get_string_context();
    return $this
      ->textgroup()
      ->load_strings(array(
      'type' => $type,
      'objectid' => $id,
    ));
  }

}

Members

Namesort descending Modifiers Type Description Overrides
i18n_object_wrapper::$key protected property
i18n_object_wrapper::$object protected property
i18n_object_wrapper::$translations protected property
i18n_object_wrapper::$type protected property
i18n_object_wrapper::get_edit_path function Get edit path for object 2
i18n_object_wrapper::get_field function Get field value from object/array
i18n_object_wrapper::get_index function Get string numeric key for indexing.
i18n_object_wrapper::get_info public function Get object info
i18n_object_wrapper::get_key function Get key value from object/array
i18n_object_wrapper::get_langcode public function Get language code
i18n_object_wrapper::get_object public function Get real object or array.
i18n_object_wrapper::get_path public function Get link for item 2
i18n_object_wrapper::get_placeholders protected function Get menu placehoders for object
i18n_object_wrapper::get_string_info public function Get object string translation info
i18n_object_wrapper::get_title public function Get title from item 2
i18n_object_wrapper::get_translate_access function Menu access callback for mixed translation tab
i18n_object_wrapper::get_translation_info public function Get object translation set info
i18n_object_wrapper::get_tsid function Get translation set id if any
i18n_object_wrapper::get_type public function Get object type
i18n_object_wrapper::load_object function Load real object or array. 1
i18n_object_wrapper::localize function Localize object if localizable.
i18n_object_wrapper::path_replace protected function Replace path with placeholders
i18n_object_wrapper::set_field function Set field value to object/array
i18n_object_wrapper::set_tsid function Set translation set id
i18n_object_wrapper::translate_access protected function Translate access (translation sets) 2
i18n_object_wrapper::__construct public function Class constructor 2
i18n_string_object_wrapper::$properties protected property
i18n_string_object_wrapper::$textgroup protected property
i18n_string_object_wrapper::build_properties protected function Build properties from object. 4
i18n_string_object_wrapper::get_properties public function Get object translatable properties
i18n_string_object_wrapper::get_strings public function Get object strings for translation
i18n_string_object_wrapper::get_string_context public function Get string context 3
i18n_string_object_wrapper::get_textgroup public function Get textgroup name
i18n_string_object_wrapper::get_translate_context protected function Context to be pre-loaded before translation. 2
i18n_string_object_wrapper::get_translate_mode public function Translation mode for object Overrides i18n_object_wrapper::get_translate_mode 3
i18n_string_object_wrapper::get_translate_path public function Get translate path for object 2
i18n_string_object_wrapper::load_strings public function Load all existing strings for this object.
i18n_string_object_wrapper::localize_access protected function Translate access (localize strings) Overrides i18n_object_wrapper::localize_access
i18n_string_object_wrapper::strings_remove public function Remove all strings for this object.
i18n_string_object_wrapper::strings_update public function Update all strings for this object.
i18n_string_object_wrapper::textgroup protected function Get textgroup object
i18n_string_object_wrapper::translate public function Translate object. Overrides i18n_object_wrapper::translate
i18n_string_object_wrapper::translate_field protected function Translate object property. 2
i18n_string_object_wrapper::translate_object protected function Translate all properties for object.