From cd32d36911b6f6499baf4b6c231973d6df248f7a Mon Sep 17 00:00:00 2001 From: "Amir E. Aharoni" Date: Fri, 5 Jul 2013 08:15:36 +0300 Subject: [PATCH] Load messages from a fallback chain Initial commit was simplified by Niklas. Change-Id: Ic45766ebeca0104de0f26441099de4e04064325d --- api/ApiULSLocalization.php | 39 ++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/api/ApiULSLocalization.php b/api/ApiULSLocalization.php index 0e45c3dd..ac3ec067 100644 --- a/api/ApiULSLocalization.php +++ b/api/ApiULSLocalization.php @@ -35,19 +35,42 @@ class ApiULSLocalization extends ApiBase { $contents = array(); // jQuery.uls localization - $filename = __DIR__ . "/../lib/jquery.uls/i18n/$language.json"; - if ( file_exists( $filename ) ) { - $contents += json_decode( file_get_contents( $filename ), true ); - } + $contents += $this->loadI18nFile( __DIR__ . '/../lib/jquery.uls/i18n', $language ); // mediaWiki.uls localization - $filename = __DIR__ . "/../i18n/$language.json"; - if ( file_exists( $filename ) ) { - $contents += json_decode( file_get_contents( $filename ), true ); - } + $contents += $this->loadI18nFile( __DIR__ . '/../i18n', $language ); + // Output the file's contents raw $this->getResult()->addValue( null, 'text', json_encode( $contents ) ); $this->getResult()->addValue( null, 'mime', 'application/json' ); + } + /** + * Load messages from the jquery.i18n json file and from + * fallback languages. + * @param string $dir Directory of the json file. + * @param string $language Language code. + * @return array + */ + protected function loadI18nFile( $dir, $language ) { + $languages = Language::getFallbacksFor( $language ); + // Prepend the requested language code + // to load them all in one loop + array_unshift( $languages, $language ); + $messages = array(); + + foreach ( $languages as $language ) { + $filename = "$dir/$language.json"; + + if ( !file_exists( $filename ) ) { + continue; + } + + $contents = file_get_contents( $filename ); + $messagesForLanguage = json_decode( $contents, true ); + $messages = array_merge( $messagesForLanguage, $messages ); + } + + return $messages; } public function getCustomPrinter() {