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

  • Home
  • SEARCH
  • 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 993011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T06:21:15+00:00 2026-05-16T06:21:15+00:00

I’m making a site that will be translated into x languages. All strings must

  • 0

I’m making a site that will be translated into x languages.

All strings must be localized.

There are occasions when I need to display a language name, country name or other information that has been retrieved from a database. The data being dealt with in this way will seldom be changed – as above I’m talking about language names, countries etc.

In this example I’m using the array that holds the languages into which the site’s UI has been translated. To allow translation of the names (used for title text when the “change language” flag / link is hovered), I have an array like*:

Array("zh_CN" => _("Chinese - Simplified"), "en_GB" => _("English"));

I use them to get the relevant name string for a given language.

Currently I’m using a global array:

$global_langNames = Array("zh_CN" => _("Chinese - Simplified"), "en_GB" => _("English"));

Usage:

global $global_langNames;

echo $global_langNames[$code]; // $code = 'zh_CN'

Output (locale = en_GB):

Chinese Simplified

Output (locale = zh_CN):

简体中文

I would much rather declare this (and other) constant arrays as private members of the class, but it seems PHP isn’t willing:

class constants_lang{

 private static $langNames = Array("zh_CN" => _("Chinese - Simplified"), "en_GB" => _("English"));

 static function getLangName($code){
  return self::$langNames($code);
 }

}

Results in:

Parse error: syntax error, unexpected '(', expecting ')' in /site/http/includes/classes/constants/lang.php on line 20

Should I hang my head low and go back to the global array, or is there another, better way for me to have a ‘constant’ array to be used in this manner?

*The array keys are from the database table storing language codes and whether we have a UI translation:

code     ui translation
zh_CN       1
en_GB       1
zh_TW       0
      ....

Solution

class constants{

     private $langNamesFromCode;

     function __construct()
     {
          $this->langNamesFromCode = $this->initLangNamesFromCode();
     }

     /* INIT */

     private static function initLangNamesFromCode()
     {
          return Array("zh_CN" => _("Chinese - Simplified"), "en_GB" => _("English"));
     }

     /* GETTERS */

     public static function getLangNameFromCode($code)
     {
          if(self::isStatic()){
               $langNamesFromCode = self::initLangNamesFromCode();
               return $langNamesFromCode[$code];
          }
          else{
               return $this->langNamesFromCode[$code];
          }
     }

     /* UTILITY */

     private static function isStatic()
     {
          return !(isset($this) && get_class($this) == __CLASS__);
     }
}
  • 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-05-16T06:21:16+00:00Added an answer on May 16, 2026 at 6:21 am

    Yes, you can only use (most) literals in variable initializations.

    The work-around is something like:

    class A  {
        private $var;
        public function init() {
            $this->var = func();
        }
    }
    A::init();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a function that will clean a strings' special characters. I do NOT
I have a French site that I want to parse, but am running into
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
this is what i have right now Drawing an RSS feed into the php,
I've got a string that has curly quotes in it. I'd like to replace
In my XML file chapters tag has more chapter tag.i need to display chapters
I am doing a simple coin flipping experiment for class that involves flipping a

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.