PageRenderTime 30ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/tiendasikah/core/libs/i18n/i18n.php

http://tiendasikah.googlecode.com/
PHP | 156 lines | 46 code | 10 blank | 100 comment | 4 complexity | f1b50aac4093420ae36776058593ebf0 MD5 | raw file
Possible License(s): GPL-3.0, BSD-3-Clause
  1. <?php
  2. /**
  3. * KumbiaPHP web & app Framework
  4. *
  5. * LICENSE
  6. *
  7. * This source file is subject to the new BSD license that is bundled
  8. * with this package in the file LICENSE.txt.
  9. * It is also available through the world-wide-web at this URL:
  10. * http://wiki.kumbiaphp.com/Licencia
  11. * If you did not receive a copy of the license and are unable to
  12. * obtain it through the world-wide-web, please send an email
  13. * to license@kumbiaphp.com so we can send you a copy immediately.
  14. *
  15. * Implementaci?n para internacionalizaci?n
  16. *
  17. * @category Kumbia
  18. * @package I18n
  19. * @copyright Copyright (c) 2005-2009 Kumbia Team (http://www.kumbiaphp.com)
  20. * @license http://wiki.kumbiaphp.com/Licencia New BSD License
  21. */
  22. /**
  23. * Enlazando al textdomain de la aplicacion
  24. **/
  25. bindtextdomain('default', APP_PATH . 'locale/');
  26. textdomain('default');
  27. /**
  28. * Implementaci?n para internacionalizaci?n
  29. *
  30. * @category Kumbia
  31. * @package I18n
  32. * @copyright Copyright (c) 2005-2009 Kumbia Team (http://www.kumbiaphp.com)
  33. * @license http://wiki.kumbiaphp.com/Licencia New BSD License
  34. */
  35. class I18n
  36. {
  37. /**
  38. * Efectua una traduccion, cuando se pasan argumentos adicionales se remplaza con sprintf
  39. *
  40. * Ejemplo:
  41. * $saludo = I18n::get('Hola %s', 'Emilio')
  42. *
  43. * @param string
  44. * @return string
  45. **/
  46. public static function get($sentence)
  47. {
  48. /**
  49. * Obtengo la traduccion
  50. **/
  51. $sentence = gettext($sentence);
  52. /**
  53. * Si se pasan multiples parametros
  54. **/
  55. if(func_num_args()>1) {
  56. $args = func_get_args();
  57. /**
  58. * Se remplaza con vsprintf
  59. **/
  60. unset($args[0]);
  61. $sentence = vsprintf($sentence, $args);
  62. }
  63. return $sentence;
  64. }
  65. /**
  66. * Obtiene una traduccion al plural, cuando se pasan argumentos adicionales se remplaza con sprintf
  67. *
  68. * @param string $sentence1 mensaje en singular
  69. * @param string $sentence2 mensaje en plural
  70. * @param int $n conteo
  71. * @return string
  72. **/
  73. public static function nget($sentence1, $sentence2, $n)
  74. {
  75. /**
  76. * Obtengo la traduccion
  77. **/
  78. $sentence = ngettext($sentence1, $sentence2, $n);
  79. /**
  80. * Si se pasan multiples parametros
  81. **/
  82. if(func_num_args()>3) {
  83. $args = func_get_args();
  84. /**
  85. * Se remplaza con vsprintf
  86. **/
  87. unset($args[0], $args[1], $args[2]);
  88. $sentence = vsprintf($sentence, $args);
  89. }
  90. return $sentence;
  91. }
  92. /**
  93. * Obtiene una traduccion por categoria, cuando se pasan argumentos adicionales se remplaza con sprintf
  94. *
  95. * @param string $sentence
  96. * @param int $category categoria del mensaje (LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES, LC_ALL)
  97. * @return string
  98. **/
  99. public static function cget($sentence, $category)
  100. {
  101. /**
  102. * Obtengo la traduccion
  103. **/
  104. $sentence = dcgettext(textdomain(null), $sentence, $category);
  105. /**
  106. * Si se pasan multiples parametros
  107. **/
  108. if(func_num_args()>2) {
  109. $args = func_get_args();
  110. /**
  111. * Se remplaza con vsprintf
  112. **/
  113. unset($args[0], $args[1]);
  114. $sentence = vsprintf($sentence, $args);
  115. }
  116. return $sentence;
  117. }
  118. /**
  119. * Obtiene una traduccion al plural por categoria, cuando se pasan argumentos adicionales se remplaza con sprintf
  120. *
  121. * @param string $sentence1 mensaje en singular
  122. * @param string $sentence2 mensaje en plural
  123. * @param int $n conteo
  124. * @param int $category categoria del mensaje (LC_CTYPE, LC_NUMERIC, LC_TIME, LC_COLLATE, LC_MONETARY, LC_MESSAGES, LC_ALL)
  125. * @return string
  126. **/
  127. public function cnget($sentence1, $sentence2, $n, $category)
  128. {
  129. /**
  130. * Obtengo la traduccion en funcion del dominio
  131. **/
  132. $sentence = dcngettext(textdomain(null), $sentence1, $sentence2, $n, $category);
  133. /**
  134. * Si se pasan multiples parametros
  135. **/
  136. if(func_num_args()>4) {
  137. $args = func_get_args();
  138. /**
  139. * Se remplaza con vsprintf
  140. **/
  141. unset($args[0], $args[1], $args[2], $args[3]);
  142. $sentence = vsprintf($sentence, $args);
  143. }
  144. return $sentence;
  145. }
  146. }