LanguageBundle.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Intl\ResourceBundle;
  11. /**
  12. * Default implementation of {@link LanguageBundleInterface}.
  13. *
  14. * @author Bernhard Schussek <bschussek@gmail.com>
  15. */
  16. class LanguageBundle extends AbstractBundle implements LanguageBundleInterface
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getLanguageName($lang, $region = null, $locale = null)
  22. {
  23. if (null === $locale) {
  24. $locale = \Locale::getDefault();
  25. }
  26. if (null === ($languages = $this->readEntry($locale, array('Languages'), true))) {
  27. return null;
  28. }
  29. // Some languages are translated together with their region,
  30. // i.e. "en_GB" is translated as "British English"
  31. if (null !== $region && isset($languages[$lang.'_'.$region])) {
  32. return $languages[$lang.'_'.$region];
  33. }
  34. return $languages[$lang];
  35. }
  36. /**
  37. * {@inheritdoc}
  38. */
  39. public function getLanguageNames($locale = null)
  40. {
  41. if (null === $locale) {
  42. $locale = \Locale::getDefault();
  43. }
  44. if (null === ($languages = $this->readEntry($locale, array('Languages'), true))) {
  45. return array();
  46. }
  47. if ($languages instanceof \Traversable) {
  48. $languages = iterator_to_array($languages);
  49. }
  50. return $languages;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getScriptName($script, $lang = null, $locale = null)
  56. {
  57. if (null === $locale) {
  58. $locale = \Locale::getDefault();
  59. }
  60. $data = $this->read($locale);
  61. // Some languages are translated together with their script,
  62. // e.g. "zh_Hans" is translated as "Simplified Chinese"
  63. if (null !== $lang && isset($data['Languages'][$lang.'_'.$script])) {
  64. $langName = $data['Languages'][$lang.'_'.$script];
  65. // If the script is appended in braces, extract it, e.g. "zh_Hans"
  66. // is translated as "Chinesisch (vereinfacht)" in locale "de"
  67. if (strpos($langName, '(') !== false) {
  68. list($langName, $scriptName) = preg_split('/[\s()]/', $langName, null, PREG_SPLIT_NO_EMPTY);
  69. return $scriptName;
  70. }
  71. }
  72. // "af" (Afrikaans) has no "Scripts" block
  73. if (!isset($data['Scripts'][$script])) {
  74. return null;
  75. }
  76. return $data['Scripts'][$script];
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function getScriptNames($locale = null)
  82. {
  83. if (null === $locale) {
  84. $locale = \Locale::getDefault();
  85. }
  86. if (null === ($scripts = $this->readEntry($locale, array('Scripts'), true))) {
  87. return array();
  88. }
  89. if ($scripts instanceof \Traversable) {
  90. $scripts = iterator_to_array($scripts);
  91. }
  92. return $scripts;
  93. }
  94. }