Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8628005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:25:31+00:00 2026-06-12T08:25:31+00:00

I am working on multilingual application with a centralized language system. It’s based on

  • 0

I am working on multilingual application with a centralized language system. It’s based on language files for each language and a simple helper function:

en.php

$lang['access_denied'] = "Access denied.";
$lang['action-required'] = "You need to choose an action.";
...
return $lang;

language_helper.php

...
function __($line) {
  return $lang[$line];
}

Up til now, all strings were system messages addressed to the current user, hence I always could do it that way. Now, I need create other messages, where the string should depend on a dynamic value. E.g. in a template file I want to echo the number of action points. If the user only has 1 point, it should echo “You have 1 point.”; but for zero or more than 1 point it should be “You have 12 points.”

For substitution purposes (both strings and numbers) I created a new function

function __s($line, $subs = array()) {
  $text = $lang[$line];
  while (count($subs) > 0) {
    $text = preg_replace('/%s/', array_shift($subs), $text, 1);
  }
  return $text;
}

Call to function looks like __s('current_points', array($points)).

$lang['current_points'] in this case would be "You have %s point(s).", which works well.

Taking it a step further, I want to get rid of the “(s)” part. So I created yet another function

function __c($line, $subs = array()) {
  $text = $lang[$line];
  $text = (isset($sub[0] && $sub[0] == 1) ? $text[0] : $text[1];
  while (count($subs) > 0) {
    $text = preg_replace('/%d/', array_shift($subs), $text, 1);
  }
  return $text;
}

Call to function looks still like __s('current_points', array($points)).

$lang['current_points'] is now array("You have %d point.","You have %d points.").

How would I now combine these two functions. E.g. if I want to print the username along with the points (like in a ranking). The function call would be something like __x('current_points', array($username,$points)) with $lang['current_points'] being array("$s has %d point.","%s has %d points.").

I tried to employ preg_replace_callback() but I am having trouble passing the substitute values to that callback function.

$text = preg_replace_callback('/%([sd])/', 
  create_function(
    '$type',
    'switch($type) {
      case "s": return array_shift($subs); break;
      case "d": return array_shift($subs); break;
    }'),
  $text);

Apparently, $subs is not defined as I am getting “out of memory” errors as if the function is not leaving the while loop.

Could anyone point me in the right direction? There’s probably a complete different (and better) way to approach this problem. Also, I still want to expand it like this:

$lang['invite_party'] = "%u invited you to $g party."; should become Adam invited you to his party." for males and "Betty invited you to her party." for females. The passed $subs value for both $u and $g would be an user object.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T08:25:32+00:00Added an answer on June 12, 2026 at 8:25 am

    As mentionned by comments, I guess gettext() is an alternative

    However if you need an alternative approach, here is a workaround

    class ll
    {
        private $lang = array(),
                $langFuncs = array(),
                $langFlags = array();
    
        function __construct()
        {
            $this->lang['access'] = 'Access denied';
            $this->lang['points'] = 'You have %s point{{s|}}';
            $this->lang['party'] = 'A %s invited you to {{his|her}} parteh !';
            $this->lang['toto'] = 'This glass seems %s, {{no one drank in already|someone came here !}}';
    
            $this->langFuncs['count'] = function($in) { return ($in>1)?true:false; };
            $this->langFuncs['gender'] = function($in) { return (strtolower($in)=='male')?true:false; };
            $this->langFuncs['emptfull'] = function($in) { return ($in=='empty')?true:false; };
    
            $this->langFlags['points'] = 'count';
            $this->langFlags['toto'] = 'emptfull';
            $this->langFlags['party'] = 'gender';
        }
    
        public function __($type,$param=null)
        {
            if (isset($this->langFlags[$type])) {
                $f = $this->lang[$type];
                preg_match("/{{(.*?)}}/",$f,$m);
    
                list ($ifTrue,$ifFalse) = explode("|",$m[1]);
    
                if($this->langFuncs[$this->langFlags[$type]]($param)) {
                    return $this->__s(preg_replace("/{{(.*?)}}/",$ifTrue,$this->lang[$type]),$param);
                } else {
                    return $this->__s(preg_replace("/{{(.*?)}}/",$ifFalse,$this->lang[$type]),$param);
                }
            } else {
                return $this->__s($this->lang[$type],$param);
            }
        }
        private function __s($s,$i=null)
        {
            return str_replace("%s",$i,$s);
        }
    }
    
    $ll = new ll();
    
    echo "Call : access - NULL\n";
    echo $ll->__('access'),"\n\n";
    echo "Call : points - 1\n";
    echo $ll->__('points',1),"\n\n";
    echo "Call : points - 175\n";
    echo $ll->__('points',175),"\n\n";
    echo "Call : party - Male\n";
    echo $ll->__('party','Male'),"\n\n";
    echo "Call : party - Female\n";
    echo $ll->__('party','Female'),"\n\n";
    echo "Call : toto - empty\n";
    echo $ll->__('toto','empty'),"\n\n";
    echo "Call : toto - full\n";
    echo $ll->__('toto','full');
    

    This outputs

    Call : access - NULL
    Access denied
    
    Call : points - 1
    You have 1 point
    
    Call : points - 175
    You have 175 points
    
    Call : party - Male
    A Male invited you to his parteh !
    
    Call : party - Female
    A Female invited you to her parteh !
    
    Call : toto - empty
    This glass seems empty, no one drank in already
    
    Call : toto - full
    This glass seems full, someone came here !
    

    This may give you an idea on how you could centralize your language possibilities, creating your own functions to resolve one or another text.

    Hope this helps you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a multilingual application that uses IIS7-based url rewriting. I'd like the
I am working on a multilingual web application. I'm wondering how do i design
I am working on a CMS system tries to support multilingual natively. I have
I'm working on multilingual Asp.NET MVC application. In url i need to use category
I am working on a multilingual website and need to include CSS file based
I am working on an application in C#, I want to make it multilingual.
I am putting together a multilingual site according to the tutorial: http://nuts-and-bolts-of-cakephp.com/2008/11/28/cakephp-url-based-language-switching-for-i18n-and-l10n-internationalization-and-localization/ However, the
I'm writing multilingual website. I have several files on server like: /index.php /files.php /funny.php
I am working on a Multilingual Desktop Application. I created a resource file for
I am working on Spring web application and my application is multilingual. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.