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:
Santhosh Thottingal
2013-11-04 16:15:07 +05:30
committed by Niklas Laxström
parent 03dabe1681
commit 76e82e4a9c
7 changed files with 165 additions and 39 deletions

View 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 );
}
}

View 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 );
}
}

View File

@@ -0,0 +1,84 @@
<?php
/**
* Resource loader module for UniversalLanguageSelector
*
* Copyright (C) 2012 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
* @author Niklas Laxström
* @ingroup Extensions
* @licence GNU General Public Licence 2.0 or later
* @licence MIT License
*/
/**
* Resource loader module for providing MediaWiki language names.
*/
class ResourceLoaderULSModule extends ResourceLoaderModule {
/**
* @var Language
*/
protected $language;
protected $targets = array( 'desktop', 'mobile' );
/**
* Get all the dynamic data for the content language to an array
*
* @return array
*/
protected function getData() {
$vars = array();
$vars['wgULSLanguages'] = Language::fetchLanguageNames(
$this->language->getCode(), 'mwfile'
);
return $vars;
}
/**
* @param $context ResourceLoaderContext
* @return string: JavaScript code
*/
public function getScript( ResourceLoaderContext $context ) {
$this->language = Language::factory( $context->getLanguage() );
$out = '';
foreach ( $this->getData() as $key => $value ) {
$out .= Xml::encodeJsCall( 'mw.config.set', array( $key, $value ) );
}
return $out;
}
/**
* @param $context ResourceLoaderContext
* @return array|int|Mixed
*/
public function getModifiedTime( ResourceLoaderContext $context ) {
$this->language = Language::factory( $context->getLanguage() );
$cache = wfGetCache( CACHE_ANYTHING );
$key = wfMemcKey( 'resourceloader', 'ulsmodule', 'changeinfo' );
$data = $this->getData();
$hash = md5( serialize( $data ) );
$result = $cache->get( $key );
if ( is_array( $result ) && $result['hash'] === $hash ) {
return $result['timestamp'];
}
$timestamp = wfTimestamp();
$cache->set( $key, array(
'hash' => $hash,
'timestamp' => $timestamp,
) );
return $timestamp;
}
}