IcuLanguageBundle.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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\Icu;
  11. use Symfony\Component\Intl\ResourceBundle\LanguageBundle;
  12. use Symfony\Component\Intl\ResourceBundle\Reader\StructuredBundleReaderInterface;
  13. /**
  14. * An ICU-specific implementation of {@link \Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface}.
  15. *
  16. * This class normalizes the data of the ICU .res files to satisfy the contract
  17. * defined in {@link \Symfony\Component\Intl\ResourceBundle\LanguageBundleInterface}.
  18. *
  19. * @author Bernhard Schussek <bschussek@gmail.com>
  20. */
  21. class IcuLanguageBundle extends LanguageBundle
  22. {
  23. public function __construct(StructuredBundleReaderInterface $reader)
  24. {
  25. parent::__construct(realpath(IcuData::getResourceDirectory() . '/lang'), $reader);
  26. }
  27. /**
  28. * {@inheritdoc}
  29. */
  30. public function getLanguageName($lang, $region = null, $locale = null)
  31. {
  32. if ('mul' === $lang) {
  33. return null;
  34. }
  35. return parent::getLanguageName($lang, $region, $locale);
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function getLanguageNames($locale = null)
  41. {
  42. if (null === $locale) {
  43. $locale = \Locale::getDefault();
  44. }
  45. $languages = parent::getLanguageNames($locale);
  46. $collator = new \Collator($locale);
  47. $collator->asort($languages);
  48. // "mul" is the code for multiple languages
  49. unset($languages['mul']);
  50. return $languages;
  51. }
  52. /**
  53. * {@inheritdoc}
  54. */
  55. public function getScriptNames($locale = null)
  56. {
  57. if (null === $locale) {
  58. $locale = \Locale::getDefault();
  59. }
  60. $scripts = parent::getScriptNames($locale);
  61. $collator = new \Collator($locale);
  62. $collator->asort($scripts);
  63. return $scripts;
  64. }
  65. }