Add multiple plural forms to the php part
This commit is contained in:
parent
18fc22b52b
commit
bb0c5bff5f
|
@ -49,8 +49,7 @@ function t(app, text, vars, count){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var _build = function (text, vars, count) {
|
var _build = function (text, vars, count) {
|
||||||
// FIXME: replace %n with content of count
|
return text.replace(/%n/g, count).replace(/{([^{}]*)}/g,
|
||||||
return text.replace(/{([^{}]*)}/g,
|
|
||||||
function (a, b) {
|
function (a, b) {
|
||||||
var r = vars[b];
|
var r = vars[b];
|
||||||
return typeof r === 'string' || typeof r === 'number' ? r : a;
|
return typeof r === 'string' || typeof r === 'number' ? r : a;
|
||||||
|
@ -62,7 +61,7 @@ function t(app, text, vars, count){
|
||||||
translation = t.cache[app][text];
|
translation = t.cache[app][text];
|
||||||
}
|
}
|
||||||
|
|
||||||
if(typeof vars === 'object' || typeof count !== 'undefined' ) {
|
if(typeof vars === 'object' || count !== undefined ) {
|
||||||
return _build(translation, vars, count);
|
return _build(translation, vars, count);
|
||||||
} else {
|
} else {
|
||||||
return translation;
|
return translation;
|
||||||
|
@ -79,8 +78,8 @@ t.cache={};
|
||||||
* @param vars (optional) FIXME
|
* @param vars (optional) FIXME
|
||||||
* @return string
|
* @return string
|
||||||
*/
|
*/
|
||||||
function tp(app, text_singular, text_plural, count, vars){
|
function n(app, text_singular, text_plural, count, vars){
|
||||||
if(count==1){
|
if(count === 1) {
|
||||||
return t(app, text_singular, vars, count);
|
return t(app, text_singular, vars, count);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
|
|
30
lib/l10n.php
30
lib/l10n.php
|
@ -176,15 +176,30 @@ class OC_L10N{
|
||||||
*
|
*
|
||||||
* Returns the translation. If no translation is found, $text will be
|
* Returns the translation. If no translation is found, $text will be
|
||||||
* returned. %n will be replaced with the number of objects.
|
* returned. %n will be replaced with the number of objects.
|
||||||
|
*
|
||||||
|
* In case there is more than one plural form you can add a function
|
||||||
|
* "selectplural" in core/l10n/l10n-*.php
|
||||||
|
*
|
||||||
|
* Example:
|
||||||
|
*
|
||||||
|
* [...]
|
||||||
|
* 'selectplural' => function($i){return $i == 1 ? 0 : $i == 2 ? 1 : 2},
|
||||||
|
* [...]
|
||||||
*/
|
*/
|
||||||
public function tp($text_singular, $text_plural, $count, $parameters = array()) {
|
public function n($text_singular, $text_plural, $count, $parameters = array()) {
|
||||||
if($count == 1){
|
$identifier = "_${text_singular}__${text_plural}_";
|
||||||
|
if(array_key_exists( $this->localizations, "selectplural") && array_key_exists($this->translations, $identifier)) {
|
||||||
|
return new OC_L10N_String( $this, $identifier, $parameters, $count );
|
||||||
|
}
|
||||||
|
else{
|
||||||
|
if($count === 1) {
|
||||||
return new OC_L10N_String($this, $text_singular, $parameters, $count);
|
return new OC_L10N_String($this, $text_singular, $parameters, $count);
|
||||||
}
|
}
|
||||||
else{
|
else{
|
||||||
return new OC_L10N_String($this, $text_plural, $parameters, $count);
|
return new OC_L10N_String($this, $text_plural, $parameters, $count);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Translating
|
* @brief Translating
|
||||||
|
@ -220,6 +235,17 @@ class OC_L10N{
|
||||||
return $this->translations;
|
return $this->translations;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief get localizations
|
||||||
|
* @returns Fetch all localizations
|
||||||
|
*
|
||||||
|
* Returns an associative array with all localizations
|
||||||
|
*/
|
||||||
|
public function getLocalizations() {
|
||||||
|
$this->init();
|
||||||
|
return $this->localizations;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Localization
|
* @brief Localization
|
||||||
* @param $type Type of localization
|
* @param $type Type of localization
|
||||||
|
|
|
@ -18,11 +18,18 @@ class OC_L10N_String{
|
||||||
|
|
||||||
public function __toString() {
|
public function __toString() {
|
||||||
$translations = $this->l10n->getTranslations();
|
$translations = $this->l10n->getTranslations();
|
||||||
|
$localizations = $this->l10n->getLocalizations();
|
||||||
|
|
||||||
$text = $this->text;
|
$text = $this->text;
|
||||||
if(array_key_exists($this->text, $translations)) {
|
if(array_key_exists($this->text, $translations)) {
|
||||||
|
if(is_array($translations[$this->text])) {
|
||||||
|
$id = $localizations["selectplural"]( $count );
|
||||||
|
$text = $translations[$this->text][$id]
|
||||||
|
}
|
||||||
|
else{
|
||||||
$text = $translations[$this->text];
|
$text = $translations[$this->text];
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// Replace %n first (won't interfere with vsprintf)
|
// Replace %n first (won't interfere with vsprintf)
|
||||||
$text = str_replace('%n', $this->count, $text);
|
$text = str_replace('%n', $this->count, $text);
|
||||||
return vsprintf($text, $this->parameters);
|
return vsprintf($text, $this->parameters);
|
||||||
|
|
Loading…
Reference in New Issue