Move findAvailableLanguages() to the factory

This commit is contained in:
Joas Schilling 2016-01-15 10:04:41 +01:00
parent 3da78c8f1c
commit 6aec550d6e
5 changed files with 68 additions and 21 deletions

View File

@ -452,26 +452,10 @@ class OC_L10N implements \OCP\IL10N {
* find all available languages for an app
* @param string $app App that needs to be translated
* @return array an array of available languages
* @deprecated 9.0.0 Use \OC::$server->getL10NFactory()->findAvailableLanguages() instead
*/
public static function findAvailableLanguages($app=null) {
// also works with null as key
if(isset(self::$availableLanguages[$app]) && !empty(self::$availableLanguages[$app])) {
return self::$availableLanguages[$app];
}
$available=array('en');//english is always available
$dir = self::findI18nDir($app);
if(is_dir($dir)) {
$files=scandir($dir);
foreach($files as $file) {
if(substr($file, -5, 5) === '.json' && substr($file, 0, 4) !== 'l10n') {
$i = substr($file, 0, -5);
$available[] = $i;
}
}
}
self::$availableLanguages[$app] = $available;
return $available;
return \OC::$server->getL10NFactory()->findAvailableLanguages($app);
}
/**

View File

@ -33,8 +33,11 @@ use OCP\L10N\IFactory;
class Factory implements IFactory {
/**
* cached instances
* @var array Structure: Lang => App => \OCP\IL10N
*/
protected $instances = array();
protected $instances = [];
protected $availableLanguages = [];
/**
* Get a language instance
@ -56,4 +59,55 @@ class Factory implements IFactory {
return $this->instances[$key][$app];
}
/**
* Find all available languages for an app
*
* @param string|null $app App id or null for core
* @return array an array of available languages
*/
public function findAvailableLanguages($app = null) {
$key = $app;
if ($key === null) {
$key = 'null';
}
// also works with null as key
if (!empty($this->availableLanguages[$key])) {
return $this->availableLanguages[$key];
}
$available = ['en']; //english is always available
$dir = $this->findL10nDir($app);
if (is_dir($dir)) {
$files = scandir($dir);
if ($files !== false) {
foreach ($files as $file) {
if (substr($file, -5) === '.json' && substr($file, 0, 4) !== 'l10n') {
$available[] = substr($file, 0, -5);
}
}
}
}
$this->availableLanguages[$key] = $available;
return $available;
}
/**
* find the l10n directory
*
* @param string $app App id or empty string for core
* @return string directory
*/
protected function findL10nDir($app = '') {
if ($app !== '') {
// Check if the app is in the app folder
if (file_exists(\OC_App::getAppPath($app) . '/l10n/')) {
return \OC_App::getAppPath($app) . '/l10n/';
} else {
return \OC::$SERVERROOT . '/' . $app . '/l10n/';
}
}
return \OC::$SERVERROOT.'/core/l10n/';
}
}

View File

@ -33,4 +33,13 @@ interface IFactory {
* @since 8.2.0
*/
public function get($app, $lang = null);
/**
* Find all available languages for an app
*
* @param string|null $app App id or null for core
* @return string[] an array of available languages
* @since 9.0.0
*/
public function findAvailableLanguages($app = null);
}

View File

@ -31,7 +31,7 @@ OCP\JSON::callCheck();
// Get data
if( isset( $_POST['lang'] ) ) {
$languageCodes=OC_L10N::findAvailableLanguages();
$languageCodes = \OC::$server->getL10NFactory()->findAvailableLanguages();
$lang = (string)$_POST['lang'];
if(array_search($lang, $languageCodes) or $lang === 'en') {
\OC::$server->getConfig()->setUserValue( OC_User::getUser(), 'core', 'lang', $lang );

View File

@ -63,7 +63,7 @@ $user = OC::$server->getUserManager()->get(OC_User::getUser());
$email = $user->getEMailAddress();
$userLang=$config->getUserValue( OC_User::getUser(), 'core', 'lang', OC_L10N::findLanguage() );
$languageCodes=OC_L10N::findAvailableLanguages();
$languageCodes = \OC::$server->getL10NFactory()->findAvailableLanguages();
// array of common languages
$commonLangCodes = array(