From 7bf7f8180dbc3a89824815de3bc1e4f40857d2f6 Mon Sep 17 00:00:00 2001 From: Stefan Parviainen Date: Wed, 31 Dec 2014 10:42:08 +0100 Subject: Revert "Revert "Language names via intl library."" This reverts commit 4f35efa0bad4ae6489b63f3eebafe6542d654094. --- library/intl/src/Language/LanguageRepository.php | 96 ++++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 library/intl/src/Language/LanguageRepository.php (limited to 'library/intl/src/Language/LanguageRepository.php') diff --git a/library/intl/src/Language/LanguageRepository.php b/library/intl/src/Language/LanguageRepository.php new file mode 100644 index 000000000..50335cdc0 --- /dev/null +++ b/library/intl/src/Language/LanguageRepository.php @@ -0,0 +1,96 @@ +definitionPath = $definitionPath ? $definitionPath : __DIR__ . '/../../resources/language/'; + } + + /** + * {@inheritdoc} + */ + public function get($languageCode, $locale = null, $fallbackLocale = null) + { + $locale = $this->resolveLocale($locale, $fallbackLocale); + $definitions = $this->loadDefinitions($locale); + if (!isset($definitions[$languageCode])) { + throw new UnknownLanguageException($languageCode); + } + + return $this->createLanguageFromDefinition($definitions[$languageCode], $locale); + } + + /** + * {@inheritdoc} + */ + public function getAll($locale = null, $fallbackLocale = null) + { + $locale = $this->resolveLocale($locale, $fallbackLocale); + $definitions = $this->loadDefinitions($locale); + $languages = array(); + foreach ($definitions as $languageCode => $definition) { + $languages[$languageCode] = $this->createLanguageFromDefinition($definition, $locale); + } + + return $languages; + } + + /** + * Loads the language definitions for the provided locale. + * + * @param string $locale The desired locale. + * + * @return array + */ + protected function loadDefinitions($locale) + { + if (!isset($this->definitions[$locale])) { + $filename = $this->definitionPath . $locale . '.json'; + $this->definitions[$locale] = json_decode(file_get_contents($filename), true); + } + + return $this->definitions[$locale]; + } + + /** + * Creates a language object from the provided definition. + * + * @param array $definition The language definition. + * @param string $locale The locale of the language definition. + * + * @return Language + */ + protected function createLanguageFromDefinition(array $definition, $locale) + { + $language = new Language(); + $language->setLanguageCode($definition['code']); + $language->setName($definition['name']); + $language->setLocale($locale); + + return $language; + } +} -- cgit v1.2.3