summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJoas Schilling <nickvergessen@owncloud.com>2016-01-15 12:37:34 +0100
committerJoas Schilling <nickvergessen@owncloud.com>2016-01-26 14:02:31 +0100
commit6652a0fb6cee6b5494dfca56c561c3ba6ae03185 (patch)
treef7ed856c020233f0ede29fdb25964fa693574c5d
parent7ec7464ee249f15d377a44c1b8b23e127e07385e (diff)
downloadnextcloud-server-6652a0fb6cee6b5494dfca56c561c3ba6ae03185.tar.gz
nextcloud-server-6652a0fb6cee6b5494dfca56c561c3ba6ae03185.zip
Move finding the language files and creating the function out of the object
-rw-r--r--lib/private/l10n.php105
-rw-r--r--lib/private/l10n/factory.php112
-rw-r--r--lib/public/l10n/ifactory.php10
3 files changed, 129 insertions, 98 deletions
diff --git a/lib/private/l10n.php b/lib/private/l10n.php
index 4405491cb78..5a704916f40 100644
--- a/lib/private/l10n.php
+++ b/lib/private/l10n.php
@@ -102,10 +102,9 @@ class OC_L10N implements \OCP\IL10N {
/**
* @param $transFile
- * @param bool $mergeTranslations
* @return bool
*/
- public function load($transFile, $mergeTranslations = false) {
+ public function load($transFile) {
$this->app = true;
$json = json_decode(file_get_contents($transFile), true);
@@ -118,11 +117,7 @@ class OC_L10N implements \OCP\IL10N {
$this->pluralFormString = $json['pluralForm'];
$translations = $json['translations'];
- if ($mergeTranslations) {
- $this->translations = array_merge($this->translations, $translations);
- } else {
- $this->translations = $translations;
- }
+ $this->translations = array_merge($this->translations, $translations);
return true;
}
@@ -135,93 +130,13 @@ class OC_L10N implements \OCP\IL10N {
$lang = $this->lang;
$this->app = true;
- // Use cache if possible
- if(array_key_exists($app.'::'.$lang, self::$cache)) {
- $this->translations = self::$cache[$app.'::'.$lang]['t'];
- } else{
- $i18nDir = $this->findI18nDir($app);
- $transFile = strip_tags($i18nDir).strip_tags($lang).'.json';
- // Texts are in $i18ndir
- // (Just no need to define date/time format etc. twice)
- if((OC_Helper::isSubDirectory($transFile, OC::$SERVERROOT.'/core/l10n/')
- || OC_Helper::isSubDirectory($transFile, OC::$SERVERROOT.'/lib/l10n/')
- || OC_Helper::isSubDirectory($transFile, OC::$SERVERROOT.'/settings')
- || OC_Helper::isSubDirectory($transFile, OC_App::getAppPath($app).'/l10n/')
- )
- && file_exists($transFile)) {
- // load the translations file
- if($this->load($transFile)) {
- //merge with translations from theme
- $theme = \OC::$server->getConfig()->getSystemValue('theme');
- if (!empty($theme)) {
- $transFile = OC::$SERVERROOT.'/themes/'.$theme.substr($transFile, strlen(OC::$SERVERROOT));
- if (file_exists($transFile)) {
- $this->load($transFile, true);
- }
- }
- }
- }
-
- self::$cache[$app.'::'.$lang]['t'] = $this->translations;
- }
- }
-
- /**
- * Creates a function that The constructor
- *
- * If language is not set, the constructor tries to find the right
- * language.
- *
- * Parts of the code is copied from Habari:
- * https://github.com/habari/system/blob/master/classes/locale.php
- * @param string $string
- * @return string
- */
- protected function createPluralFormFunction($string){
- if(preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) {
- // sanitize
- $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] );
- $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] );
-
- $body = str_replace(
- array( 'plural', 'n', '$n$plurals', ),
- array( '$plural', '$n', '$nplurals', ),
- 'nplurals='. $nplurals . '; plural=' . $plural
- );
-
- // add parents
- // important since PHP's ternary evaluates from left to right
- $body .= ';';
- $res = '';
- $p = 0;
- for($i = 0; $i < strlen($body); $i++) {
- $ch = $body[$i];
- switch ( $ch ) {
- case '?':
- $res .= ' ? (';
- $p++;
- break;
- case ':':
- $res .= ') : (';
- break;
- case ';':
- $res .= str_repeat( ')', $p ) . ';';
- $p = 0;
- break;
- default:
- $res .= $ch;
- }
- }
+ /** @var \OC\L10N\Factory $factory */
+ $factory = \OC::$server->getL10NFactory();
+ $languageFiles = $factory->getL10nFilesForApp($app, $lang);
- $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);';
- return create_function('$n', $body);
- }
- else {
- // default: one plural form for all cases but n==1 (english)
- return create_function(
- '$n',
- '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);'
- );
+ $this->translations = [];
+ foreach ($languageFiles as $languageFile) {
+ $this->load($languageFile);
}
}
@@ -286,8 +201,8 @@ class OC_L10N implements \OCP\IL10N {
*/
public function getPluralFormFunction() {
$this->init();
- if(is_null($this->pluralFormFunction)) {
- $this->pluralFormFunction = $this->createPluralFormFunction($this->pluralFormString);
+ if (is_null($this->pluralFormFunction)) {
+ $this->pluralFormFunction = \OC::$server->getL10NFactory()->createPluralFunction($this->pluralFormString);
}
return $this->pluralFormFunction;
}
diff --git a/lib/private/l10n/factory.php b/lib/private/l10n/factory.php
index 39d69c81935..c564de685f2 100644
--- a/lib/private/l10n/factory.php
+++ b/lib/private/l10n/factory.php
@@ -48,6 +48,11 @@ class Factory implements IFactory {
*/
protected $availableLanguages = [];
+ /**
+ * @var array Structure: string => callable
+ */
+ protected $pluralFunctions = [];
+
/** @var IConfig */
protected $config;
@@ -216,15 +221,52 @@ class Factory implements IFactory {
}
/**
+ * Get a list of language files that should be loaded
+ *
+ * @param string $app
+ * @param string $lang
+ * @return string[]
+ */
+ // FIXME This method is only public, until OC_L10N does not need it anymore,
+ // FIXME This is also the reason, why it is not in the public interface
+ public function getL10nFilesForApp($app, $lang) {
+ $languageFiles = [];
+
+ $i18nDir = $this->findL10nDir($app);
+ $transFile = strip_tags($i18nDir) . strip_tags($lang) . '.json';
+
+ if((\OC_Helper::isSubDirectory($transFile, \OC::$SERVERROOT . '/core/l10n/')
+ || \OC_Helper::isSubDirectory($transFile, \OC::$SERVERROOT . '/lib/l10n/')
+ || \OC_Helper::isSubDirectory($transFile, \OC::$SERVERROOT . '/settings/l10n/')
+ || \OC_Helper::isSubDirectory($transFile, \OC_App::getAppPath($app) . '/l10n/')
+ )
+ && file_exists($transFile)) {
+ // load the translations file
+ $languageFiles[] = $transFile;
+
+ // merge with translations from theme
+ $theme = $this->config->getSystemValue('theme');
+ if (!empty($theme)) {
+ $transFile = \OC::$SERVERROOT . '/themes/' . $theme . substr($transFile, strlen(\OC::$SERVERROOT));
+ if (file_exists($transFile)) {
+ $languageFiles[] = $transFile;
+ }
+ }
+ }
+
+ return $languageFiles;
+ }
+
+ /**
* find the l10n directory
*
* @param string $app App id or empty string for core
* @return string directory
*/
- protected function findL10nDir($app = '') {
- if ($app !== '') {
+ protected function findL10nDir($app = null) {
+ if ($app) {
// Check if the app is in the app folder
- if (file_exists(\OC_App::getAppPath($app) . '/l10n/')) {
+ if (\OC_App::getAppPath($app) && file_exists(\OC_App::getAppPath($app) . '/l10n/')) {
return \OC_App::getAppPath($app) . '/l10n/';
} else {
return \OC::$SERVERROOT . '/' . $app . '/l10n/';
@@ -232,4 +274,68 @@ class Factory implements IFactory {
}
return \OC::$SERVERROOT.'/core/l10n/';
}
+
+
+ /**
+ * Creates a function from the plural string
+ *
+ * Parts of the code is copied from Habari:
+ * https://github.com/habari/system/blob/master/classes/locale.php
+ * @param string $string
+ * @return string
+ */
+ public function createPluralFunction($string) {
+ if (isset($this->pluralFunctions[$string])) {
+ return $this->pluralFunctions[$string];
+ }
+
+ if (preg_match( '/^\s*nplurals\s*=\s*(\d+)\s*;\s*plural=(.*)$/u', $string, $matches)) {
+ // sanitize
+ $nplurals = preg_replace( '/[^0-9]/', '', $matches[1] );
+ $plural = preg_replace( '#[^n0-9:\(\)\?\|\&=!<>+*/\%-]#', '', $matches[2] );
+
+ $body = str_replace(
+ array( 'plural', 'n', '$n$plurals', ),
+ array( '$plural', '$n', '$nplurals', ),
+ 'nplurals='. $nplurals . '; plural=' . $plural
+ );
+
+ // add parents
+ // important since PHP's ternary evaluates from left to right
+ $body .= ';';
+ $res = '';
+ $p = 0;
+ for($i = 0; $i < strlen($body); $i++) {
+ $ch = $body[$i];
+ switch ( $ch ) {
+ case '?':
+ $res .= ' ? (';
+ $p++;
+ break;
+ case ':':
+ $res .= ') : (';
+ break;
+ case ';':
+ $res .= str_repeat( ')', $p ) . ';';
+ $p = 0;
+ break;
+ default:
+ $res .= $ch;
+ }
+ }
+
+ $body = $res . 'return ($plural>=$nplurals?$nplurals-1:$plural);';
+ $function = create_function('$n', $body);
+ $this->pluralFunctions[$string] = $function;
+ return $function;
+ } else {
+ // default: one plural form for all cases but n==1 (english)
+ $function = create_function(
+ '$n',
+ '$nplurals=2;$plural=($n==1?0:1);return ($plural>=$nplurals?$nplurals-1:$plural);'
+ );
+ $this->pluralFunctions[$string] = $function;
+ return $function;
+ }
+ }
}
diff --git a/lib/public/l10n/ifactory.php b/lib/public/l10n/ifactory.php
index 2d6f8e873cc..264c9719639 100644
--- a/lib/public/l10n/ifactory.php
+++ b/lib/public/l10n/ifactory.php
@@ -66,4 +66,14 @@ interface IFactory {
* @since 9.0.0
*/
public function setLanguageFromRequest($app = null);
+
+
+ /**
+ * Creates a function from the plural string
+ *
+ * @param string $string
+ * @return string Unique function name
+ * @since 9.0.0
+ */
+ public function createPluralFunction($string);
}