Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/nextcloud/server.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakob Sack <mail@jakobsack.de>2013-08-01 23:18:18 +0400
committerJakob Sack <mail@jakobsack.de>2013-08-01 23:25:14 +0400
commit9673a563b567ee6f580c157a901f038f38f0b5a2 (patch)
tree699c20139788dd5f2528e87d2d26c81be99bdc12 /lib/l10n.php
parent495e44e76c15d43eb847dd73317bdb9d6926ff31 (diff)
Create plural functions on the fly
Diffstat (limited to 'lib/l10n.php')
-rw-r--r--lib/l10n.php91
1 files changed, 85 insertions, 6 deletions
diff --git a/lib/l10n.php b/lib/l10n.php
index d24717a23a2..208fa930c99 100644
--- a/lib/l10n.php
+++ b/lib/l10n.php
@@ -55,9 +55,14 @@ class OC_L10N{
private $translations = array();
/**
- * Plural forms
+ * Plural forms (string)
*/
- private $plural_forms = "";
+ private $plural_form_string;
+
+ /**
+ * Plural forms (function)
+ */
+ private $plural_form_function = null;
/**
* Localization
@@ -144,7 +149,7 @@ class OC_L10N{
}
}
if(isset($PLURAL_FORMS)) {
- $this->plural_forms = $PLURAL_FORMS;
+ $this->plural_form_string = $PLURAL_FORMS;
}
}
@@ -162,6 +167,66 @@ class OC_L10N{
}
/**
+ * @brief Creates a function that The constructor
+ * @param $app the app requesting l10n
+ * @param $lang default: null Language
+ * @returns OC_L10N-Object
+ *
+ * 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
+ */
+ 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 parens
+ // 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);';
+ 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);'
+ );
+ }
+ }
+
+ /**
* @brief Translating
* @param $text String The text we need a translation for
* @param array $parameters default:array() Parameters for sprintf
@@ -239,14 +304,28 @@ class OC_L10N{
}
/**
- * @brief getPluralForms
+ * @brief getPluralFormString
* @returns Fetches the gettext "Plural-Forms"-string
*
* Returns a string like "nplurals=2; plural=(n != 1);"
*/
- public function getPluralForms() {
+ public function getPluralFormString() {
$this->init();
- return $this->plural_forms;
+ return $this->plural_form_string;
+ }
+
+ /**
+ * @brief getPluralFormFunction
+ * @returns returns the plural form function
+ *
+ * returned function accepts the argument $n
+ */
+ public function getPluralFormString() {
+ $this->init();
+ if(is_null($this->plural_form_function)) {
+ $this->plural_form_function = createPluralFormFunction($this->plural_form_string);
+ }
+ return $this->plural_form_function;
}
/**