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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T18:13:56+00:00 2026-05-13T18:13:56+00:00

I tried gettext , but my free hosting doesn’t allow it. I thought about

  • 0

I tried gettext, but my free hosting doesn’t allow it. I thought about Zend_translate, but I didn’t want to use elements from frameworks since my page is mostly static.

So, I ended up with this tutorial:

http://www.bitrepository.com/php-how-to-add-multi-language-support-to-a-website.html

Where the author only use basic php (not sure), and it seems to work, but I’m not quite sure if it a good (or common) practice or if it can cause me problems in the future (adding and deleting bunch of code).

Here it is:

common.php:

<?php
session_start();
header('Cache-control: private'); // IE 6 FIX

if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];

// register the session and set the cookie
$_SESSION['lang'] = $lang;

setcookie("lang", $lang, time() + (3600 * 24 * 30));
}
else if(isSet($_SESSION['lang']))
{
$lang = $_SESSION['lang'];
}
else if(isSet($_COOKIE['lang']))
{
$lang = $_COOKIE['lang'];
}
else
{
$lang = 'en';
}

switch ($lang) {
  case 'en':
  $lang_file = 'lang.en.php';
  break;

  case 'de':
  $lang_file = 'lang.es.php';
  break;

  default:
  $lang_file = 'lang.en.php';

}

include_once 'languages/'.$lang_file;
?>

languages/lang.en.php:

<?php
/* 
-----------------
Language: English
-----------------
*/

define('GREETING, Hello World');
?>

languages/lang.es.php:

<?php
/* 
-----------------
Language: Espanol
-----------------
*/

define('GREETING, Hola Mundo');
?>

index.php:

include_once 'common.php';
<p><?php echo LANG_TEST; ?></p>

So, if I want to change it to Spanish I just add: ?lang=es in the URL (after index.php)

  • 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-13T18:13:56+00:00Added an answer on May 13, 2026 at 6:13 pm

    First off all, it will not work with your code. You would have to use

    define('GREETING', 'Hello World').
    

    Check the PHP manual for define.

    Second, using contants for this is a horrible idea. You are littering the global namespace with tons of constants and risk constant nameclashing. See the Userland Naming Guide.

    If you do not want to use Zend_Translate (you don’t have to use the entire framework for this) and cannot use gettext, I suggest you use arrays for storing the translations, e.g. something like this:

    $lang = array(
        'greeting'  => 'Hello World'
        'something' => 'else'
    );
    

    and then you can use it like this in your template:

    <h1><?php echo $lang['greeting'] ?></h1>
    

    This way, you only have to make sure, $lang is not already defined in the global scope.

    Some people prefer to use the default language instead of translation ids, e.g. they prefer to write

    <h1><?php echo t('Hello World') ?></h1>
    

    where t would function mapping the input string to the output string. The translation array would have to contain the full sentences then and map these to the other languages, e.g.

    $lang = array(
        'Hello World' => 'Hola Mundo'
    );
    

    But of course, you could just access this with $lang['Hello World'] as well. It just gets awkward for long strings. Many translation functions allow you to pass in additional params though, to allow for something like this:

    $lang = array(
        'currentTime' => 'The current time is %s'
    );
    
    <h1><?php echo t('currentTime', date('H:i:s')) ?></h1>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Tried Googling , but couldn't find anything. I have a few files and folders
Tried deploying a simple web application (jboss 5.1, jboss seam 2.2.1) on a machine.
I'm getting an obnoxious segfault in PHP 5.3.6 (compiled from source, Slackware 13.1 x86,
I have many buttons in a button group that need to search a database
I am trying to create a table, whereas when the user clicks on one
I am trying to attach files in my RoR app. I am using Rails
I have a textview in which i append text in two ways: First way:
I am trying to create my first Chrome extension - a duplicate of the
I wrote an app which has 2 screens. The first screen is triggered by
Currently, I am working on a web application that uses draw2d library to draw

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.