1: <?php
2:
3: 4: 5: 6: 7:
8:
9: namespace academicpuma\citeproc;
10: use \SimpleXMLElement;
11:
12: 13: 14: 15: 16:
17:
18: class Locale {
19:
20: protected $locale_xmlstring = NULL;
21: protected $style_locale_xmlstring = NULL;
22: protected $locale = NULL;
23: protected $style_locale = NULL;
24:
25:
26: function __construct($lang = 'en') {
27:
28: $this->locale = new SimpleXMLElement($this->get_locales_file_name($lang));
29: if ($this->locale) {
30: $this->locale->registerXPathNamespace('cs', 'http://purl.org/net/xbiblio/csl');
31: }
32: }
33:
34:
35: function __sleep() {
36: $this->locale_xmlstring = ($this->locale) ? $this->locale->asXML() : '';
37: $this->style_locale_xmlstring = ($this->style_locale) ? $this->style_locale->asXML() : '';
38: return array('locale_xmlstring', 'style_locale_xmlstring');
39: }
40:
41:
42: function __wakeup() {
43: $this->style_locale = (!empty($this->style_locale_xmlstring)) ? new SimpleXMLElement($this->style_locale_xmlstring) : NULL;
44: $this->locale = (!empty($this->locale_xmlstring)) ? new SimpleXMLElement($this->locale_xmlstring) : NULL;
45: if ($this->locale) {
46: $this->locale->registerXPathNamespace('cs', 'http://purl.org/net/xbiblio/csl');
47: }
48: }
49:
50: function get_locales_file_name($lang) {
51: $lang_bases = array(
52: "af" => "af-ZA",
53: "ar" => "ar-AR",
54: "bg" => "bg-BG",
55: "ca" => "ca-AD",
56: "cs" => "cs-CZ",
57: "da" => "da-DK",
58: "de" => "de-DE",
59: "el" => "el-GR",
60: "en" => "en-GB",
61: "en" => "en-US",
62: "es" => "es-ES",
63: "et" => "et-EE",
64: "fa" => "fa-IR",
65: "fi" => "fi-FI",
66: "fr" => "fr-FR",
67: "he" => "he-IL",
68: "hu" => "hu-HU",
69: "is" => "is-IS",
70: "it" => "it-IT",
71: "ja" => "ja-JP",
72: "km" => "km-KH",
73: "ko" => "ko-KR",
74: "mn" => "mn-MN",
75: "nb" => "nb-NO",
76: "nl" => "nl-NL",
77: "nn" => "nn-NO",
78: "pl" => "pl-PL",
79: "pt" => "pt-PT",
80: "ro" => "ro-RO",
81: "ru" => "ru-RU",
82: "sk" => "sk-SK",
83: "sl" => "sl-SI",
84: "sr" => "sr-RS",
85: "sv" => "sv-SE",
86: "th" => "th-TH",
87: "tr" => "tr-TR",
88: "uk" => "uk-UA",
89: "vi" => "vi-VN",
90: "zh" => "zh-CN",
91: );
92: return (isset($lang_bases[$lang])) ? file_get_contents('../locale/locales-' . $lang_bases[$lang] . '.xml') : file_get_contents($this->module_path . '/locale/locales-en-US.xml');
93: }
94:
95: function get_locale($type, $arg1, $arg2 = NULL, $arg3 = NULL) {
96: switch ($type) {
97: case 'term':
98: $term = '';
99: $form = $arg2 ? " and @form='$arg2'" : '';
100: $plural = $arg3 ? "/cs:$arg3" : '';
101: if ($this->style_locale) {
102: $term = @$this->style_locale->xpath("//locale[@xml:lang='en']/terms/term[@name='$arg1'$form]$plural");
103: if (!$term) {
104: $term = @$this->style_locale->xpath("//locale/terms/term[@name='$arg1'$form]$plural");
105: }
106: }
107: if (!$term) {
108: $term = $this->locale->xpath("//cs:term[@name='$arg1'$form]$plural");
109: }
110: if (isset($term[0])) {
111: if (isset($arg3) && isset($term[0]->{$arg3}))
112: return (string) $term[0]->{$arg3};
113: if (!isset($arg3) && isset($term[0]->single))
114: return (string) $term[0]->single;
115: return (string) $term[0];
116: }
117: break;
118: case 'date_option':
119: $attribs = array();
120: if ($this->style_locale) {
121: $date_part = $this->style_locale->xpath("//date[@form='$arg1']/date-part[@name='$arg2']");
122: }
123: if (!isset($date_part)) {
124: $date_part = $this->locale->xpath("//cs:date[@form='$arg1']/cs:date-part[@name='$arg2']");
125: }
126: if (isset($date_part)) {
127: foreach ($$date_part->attributes() as $name => $value) {
128: $attribs[$name] = (string) $value;
129: }
130: }
131: return $attribs;
132: break;
133: case 'date_options':
134: $options = array();
135: if ($this->style_locale) {
136: $options = $this->style_locale->xpath("//locale[@xml:lang='en']/date[@form='$arg1']");
137: if (!$options) {
138: $options = $this->style_locale->xpath("//locale/date[@form='$arg1']");
139: }
140: }
141: if (!$options) {
142: $options = $this->locale->xpath("//cs:date[@form='$arg1']");
143: }
144: if (isset($options[0]))
145: return $options[0];
146: break;
147: case 'style_option':
148: $attribs = array();
149: if ($this->style_locale) {
150: $option = $this->style_locale->xpath("//locale[@xml:lang='en']/style-options[@$arg1]");
151: if (!$option) {
152: $option = $this->style_locale->xpath("//locale/style-options[@$arg1]");
153: }
154: }
155: if (isset($option) && !empty($option)) {
156: $attribs = $option[0]->attributes();
157: }
158: if (empty($attribs)) {
159: $option = $this->locale->xpath("//cs:style-options[@$arg1]");
160: }
161: foreach ($option[0]->attributes() as $name => $value) {
162: if ($name == $arg1)
163: return (string) $value;
164: }
165: break;
166: }
167: }
168:
169: public function set_style_locale($csl_doc) {
170: $xml = '';
171: $locale_nodes = $csl_doc->getElementsByTagName('locale');
172: if ($locale_nodes) {
173: $xml_open = '<style-locale>';
174: $xml_close = '</style-locale>';
175: foreach ($locale_nodes as $key => $locale_node) {
176: $xml .= $csl_doc->saveXML($locale_node);
177: }
178: if (!empty($xml)) {
179: $this->style_locale = new SimpleXMLElement($xml_open . $xml . $xml_close);
180: }
181: }
182: }
183:
184: }
185: