You are here

class i18n_string_object in Internationalization 7

String object that contains source and translations.

Note all database operations must go through textgroup object so we can switch storage at some point.

Hierarchy

Expanded class hierarchy of i18n_string_object

1 string reference to 'i18n_string_object'
i18n_string_load_multiple in i18n_string/i18n_string.module
Load multiple strings, including string source

File

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

View source
class i18n_string_object {

  // Updated source string
  public $string;

  // Properties from locale source
  public $lid;
  public $source;
  public $textgroup;
  public $location;
  public $context;
  public $version;

  // Properties from i18n_tring
  public $type;
  public $objectid;
  public $property;
  public $objectkey;
  public $format;

  // Properties from metadata
  public $title;

  // Array of translations to multiple languages
  public $translations = array();

  // Textgroup object
  protected $_textgroup;

  /**
   * Class constructor
   */
  public function __construct($data = NULL) {
    if ($data) {
      $this
        ->set_properties($data);
    }

    // Attempt to re-build the  data from the persistent cache.
    $this
      ->rebuild_from_cache($data);
  }

  /**
   * Rebuild the object data based on the persistent cache.
   *
   * Since the textgroup defines if a string is cacheable or not the caching
   * of the string objects happens in the textgroup handler itself.
   *
   * @see i18n_string_textgroup_cached::__destruct()
   */
  protected function rebuild_from_cache($data = NULL) {

    // Check if we've the required information to repopulate the cache and do so
    // if possible.
    $meta_data_exist = isset($this->textgroup) && isset($this->type) && isset($this->objectid) && isset($this->property);
    if ($meta_data_exist && ($cache = cache_get($this
      ->get_cid())) && !empty($cache->data)) {

      // Re-spawn the cached data.
      // @TODO do we need a array_diff to ensure we don't overwrite the data
      // provided by the $data parameter?
      $this
        ->set_properties($cache->data);
    }
  }

  /**
   * Reset cache, needed for tests.
   */
  public function cache_reset() {
    $this->translations = array();

    // Ensure a possible persistent cache of this object is cleared too.
    cache_clear_all($this
      ->get_cid(), 'cache', TRUE);
  }

  /**
   * Returns the caching id for this object.
   *
   * @return string
   *   The caching id.
   */
  public function get_cid() {
    return 'i18n:string:obj:' . $this
      ->get_name();
  }

  /**
   * Get message parameters from context and string.
   */
  public function get_args() {
    return array(
      '%location' => $this->location,
      '%textgroup' => $this->textgroup,
      '%string' => ($string = $this
        ->get_string()) ? $string : t('[empty string]'),
    );
  }

  /**
   * Set context properties
   */
  public function set_context($context) {
    $parts = is_array($context) ? $context : explode(':', $context);
    $this->context = is_array($context) ? implode(':', $context) : $context;

    // Location will be the full string name
    $this->location = $this->textgroup . ':' . $this->context;
    $this->type = array_shift($parts);
    $this->objectid = $parts ? array_shift($parts) : '';
    $this->objectkey = (int) $this->objectid;

    // Remaining elements glued again with ':'
    $this->property = $parts ? implode(':', $parts) : '';

    // Attempt to re-build the other data from the persistent cache.
    $this
      ->rebuild_from_cache();
    return $this;
  }

  /**
   * Get string name including textgroup and context
   */
  public function get_name() {
    return $this->textgroup . ':' . $this->type . ':' . $this->objectid . ':' . $this->property;
  }

  /**
   * Get source string
   */
  public function get_string() {
    if (isset($this->string)) {
      return $this->string;
    }
    elseif (isset($this->source)) {
      return $this->source;
    }
    elseif ($this
      ->textgroup()->debug) {
      return empty($this->lid) ? t('[Source not found]') : t('[String not found]');
    }
    else {
      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'];
      }
      if (isset($string['title'])) {
        $this->title = $string['title'];
      }
    }
    else {
      $this->string = $string;
    }
    return $this;
  }

  /**
   * Get string title.
   */
  public function get_title() {
    return isset($this->title) ? $this->title : t('String');
  }

  /**
   * Get translation to language from string object
   */
  public function get_translation($langcode) {
    if (!isset($this->translations[$langcode])) {
      $translation = $this
        ->textgroup()
        ->load_translation($this, $langcode);
      if ($translation && isset($translation->translation)) {
        $this
          ->set_translation($translation, $langcode);
      }
      else {

        // No source, no translation
        $this->translations[$langcode] = FALSE;
      }
    }

    // Which doesn't mean we've got a translation, only that we've got the result cached
    return $this->translations[$langcode];
  }

  /**
   * Set translation for language
   *
   * @param $translation
   *   Translation object (from database) or string
   */
  public function set_translation($translation, $langcode = NULL) {
    if (is_object($translation)) {
      $langcode = $langcode ? $langcode : $translation->language;
      $string = isset($translation->translation) ? $translation->translation : FALSE;
      $this
        ->set_properties($translation);
    }
    else {
      $string = $translation;
    }
    $this->translations[$langcode] = $string;
    return $this;
  }

  /**
   * Format the resulting translation or the default string applying callbacks
   *
   * There's a hidden variable, 'i18n_string_debug', that when set to TRUE will display additional info
   */
  public function format_translation($langcode, $options = array()) {
    $options += array(
      'langcode' => $langcode,
      'sanitize' => TRUE,
      'cache' => FALSE,
      'debug' => $this
        ->textgroup()->debug,
    );
    if ($translation = $this
      ->get_translation($langcode)) {
      $string = $translation;
      if (isset($options['filter'])) {
        $string = call_user_func($options['filter'], $string);
      }
    }
    else {

      // Get default source string if no translation.
      $string = $this
        ->get_string();
      $options['sanitize'] = !empty($options['sanitize default']);
    }
    if (!empty($this->format)) {
      $options += array(
        'format' => $this->format,
      );
    }

    // Add debug information if enabled
    if ($options['debug']) {
      $info = array(
        $langcode,
        $this->textgroup,
        $this->context,
      );
      if (!empty($this->format)) {
        $info[] = $this->format;
      }
      $options += array(
        'suffix' => '',
      );
      $options['suffix'] .= ' [' . implode(':', $info) . ']';
    }

    // Finally, apply options, filters, callback, etc...
    return i18n_string_format($string, $options);
  }

  /**
   * Get source string provided a string object.
   *
   * @return
   *   String object if source exists.
   */
  public function get_source() {

    // If already searched and not found we don't have a source,
    if (isset($this->lid) && !$this->lid) {
      return NULL;
    }
    elseif (!isset($this->lid) || !isset($this->source)) {

      // We may have lid from loading a translation but not loaded the source yet.
      if ($source = $this
        ->textgroup()
        ->load_source($this)) {

        // Set properties but don't override existing ones
        $this
          ->set_properties($source, FALSE, FALSE);
        if (!isset($this->string)) {
          $this->string = $source->source;
        }
        return $this;
      }
      else {
        $this->lid = FALSE;
        return NULL;
      }
    }
    else {
      return $this;
    }
  }

  /**
   * Set properties from object or array
   *
   * @param $properties
   *   Obejct or array of properties
   * @param $set_null
   *   Whether to set null properties too
   * @param $override
   *   Whether to set properties that are already set in this object
   */
  public function set_properties($properties, $set_null = TRUE, $override = TRUE) {
    foreach ((array) $properties as $field => $value) {
      if (property_exists($this, $field) && ($set_null || isset($value)) && ($override || !isset($this->{$field}))) {
        $this->{$field} = $value;
      }
    }
    return $this;
  }

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

  /**
   * Update this string.
   */
  public function update($options = array()) {
    return $this
      ->textgroup()
      ->string_update($this, $options);
  }

  /**
   * Delete this string.
   */
  public function remove($options = array()) {
    return $this
      ->textgroup()
      ->string_remove($this, $options);
  }

  /**
   * Check whether there is any problem for the  user to translate a this string.
   *
   * @param $account
   *   Optional user account, defaults to current user.
   *
   * @return
   *   None if the user has access to translate the string.
   *   Error message if the user cannot translate that string.
   */
  public function check_translate_access($account = NULL) {
    return i18n_string_translate_check_string($this, $account);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
i18n_string_object::$context public property
i18n_string_object::$format public property
i18n_string_object::$lid public property
i18n_string_object::$location public property
i18n_string_object::$objectid public property
i18n_string_object::$objectkey public property
i18n_string_object::$property public property
i18n_string_object::$source public property
i18n_string_object::$string public property
i18n_string_object::$textgroup public property
i18n_string_object::$title public property
i18n_string_object::$translations public property
i18n_string_object::$type public property
i18n_string_object::$version public property
i18n_string_object::$_textgroup protected property
i18n_string_object::cache_reset public function Reset cache, needed for tests.
i18n_string_object::check_translate_access public function Check whether there is any problem for the user to translate a this string.
i18n_string_object::format_translation public function Format the resulting translation or the default string applying callbacks
i18n_string_object::get_args public function Get message parameters from context and string.
i18n_string_object::get_cid public function Returns the caching id for this object.
i18n_string_object::get_name public function Get string name including textgroup and context
i18n_string_object::get_source public function Get source string provided a string object.
i18n_string_object::get_string public function Get source string
i18n_string_object::get_title public function Get string title.
i18n_string_object::get_translation public function Get translation to language from string object
i18n_string_object::rebuild_from_cache protected function Rebuild the object data based on the persistent cache.
i18n_string_object::remove public function Delete this string.
i18n_string_object::set_context public function Set context properties
i18n_string_object::set_properties public function Set properties from object or array
i18n_string_object::set_string public function Set source string
i18n_string_object::set_translation public function Set translation for language
i18n_string_object::textgroup protected function Access textgroup object
i18n_string_object::update public function Update this string.
i18n_string_object::__construct public function Class constructor