ResourceLoader Module for serving json based localization messages
ApiULSLocalization is still present since we have to provide live language preview feature. Moved the loading of json file to includes/JsonMessageLoader.php Also moved all RL modules to includes folder. Bug: 56509 Change-Id: Ic39dec1c484982fb07edd167e83794c0b5f470ee
This commit is contained in:
committed by
Niklas Laxström
parent
03dabe1681
commit
76e82e4a9c
@@ -15,6 +15,10 @@ $wgResourceModules['ext.uls.languagenames'] = array(
|
|||||||
'class' => 'ResourceLoaderULSModule'
|
'class' => 'ResourceLoaderULSModule'
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$wgResourceModules['ext.uls.messages'] = array(
|
||||||
|
'class' => 'ResourceLoaderULSJsonMessageModule'
|
||||||
|
);
|
||||||
|
|
||||||
$wgResourceModules['ext.uls.buttons'] = array(
|
$wgResourceModules['ext.uls.buttons'] = array(
|
||||||
'styles' => 'resources/css/ext.uls.buttons.css',
|
'styles' => 'resources/css/ext.uls.buttons.css',
|
||||||
) + $resourcePaths;
|
) + $resourcePaths;
|
||||||
@@ -68,7 +72,7 @@ $wgResourceModules['ext.uls.init'] = array(
|
|||||||
'jquery.json',
|
'jquery.json',
|
||||||
'jquery.cookie',
|
'jquery.cookie',
|
||||||
'jquery.uls',
|
'jquery.uls',
|
||||||
'ext.uls.i18n',
|
'ext.uls.messages',
|
||||||
),
|
),
|
||||||
'position' => 'top',
|
'position' => 'top',
|
||||||
) + $resourcePaths;
|
) + $resourcePaths;
|
||||||
|
|||||||
@@ -152,9 +152,12 @@ $wgExtensionMessagesFiles['UniversalLanguageSelector'] =
|
|||||||
// Register auto load for the page class
|
// Register auto load for the page class
|
||||||
$wgAutoloadClasses += array(
|
$wgAutoloadClasses += array(
|
||||||
'UniversalLanguageSelectorHooks' => __DIR__ . '/UniversalLanguageSelector.hooks.php',
|
'UniversalLanguageSelectorHooks' => __DIR__ . '/UniversalLanguageSelector.hooks.php',
|
||||||
'ResourceLoaderULSModule' => __DIR__ . '/ResourceLoaderULSModule.php',
|
'ResourceLoaderULSModule' => __DIR__ . '/includes/ResourceLoaderULSModule.php',
|
||||||
|
'ResourceLoaderULSJsonMessageModule' =>
|
||||||
|
__DIR__ . '/includes/ResourceLoaderULSJsonMessageModule.php',
|
||||||
'ApiLanguageSearch' => __DIR__ . '/api/ApiLanguageSearch.php',
|
'ApiLanguageSearch' => __DIR__ . '/api/ApiLanguageSearch.php',
|
||||||
'ApiULSLocalization' => __DIR__ . '/api/ApiULSLocalization.php',
|
'ApiULSLocalization' => __DIR__ . '/api/ApiULSLocalization.php',
|
||||||
|
'JsonMessageLoader' => __DIR__ . '/includes/JsonMessageLoader.php',
|
||||||
'LanguageNameSearch' => __DIR__ . '/data/LanguageNameSearch.php',
|
'LanguageNameSearch' => __DIR__ . '/data/LanguageNameSearch.php',
|
||||||
);
|
);
|
||||||
|
|
||||||
|
|||||||
@@ -32,47 +32,12 @@ class ApiULSLocalization extends ApiBase {
|
|||||||
if ( !Language::isValidCode( $language ) ) {
|
if ( !Language::isValidCode( $language ) ) {
|
||||||
$this->dieUsage( 'Invalid language', 'invalidlanguage' );
|
$this->dieUsage( 'Invalid language', 'invalidlanguage' );
|
||||||
}
|
}
|
||||||
|
$contents = JsonMessageLoader::getMessages( $language );
|
||||||
$contents = array();
|
|
||||||
// jQuery.uls localization
|
|
||||||
$contents += $this->loadI18nFile( __DIR__ . '/../lib/jquery.uls/i18n', $language );
|
|
||||||
// mediaWiki.uls localization
|
|
||||||
$contents += $this->loadI18nFile( __DIR__ . '/../i18n', $language );
|
|
||||||
|
|
||||||
// Output the file's contents raw
|
// Output the file's contents raw
|
||||||
$this->getResult()->addValue( null, 'text', json_encode( $contents ) );
|
$this->getResult()->addValue( null, 'text', json_encode( $contents ) );
|
||||||
$this->getResult()->addValue( null, 'mime', 'application/json' );
|
$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() {
|
public function getCustomPrinter() {
|
||||||
return new ApiFormatRaw(
|
return new ApiFormatRaw(
|
||||||
$this->getMain(),
|
$this->getMain(),
|
||||||
|
|||||||
79
includes/JsonMessageLoader.php
Normal file
79
includes/JsonMessageLoader.php
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* Json formatted MessageLoader for ULS
|
||||||
|
*
|
||||||
|
* Copyright (C) 2013 Alolita Sharma, Amir Aharoni, Arun Ganesh, Brandon Harris,
|
||||||
|
* Niklas Laxström, Pau Giner, Santhosh Thottingal, Siebrand Mazeland and other
|
||||||
|
* contributors. See CREDITS for a list.
|
||||||
|
*
|
||||||
|
* UniversalLanguageSelector is dual licensed GPLv2 or later and MIT. You don't
|
||||||
|
* have to do anything special to choose one license or the other and you don't
|
||||||
|
* have to notify anyone which license you are using. You are free to use
|
||||||
|
* UniversalLanguageSelector in commercial projects as long as the copyright
|
||||||
|
* header is left intact. See files GPL-LICENSE and MIT-LICENSE for details.
|
||||||
|
*
|
||||||
|
* @file
|
||||||
|
* @ingroup Extensions
|
||||||
|
* @licence GNU General Public Licence 2.0 or later
|
||||||
|
* @licence MIT License
|
||||||
|
* @since 2013.11
|
||||||
|
*/
|
||||||
|
|
||||||
|
class JsonMessageLoader {
|
||||||
|
/**
|
||||||
|
* Returns all message files that are used to load messages for the given
|
||||||
|
* language.
|
||||||
|
* @param string $language Language code.
|
||||||
|
* @return string[]
|
||||||
|
*/
|
||||||
|
public static function getFilenames( $language ) {
|
||||||
|
$filenames = array();
|
||||||
|
|
||||||
|
$languages = Language::getFallbacksFor( $language );
|
||||||
|
// Prepend the requested language code
|
||||||
|
// to load them all in one loop
|
||||||
|
array_unshift( $languages, $language );
|
||||||
|
|
||||||
|
// jQuery.uls localization
|
||||||
|
foreach ( $languages as $language ) {
|
||||||
|
$filenames[] = __DIR__ . "/../lib/jquery.uls/i18n/$language.json";
|
||||||
|
}
|
||||||
|
|
||||||
|
// mediaWiki.uls localization
|
||||||
|
foreach ( $languages as $language ) {
|
||||||
|
$filenames[] = __DIR__ . "/../i18n/$language.json";
|
||||||
|
}
|
||||||
|
|
||||||
|
$filenames = array_filter( $filenames, function( $filename ) {
|
||||||
|
return file_exists( $filename );
|
||||||
|
} );
|
||||||
|
|
||||||
|
return $filenames;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get messages for the given language.
|
||||||
|
* @param string $language Language code.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function getMessages( $language ) {
|
||||||
|
$contents = array();
|
||||||
|
|
||||||
|
foreach ( self::getFilenames( $language ) as $filename ) {
|
||||||
|
$contents += self::loadI18nFile( $filename );
|
||||||
|
}
|
||||||
|
|
||||||
|
return $contents;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Load messages from a json file.
|
||||||
|
* @param string $filename Directory of the json file.
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
protected static function loadI18nFile( $filename ) {
|
||||||
|
$contents = file_get_contents( $filename );
|
||||||
|
|
||||||
|
return json_decode( $contents, true );
|
||||||
|
}
|
||||||
|
}
|
||||||
63
includes/ResourceLoaderULSJsonMessageModule.php
Normal file
63
includes/ResourceLoaderULSJsonMessageModule.php
Normal file
@@ -0,0 +1,63 @@
|
|||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* ResourceLoaderModule subclass for loading the json
|
||||||
|
* based localization to client-side code.
|
||||||
|
*
|
||||||
|
* @file
|
||||||
|
* @ingroup Extensions
|
||||||
|
* @author Santhosh Thottingal
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Packages a remote schema as a JavaScript ResourceLoader module.
|
||||||
|
* @since 2013.11
|
||||||
|
*/
|
||||||
|
class ResourceLoaderULSJsonMessageModule extends ResourceLoaderModule {
|
||||||
|
/**
|
||||||
|
* Part of the ResourceLoader module interface.
|
||||||
|
* Declares the core ext.uls.i18n module as a dependency.
|
||||||
|
* @return string[] Module names.
|
||||||
|
*/
|
||||||
|
function getDependencies() {
|
||||||
|
return array( 'ext.uls.i18n' );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the last modified timestamp of this module.
|
||||||
|
* The last modified timestamp controls caching.
|
||||||
|
* @param ResourceLoaderContext $context
|
||||||
|
* @return int Unix timestamp.
|
||||||
|
*/
|
||||||
|
function getModifiedTime( ResourceLoaderContext $context ) {
|
||||||
|
$code = $context->getLanguage();
|
||||||
|
if ( !Language::isValidCode( $code ) ) {
|
||||||
|
$code = 'en';
|
||||||
|
}
|
||||||
|
|
||||||
|
$mtimes = array_map(
|
||||||
|
'filemtime',
|
||||||
|
JsonMessageLoader::getFilenames( $code )
|
||||||
|
);
|
||||||
|
// Make sure we have at least one entry
|
||||||
|
$mtimes[] = 1;
|
||||||
|
|
||||||
|
return max( $mtimes );
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the message strings for the current UI language. Uses
|
||||||
|
* mw.uls.loadLocalization to register them on the frontend.
|
||||||
|
* @param ResourceLoaderContext $context
|
||||||
|
* @return string JavaScript code.
|
||||||
|
*/
|
||||||
|
function getScript( ResourceLoaderContext $context ) {
|
||||||
|
$code = $context->getLanguage();
|
||||||
|
if ( !Language::isValidCode( $code ) ) {
|
||||||
|
$code = 'en';
|
||||||
|
}
|
||||||
|
|
||||||
|
$params = array( $code, JsonMessageLoader::getMessages( $code ) );
|
||||||
|
|
||||||
|
return Xml::encodeJsCall( 'mw.uls.loadLocalization', $params );
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -34,10 +34,22 @@
|
|||||||
// ApiULSLocalization handles fallback in ULS
|
// ApiULSLocalization handles fallback in ULS
|
||||||
$.i18n.fallbacks = {};
|
$.i18n.fallbacks = {};
|
||||||
|
|
||||||
mw.uls.loadLocalization = function ( locale ) {
|
/**
|
||||||
|
* Load localization messags for a locale to the jquery.i18n
|
||||||
|
* messagestore.
|
||||||
|
* Also called by RL module ResourceLoaderULSJsonMessageModule
|
||||||
|
* @param {string} locale the language code
|
||||||
|
* @param {Object} [messages]
|
||||||
|
* @return {jQuery.Deferred}
|
||||||
|
*/
|
||||||
|
mw.uls.loadLocalization = function ( locale, messages ) {
|
||||||
var i18n = $.i18n();
|
var i18n = $.i18n();
|
||||||
|
|
||||||
i18n.locale = locale;
|
i18n.locale = locale;
|
||||||
|
if ( messages ) {
|
||||||
|
i18n.load( messages, locale );
|
||||||
|
return $.Deferred().resolve();
|
||||||
|
}
|
||||||
if ( i18n.messageStore.messages[locale] ) {
|
if ( i18n.messageStore.messages[locale] ) {
|
||||||
return $.Deferred().resolve();
|
return $.Deferred().resolve();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user