You are here

boxes_i18n.inc in Boxes translation 6

Same filename and directory in other branches
  1. 7 plugins/boxes/boxes_i18n.inc

File

plugins/boxes/boxes_i18n.inc
View source
<?php

/**
 * Simple translatable custom text box.
 */
class boxes_i18n extends boxes_simple {

  /**
   * Implementation of boxes_content::options_defaults().
   */
  public function options_defaults() {
    $defaults = parent::options_defaults();
    return $defaults + array(
      'i18n_boxes' => array(
        'language' => I18N_BOXES_LOCALIZE,
      ),
    );
  }

  /**
   * Implementation of boxes_content::options_form().
   */
  public function options_form() {
    $form = parent::options_form();
    $form['i18n_boxes'] = array(
      '#type' => 'fieldset',
      '#title' => t('Multilingual settings'),
      '#collapsible' => TRUE,
      '#weight' => 0,
      '#tree' => TRUE,
    );
    $options = array(
      '' => t('All languages'),
    );
    $options[I18N_BOXES_LOCALIZE] = t('All languages (Translatable)');
    $options += locale_language_list('name');
    $form['i18n_boxes']['language'] = array(
      '#type' => 'radios',
      '#title' => t('Language'),
      '#default_value' => $this->options['i18n_boxes']['language'],
      '#options' => $options,
    );
    return $form;
  }

  /**
   * Save a box.
   */
  public function save() {
    parent::save();

    // Make the box strings available to be translated.
    $this
      ->locale_refresh();
  }

  /**
   * Refresh the translatable strings.
   */
  public function locale_refresh() {
    $title = '';
    $content = '';
    if ($this->options['i18n_boxes']['language'] == I18N_BOXES_LOCALIZE) {
      $content = $this->options['body'];
      $title = $this->title;
    }

    // Update or delete the i18nstrings record for the body.
    i18nstrings_update("boxes:{$this->plugin_key}:{$this->delta}:body", $content, $this->options['format']);

    // Update or delete the title, too.
    i18nstrings_update("boxes:{$this->plugin_key}:{$this->delta}:title", $title);
  }

  /**
   * Implementation of boxes_content::render().
   */
  public function render() {
    global $language;

    // Get the filtered rendered data from boxes_simple.
    $block = parent::render();

    // Handle any multilingual settings.
    if (!empty($this->options['i18n_boxes']['language'])) {
      if ($this->options['i18n_boxes']['language'] == I18N_BOXES_LOCALIZE) {

        // This box is available to translate.
        $block['content'] = i18nstrings_text("boxes:{$this->plugin_key}:{$this->delta}:body", $block['content']);

        // Also translate the title.
        $block['subject'] = i18nstrings("boxes:{$this->plugin_key}:{$this->delta}:title", $block['subject']);
        $block['title'] = $block['subject'];
      }
      elseif ($this->options['i18n_boxes']['language'] != $language->language) {

        // This box does not share the language of the user, so do not display.
        $block = NULL;
      }
    }
    return $block;
  }

}

Classes

Namesort descending Description
boxes_i18n Simple translatable custom text box.