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 528569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T09:00:02+00:00 2026-05-13T09:00:02+00:00

I am doing a PHP web site, without using any framework. I need that

  • 0

I am doing a PHP web site, without using any framework. I need that the site is available in several languages, and I was reading about it and it seems to be a little bit confusing. There are several solutions but all seem to depend on a specific framework.

What you think of using a simple translation function like the one shown below?

I mean, I would like to know what can be a disadvantage of using such code.
Here it is (this is just a simple and incomplete sample):

class Translator{

    private $translations;

    public function __construct(){
        $this->translations = array(
            'Inbox'  => array(
                'en' => 'Inbox',
                'fr' => 'the french word for this'
            ),
            'Messages' => array(
                'en' => 'Messages',
                'fr' => 'the french word for this'
            )
            //And so on...
        );
    }

    public function translate($word,$lang){
        echo $this->translations[$word][$lang];
    }
}
  • 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-13T09:00:02+00:00Added an answer on May 13, 2026 at 9:00 am

    It does not look bad. I’ve seen this used many times.

    I would however separate the different strings in one file per language. At least, or if the files get large, one file per module per language.

    Then your translation class can load and cache the language files (if you don’t rely on any other caching system) every time a new language is to be used.

    A little example of what i mean

    class Translator {
        private $lang = array();
        private function findString($str,$lang) {
            if (array_key_exists($str, $this->lang[$lang])) {
                return $this->lang[$lang][$str];
            }
            return $str;
        }
        private function splitStrings($str) {
            return explode('=',trim($str));
        }
        public function __($str,$lang) {
            if (!array_key_exists($lang, $this->lang)) {
                if (file_exists($lang.'.txt')) {
                    $strings = array_map(array($this,'splitStrings'),file($lang.'.txt'));
                    foreach ($strings as $k => $v) {
                        $this->lang[$lang][$v[0]] = $v[1];
                    }
                    return $this->findString($str, $lang);
                }
                else {
                    return $str;
                }
            }
            else {
                return $this->findString($str, $lang);
            }
        }
    }
    

    This will look for .txt files named after the language having entries such as this

    Foo=FOO
    Bar=BAR

    It always falls back to the original string in case it does not find any translation.

    It’s a very simple example. But there is nothing wrong in my opinion with doing this by yourself if you have no need for a bigger framework.

    To use it in a much simpler way you can always do this and create a file called ‘EN_Example.txt’

    class Example extends Translator {
        private $lang = 'EN';
        private $package = 'Example';
        public function __($str) {
            return parent::__($str, $this->lang . '_' . $this->package);
        }
    }
    

    Sometimes you wish to translate strings that contain variables. One such approach is this which i find simple enough to use from time to time.

    // Translate string "Fox=FOX %s %s"
    $e = new Example();
    // Translated string with substituted arguments
    $s = printf($e->__('Fox'),'arg 1','arg 2');
    

    To further integrate variable substitution the printf functionality can be put inside the __() function like this

    public function __() {
        if (func_num_args() < 1) {
            return false;
        }
        $args = func_get_args();
        $str = array_shift($args);
        if (count($args)) {
            return vsprintf(parent::__($str, $this->lang . '_' . $this->package),$args);
        }
        else {
            return parent::__($str, $this->lang . '_' . $this->package);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

On my web site I would like to have several sub-domains. Files that create
I am doing some work on a web site that has a secure area
I'm doing a little robot in php that does the login to a site
I'm doing PHP development and I'm thinking of using one of these. I have
Since PHP has no custom-class type-casting, how would I go about doing the PHP
I have a web site built using asp.net with ugly URLs like /DisplayContent.aspx?id=789564 .
I'm working on a web app using PHP on Apache. $_SESSION variables are used
I am good in web development but recently started using PHP and developed few
I want to run a web application on php and mysql, using the CakePHP
I just finished rewriting a significant portion of my web site using python's django,

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.