LanguageBundle.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  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. use Symfony\Component\Intl\Data\Bundle\Reader\BundleEntryReaderInterface;
  12. use Symfony\Component\Intl\Data\Provider\LanguageDataProvider;
  13. use Symfony\Component\Intl\Data\Provider\LocaleDataProvider;
  14. use Symfony\Component\Intl\Data\Provider\ScriptDataProvider;
  15. use Symfony\Component\Intl\Exception\MissingResourceException;
  16. /**
  17. * Default implementation of {@link LanguageBundleInterface}.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. *
  21. * @internal
  22. */
  23. class LanguageBundle extends LanguageDataProvider implements LanguageBundleInterface
  24. {
  25. /**
  26. * @var LocaleDataProvider
  27. */
  28. private $localeProvider;
  29. /**
  30. * @var ScriptDataProvider
  31. */
  32. private $scriptProvider;
  33. /**
  34. * Creates a new language bundle.
  35. *
  36. * @param string $path
  37. * @param BundleEntryReaderInterface $reader
  38. * @param LocaleDataProvider $localeProvider
  39. * @param ScriptDataProvider $scriptProvider
  40. */
  41. public function __construct($path, BundleEntryReaderInterface $reader, LocaleDataProvider $localeProvider, ScriptDataProvider $scriptProvider)
  42. {
  43. parent::__construct($path, $reader);
  44. $this->localeProvider = $localeProvider;
  45. $this->scriptProvider = $scriptProvider;
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getLanguageName($language, $region = null, $displayLocale = null)
  51. {
  52. // Some languages are translated together with their region,
  53. // i.e. "en_GB" is translated as "British English"
  54. if (null !== $region) {
  55. try {
  56. return $this->getName($language.'_'.$region, $displayLocale);
  57. } catch (MissingResourceException $e) {
  58. }
  59. }
  60. try {
  61. return $this->getName($language, $displayLocale);
  62. } catch (MissingResourceException $e) {
  63. return;
  64. }
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function getLanguageNames($displayLocale = null)
  70. {
  71. try {
  72. return $this->getNames($displayLocale);
  73. } catch (MissingResourceException $e) {
  74. return array();
  75. }
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getScriptName($script, $language = null, $displayLocale = null)
  81. {
  82. try {
  83. return $this->scriptProvider->getName($script, $displayLocale);
  84. } catch (MissingResourceException $e) {
  85. return;
  86. }
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function getScriptNames($displayLocale = null)
  92. {
  93. try {
  94. return $this->scriptProvider->getNames($displayLocale);
  95. } catch (MissingResourceException $e) {
  96. return array();
  97. }
  98. }
  99. /**
  100. * {@inheritdoc}
  101. */
  102. public function getLocales()
  103. {
  104. try {
  105. return $this->localeProvider->getLocales();
  106. } catch (MissingResourceException $e) {
  107. return array();
  108. }
  109. }
  110. }