Skip to content

Commit

Permalink
lang(): instead of count accept options as 2nd parameter (if int -> c…
Browse files Browse the repository at this point in the history
…ount)
  • Loading branch information
plepe committed Aug 27, 2018
1 parent 5a2336e commit 3f1bf70
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 13 deletions.
23 changes: 17 additions & 6 deletions inc/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function lang_shall_count_translations() {
return true
}

function lang_element(str, count) {
function lang_element(str, options) {
var l;
var non_translated_counted = false

Expand All @@ -42,7 +42,7 @@ function lang_element(str, count) {

var i;
if(l.length && l.length>1) {
if((count===0)||(count>1))
if((options.count===0)||(options.count>1))
i=1;
else
i=0;
Expand All @@ -57,7 +57,7 @@ function lang_element(str, count) {
return l[0];
}
else if (typeof l === 'object') {
if ('!=1' in l && (count === 0 || count > 1)) {
if ('!=1' in l && (options.count === 0 || options.count > 1)) {
return l['!=1']
}

Expand Down Expand Up @@ -96,7 +96,7 @@ function lang_element(str, count) {
return str;
}

function lang(str, count) {
function lang(str, options) {
var el;

// if 'key' is an array, translations are passed as array values, like:
Expand All @@ -117,11 +117,22 @@ function lang(str, count) {
//
// if current language is not defined in the array the first language
// will be used (in that case 'en').
if (typeof options === 'number') {
options = { count: options }
}
if (options === null || typeof options === 'undefined') {
options = { count: null }
}

if(typeof str=="object") {
prefix = ""
if((arguments.length>1) && (typeof arguments[1] == "string")) {
prefix = arguments[1];
count = arguments[2];
options = arguments[2];
}

if (typeof options === 'number') {
options = { count: options }
}

if(typeof str[prefix + ui_lang] !== "undefined") {
Expand All @@ -140,7 +151,7 @@ function lang(str, count) {
}
}
else
el=lang_element(str, count);
el=lang_element(str, options);

if(arguments.length<=2)
return el;
Expand Down
18 changes: 11 additions & 7 deletions inc/lang.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,18 @@ function lang() {
$offset=1;

$key=func_get_arg(0);
if((sizeof(func_get_args())>1)&&is_numeric(func_get_arg(1))) {
if((sizeof(func_get_args())>1) && (is_numeric(func_get_arg(1)) || is_array(func_get_arg(1)))) {
$offset++;
$count=func_get_arg(1);
$options = func_get_arg(1);
}
else
$count=1;
$options = array('count' => 1);
$params=array_slice(func_get_args(), $offset);

if (is_numeric($options)) {
$options = array('count' => $options);
}

// if 'key' is an array, translations are passed as array values, like:
// array(
// 'en' =>"English text",
Expand All @@ -44,7 +48,7 @@ function lang() {
if((sizeof(func_get_args()) > 1) && (is_string(func_get_arg(1)))) {
$prefix = func_get_arg(1);
if(sizeof(func_get_args())>2)
$count = func_get_arg(2);
$options = func_get_arg(2);
}

if(isset($key["{$prefix}{$ui_lang}"]))
Expand All @@ -64,7 +68,7 @@ function lang() {
$key_exp=explode(";", $m[2]);
if(sizeof($key_exp)>1) {
foreach($key_exp as $key_index=>$key_value) {
$key_exp[$key_index]=lang("$m[1]/$key_value", $count);
$key_exp[$key_index]=lang("$m[1]/$key_value", $options);
}
$l=implode(", ", $key_exp);
}
Expand Down Expand Up @@ -99,7 +103,7 @@ function lang() {
$l=$l[0];
}
else {
if ($count===0 || $count!=1)
if ($options['count'] === 0 || $options['count'] !=1)
$i=1;
else
$i=0;
Expand All @@ -112,7 +116,7 @@ function lang() {
}
}
else {
if (array_key_exists('!=1', $l) && ($count === 0 || $count > 1)) {
if (array_key_exists('!=1', $l) && ($options['count'] === 0 || $options['count'] > 1)) {
$l = $l['!=1'];
}
elseif (array_key_exists('message', $l)) {
Expand Down

0 comments on commit 3f1bf70

Please sign in to comment.