/library/Helper/I18n.php

https://github.com/cahnory/Cahnory-Framework · PHP · 161 lines · 117 code · 13 blank · 31 comment · 20 complexity · 20a335b646836024b4f4083ab2d4f8d3 MD5 · raw file

  1. <?php
  2. /**
  3. * LICENSE: Copyright (c) 2010 François 'cahnory' Germain
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. * that is available through the world-wide-web at the following URI:
  22. * http://www.php.net/license/3_01.txt. If you did not receive a copy of
  23. * the PHP License and are unable to obtain it through the web, please
  24. * send a note to license@php.net so we can mail you a copy immediately.
  25. *
  26. * @package Cahnory
  27. * @subpackage Library
  28. * @category Helper
  29. * @author François 'cahnory' Germain <cahnory@gmail.com>
  30. * @copyright 2010 François Germain
  31. * @license http://www.opensource.org/licenses/mit-license.php
  32. */
  33. class Helper_I18n
  34. {
  35. private $system;
  36. private $_parent;
  37. private $_name;
  38. private $_languages = array();
  39. private $_dictionary = array();
  40. private $_nameSpaces = array();
  41. public function __construct($system)
  42. {
  43. $this->system = $system;
  44. $this->system->load->help('I18n', array($this, 'load'));
  45. }
  46. public function __get($name)
  47. {
  48. if(!isset($this->_nameSpaces[$name])) {
  49. $this->_nameSpaces[$name] = new Helper_I18n($this->system);
  50. $this->_nameSpaces[$name]->_parent = $this;
  51. $this->_nameSpaces[$name]->_name = $name;
  52. $this->_nameSpaces[$name]->_languages =& $this->_languages;
  53. }
  54. return $this->_nameSpaces[$name];
  55. }
  56. public function __toString()
  57. {
  58. if($this->_parent !== NULL) {
  59. if(($name = $this->_parent->get($this->_name)) !== NULL) {
  60. return $name;
  61. }
  62. }
  63. return $this->_name;
  64. }
  65. public function main($language)
  66. {
  67. $languages = func_get_args();
  68. $i = 0;
  69. foreach($languages as $language) {
  70. if(($language = $this->_getLanguage($language)) !== NULL) {
  71. array_splice($this->_languages, array_search($language, $this->_languages), 1);
  72. array_splice($this->_languages, $i, 0, $language);
  73. $i++;
  74. }
  75. }
  76. return $i > 0;
  77. }
  78. public function set($language, $name, $value)
  79. {
  80. if(($language = $this->_getLanguage($language)) !== false) {
  81. if(!isset($this->_dictionary[$language])) {
  82. $this->_dictionary[$language] = array();
  83. }
  84. $this->_dictionary[$language][$name] = $value;
  85. return true;
  86. }
  87. return false;
  88. }
  89. public function get($name, $language = NULL)
  90. {
  91. if($language !== NULL) {
  92. if(($language = $this->_getLanguage($language)) !== false) {
  93. if(isset($this->_dictionary[$language]) && array_key_exists($name, $this->_dictionary[$language])) {
  94. return $this->_dictionary[$language][$name];
  95. }
  96. }
  97. } else {
  98. foreach($this->_languages as $language) {
  99. if(isset($this->_dictionary[$language]) && array_key_exists($name, $this->_dictionary[$language])) {
  100. return $this->_dictionary[$language][$name];
  101. }
  102. }
  103. }
  104. }
  105. public function load($name)
  106. {
  107. $files = $this->getFile($name);
  108. if(is_array($files)) {
  109. foreach($files as $file) {
  110. include $file;
  111. }
  112. } elseif($files !== NULL) {
  113. include $files;
  114. } else {
  115. return false;
  116. }
  117. return true;
  118. }
  119. public function getFile($filename)
  120. {
  121. $filename .= '.php';
  122. $files = array();
  123. if($modified = $this->system->module->modifyViewFilename('I18N/'.$filename)) {
  124. $files = array_merge($files, $modified);
  125. }
  126. if($modified = $this->system->widget->modifyViewFilename('I18N/'.$filename)) {
  127. $files = array_merge($files, $modified);
  128. }
  129. $files[] = 'I18n/'.$filename;
  130. $files = $this->system->load->getFile($files);
  131. return $files;
  132. }
  133. private function _getLanguage($language)
  134. {
  135. $language = strtolower($language);
  136. if(!in_array($language, $this->_languages)) {
  137. if($language === NULL) {
  138. $language = sizeof($this->_languages)
  139. ? $this->_languages[0]
  140. : false;
  141. } else {
  142. $this->_languages[] = $language;
  143. }
  144. }
  145. return $language;
  146. }
  147. }
  148. ?>