PageRenderTime 26ms CodeModel.GetById 20ms RepoModel.GetById 1ms app.codeStats 0ms

/library/Unodor/Translate/Adapter.php

https://github.com/besters/My-Base
PHP | 131 lines | 92 code | 19 blank | 20 comment | 30 complexity | 145c09ac6ec2450665bad9e654bf3b45 MD5 | raw file
  1. <?php
  2. abstract class Unodor_Translate_Adapter extends Zend_Translate_Adapter {
  3. /**
  4. * Translates the given string
  5. * returns the translation
  6. *
  7. * @see Zend_Locale
  8. * @param string|array $messageId Translation string, or Array for plural translations
  9. * @param string|Zend_Locale $locale (optional) Locale/Language to use, identical with
  10. * locale identifier, @see Zend_Locale for more information
  11. * @return string
  12. */
  13. public function translate($messageId, $locale = null)
  14. {
  15. if ($locale === null) {
  16. $locale = $this->_options['locale'];
  17. }
  18. $plural = null;
  19. if (is_array($messageId)) {
  20. if (count($messageId) > 2) {
  21. $number = array_pop($messageId);
  22. if (!is_numeric($number)) {
  23. $plocale = $number;
  24. $number = array_pop($messageId);
  25. } else {
  26. $plocale = 'en';
  27. }
  28. $plural = $messageId;
  29. $messageId = $messageId[0];
  30. } else {
  31. $messageId = $messageId[0];
  32. }
  33. }
  34. if (!Zend_Locale::isLocale($locale, true, false)) {
  35. if (!Zend_Locale::isLocale($locale, false, false)) {
  36. // language does not exist, return original string
  37. $this->_log($messageId, $locale);
  38. if ($plural === null) {
  39. return $messageId;
  40. }
  41. $rule = Zend_Translate_Plural::getPlural($number, $plocale);
  42. if (!isset($plural[$rule])) {
  43. $rule = 0;
  44. }
  45. return $plural[$rule];
  46. }
  47. $locale = new Zend_Locale($locale);
  48. }
  49. $locale = (string) $locale;
  50. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  51. // return original translation
  52. if ($plural === null) {
  53. return $this->_translate[$locale][$messageId];
  54. }
  55. $rule = Zend_Translate_Plural::getPlural($number, $locale);
  56. if (isset($this->_translate[$locale][$plural[0]][$rule])) {
  57. return $this->_translate[$locale][$plural[0]][$rule];
  58. }
  59. } else if (strlen($locale) != 2) {
  60. // faster than creating a new locale and separate the leading part
  61. $locale = substr($locale, 0, -strlen(strrchr($locale, '_')));
  62. if ((is_string($messageId) || is_int($messageId)) && isset($this->_translate[$locale][$messageId])) {
  63. // return regionless translation (en_US -> en)
  64. if ($plural === null) {
  65. return $this->_translate[$locale][$messageId];
  66. }
  67. $rule = Zend_Translate_Plural::getPlural($number, $locale);
  68. if (isset($this->_translate[$locale][$plural[0]][$rule])) {
  69. return $this->_translate[$locale][$plural[0]][$rule];
  70. }
  71. }
  72. }
  73. if(isset($plural)){
  74. $messageId = $plural;
  75. }
  76. $this->_log($messageId, $locale);
  77. if ($plural === null) {
  78. return $messageId;
  79. }
  80. $rule = Zend_Translate_Plural::getPlural($number, $plocale);
  81. if (!isset($plural[$rule])) {
  82. $rule = 0;
  83. }
  84. return $plural[$rule];
  85. }
  86. /**
  87. * Logs a message when the log option is set
  88. *
  89. * @param string $message Message to log
  90. * @param String $locale Locale to log
  91. */
  92. protected function _log($message, $locale) {
  93. if ($this->_options['logUntranslated']) {
  94. $string = '';
  95. if(is_array($message)){
  96. foreach($message as $str){
  97. $string .= '"'.$str.'", ';
  98. }
  99. $string .= 1;
  100. $message = str_replace('%message%', $string, 'n'.$this->_options['logMessage']);
  101. }else{
  102. $message = str_replace('%message%', '"'.$message.'"', $this->_options['logMessage']);
  103. }
  104. $message = str_replace('%locale%', $locale, $message);
  105. if ($this->_options['log']) {
  106. $this->_options['log']->notice($message);
  107. } else {
  108. trigger_error($message, E_USER_NOTICE);
  109. }
  110. }
  111. }
  112. }