You are here

class csl_locale in Bibliography Module 7

Same name and namespace in other branches
  1. 6.2 modules/CiteProc/CSL.inc \csl_locale
  2. 7.2 modules/CiteProc/CSL.inc \csl_locale

Hierarchy

Expanded class hierarchy of csl_locale

File

modules/CiteProc/CSL.inc, line 2050
CiteProc-PHP.

View source
class csl_locale {
  protected $locale_xmlstring = NULL;
  protected $style_locale_xmlstring = NULL;
  protected $locale = NULL;
  protected $style_locale = NULL;
  private $module_path;

  /**
   *
   */
  public function __construct($lang = 'en') {
    $this->module_path = drupal_get_path('module', 'biblio_citeproc');
    $this->locale = new SimpleXMLElement($this
      ->get_locales_file_name($lang));
    if ($this->locale) {
      $this->locale
        ->registerXPathNamespace('cs', 'http://purl.org/net/xbiblio/csl');
    }
  }

  /**
   * SimpleXML objects cannot be serialized, so we must convert to an XML string prior to serialization.
   */
  public function __sleep() {
    $this->locale_xmlstring = $this->locale ? $this->locale
      ->asXML() : '';
    $this->style_locale_xmlstring = $this->style_locale ? $this->style_locale
      ->asXML() : '';
    return array(
      'locale_xmlstring',
      'style_locale_xmlstring',
    );
  }

  /**
   * SimpleXML objects cannot be serialized, so when un-serializing them, they must rebuild from the serialized XML string.
   */
  public function __wakeup() {
    $this->style_locale = !empty($this->style_locale_xmlstring) ? new SimpleXMLElement($this->style_locale_xmlstring) : NULL;
    $this->locale = !empty($this->locale_xmlstring) ? new SimpleXMLElement($this->locale_xmlstring) : NULL;
    if ($this->locale) {
      $this->locale
        ->registerXPathNamespace('cs', 'http://purl.org/net/xbiblio/csl');
    }
  }

  /**
   *
   */
  public function get_locales_file_name($lang) {
    $lang_bases = array(
      "af" => "af-ZA",
      "ar" => "ar-AR",
      "bg" => "bg-BG",
      "ca" => "ca-AD",
      "cs" => "cs-CZ",
      "da" => "da-DK",
      "de" => "de-DE",
      "el" => "el-GR",
      "en" => "en-US",
      "es" => "es-ES",
      "et" => "et-EE",
      "fa" => "fa-IR",
      "fi" => "fi-FI",
      "fr" => "fr-FR",
      "he" => "he-IL",
      "hu" => "hu-HU",
      "is" => "is-IS",
      "it" => "it-IT",
      "ja" => "ja-JP",
      "km" => "km-KH",
      "ko" => "ko-KR",
      "mn" => "mn-MN",
      "nb" => "nb-NO",
      "nl" => "nl-NL",
      "nn" => "nn-NO",
      "pl" => "pl-PL",
      "pt" => "pt-PT",
      "ro" => "ro-RO",
      "ru" => "ru-RU",
      "sk" => "sk-SK",
      "sl" => "sl-SI",
      "sr" => "sr-RS",
      "sv" => "sv-SE",
      "th" => "th-TH",
      "tr" => "tr-TR",
      "uk" => "uk-UA",
      "vi" => "vi-VN",
      "zh" => "zh-CN",
    );
    return isset($lang_bases[$lang]) ? file_get_contents($this->module_path . '/locale/locales-' . $lang_bases[$lang] . '.xml') : file_get_contents($this->module_path . '/locale/locales-en-US.xml');
  }

  /**
   *
   */
  public function get_locale($type, $arg1, $arg2 = NULL, $arg3 = NULL) {
    switch ($type) {
      case 'term':
        $term = '';
        $form = $arg2 ? " and @form='{$arg2}'" : '';
        $plural = $arg3 ? "/cs:{$arg3}" : '';
        if ($arg2 == 'verb' || $arg2 == 'verb-short') {
          $plural = '';
        }
        if ($this->style_locale) {
          $term = @$this->style_locale
            ->xpath("//locale[@xml:lang='en']/terms/term[@name='{$arg1}'{$form}]{$plural}");
          if (!$term) {
            $term = @$this->style_locale
              ->xpath("//locale/terms/term[@name='{$arg1}'{$form}]{$plural}");
          }
        }
        if (!$term) {
          $term = $this->locale
            ->xpath("//cs:term[@name='{$arg1}'{$form}]{$plural}");
        }
        if (isset($term[0])) {
          if (isset($arg3) && isset($term[0]->{$arg3})) {
            return (string) $term[0]->{$arg3};
          }
          if (!isset($arg3) && isset($term[0]->single)) {
            return (string) $term[0]->single;
          }
          return (string) $term[0];
        }
        break;
      case 'date_option':
        $attribs = array();
        if ($this->style_locale) {
          $date_part = $this->style_locale
            ->xpath("//date[@form='{$arg1}']/date-part[@name='{$arg2}']");
        }
        if (!isset($date_part)) {
          $date_part = $this->locale
            ->xpath("//cs:date[@form='{$arg1}']/cs:date-part[@name='{$arg2}']");
        }
        if (isset($date_part)) {
          foreach (${$date_part}
            ->attributes() as $name => $value) {
            $attribs[$name] = (string) $value;
          }
        }
        return $attribs;
        break;
      case 'date_options':
        $options = array();
        if ($this->style_locale) {
          $options = $this->style_locale
            ->xpath("//locale[@xml:lang='en']/date[@form='{$arg1}']");
          if (!$options) {
            $options = $this->style_locale
              ->xpath("//locale/date[@form='{$arg1}']");
          }
        }
        if (!$options) {
          $options = $this->locale
            ->xpath("//cs:date[@form='{$arg1}']");
        }
        if (isset($options[0])) {
          return $options[0];
        }
        break;
      case 'style_option':
        $attribs = array();
        if ($this->style_locale) {
          $option = $this->style_locale
            ->xpath("//locale[@xml:lang='en']/style-options[@{$arg1}]");
          if (!$option) {
            $option = $this->style_locale
              ->xpath("//locale/style-options[@{$arg1}]");
          }
        }
        if (isset($option) && !empty($option)) {
          $attribs = $option[0]
            ->attributes();
        }
        if (empty($attribs)) {
          $option = $this->locale
            ->xpath("//cs:style-options[@{$arg1}]");
        }
        foreach ($option[0]
          ->attributes() as $name => $value) {
          if ($name == $arg1) {
            return (string) $value;
          }
        }
        break;
    }
  }

  /**
   *
   */
  public function set_style_locale($csl_doc) {
    $xml = '';
    $locale_nodes = $csl_doc
      ->getElementsByTagName('locale');
    if ($locale_nodes) {
      $xml_open = '<style-locale>';
      $xml_close = '</style-locale>';
      foreach ($locale_nodes as $key => $locale_node) {
        $xml .= $csl_doc
          ->saveXML($locale_node);
      }
      if (!empty($xml)) {
        $this->style_locale = new SimpleXMLElement($xml_open . $xml . $xml_close);
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
csl_locale::$locale protected property
csl_locale::$locale_xmlstring protected property
csl_locale::$module_path private property
csl_locale::$style_locale protected property
csl_locale::$style_locale_xmlstring protected property
csl_locale::get_locale public function
csl_locale::get_locales_file_name public function
csl_locale::set_style_locale public function
csl_locale::__construct public function
csl_locale::__sleep public function SimpleXML objects cannot be serialized, so we must convert to an XML string prior to serialization.
csl_locale::__wakeup public function SimpleXML objects cannot be serialized, so when un-serializing them, they must rebuild from the serialized XML string.