0

I want to use the array function of PHP to translate a website. I have created PHP files with arrays in them for all the text to be translated.

<?php 
//ESPANOL
$lang = array(
              'work' => 'Trabajo'
              'packaging' => 'Empaque'
             );

And then I am calling them inside my nav.php file, and will in the content section too.

<?php include('includes/languages/es.php'); ?>
<a href="#"><?php echo $lang['work']; ?></a>

All pretty straight forward.

What I want to know is how to switch between these array files without editing the HTML, so that I don't have to link to another 'index_es.php' etc. I understand that the link would be something like this, but I don't know how this is going to work.

<a href="index.php?es.php>Español</a>|<a href="index.php/?en.php">English</a>

I'm guessing I need to include another file that includes the language files and then the link can choose from them but I don't know what the code would be for this. Would it involve including a 'lang_directory' above the link and then somehow including from there??

**Also I would like to avoid using Zend/Gettext translation becuase I want to learn this inside out.

3 Answers 3

3

You can make another dimension containing the target language. Then pass a GET parameter to select that language. If the language isn't recognized you can fallback to English. Here's a sample.

$languages = array(
    'en' => array(
        'work' => 'work',
        'packaging' => 'packaging'
    ),
    'es' => array(
        'work' => 'Trabajo',
        'packaging' => 'Empaque'
    ),
);

// default language to use when the requested isn't found
$defaultLanguage = 'en';

// language requested via GET
$requested = $_GET['locale'];

// use the requested language if it exists, otherwise the default language
$language = isset($languages[$requested]) ? $requested : $defaultLanguage;

// our translations
$translation = $languages[$language];

// "work" translated based on the language
echo $translation['work'];

And the link for Español would look like this.

index.php?locale=es
Sign up to request clarification or add additional context in comments.

5 Comments

So this is a Multi-Dimensional Array? And I would put all the languages in one file and then include it at the start of the <body> tags as locale.php and the link would run through the array and take use the language it requested?
Yes this is a multidimensional array, and yes that sounds correct.
The locale.php would be in the parent directory of index.php then? How does the link work? I mean if I have already included locale.php how does the ?locale=es change it?
locale.php would be in the same directory as index.php. As for ?locale=es, it's evaluated with $_GET and selects the languages array according to what locale is equal to.
How do you handle the full sentence? $translation["work work working a english sentence" ] ?
2

I'd keep your array system, correct the links into something like index.php?lang=en and then include your file depending on the lang parameter:

if ( isset($_GET['lang']) && file_exists('includes/languages/'.$_GET['lang'].'.php') ){
  include_once('includes/languages/'.$_GET['lang'].'.php');
}

And if you want to keep the language parameter in your session, do something like this:

if ( isset($_GET['lang']) && file_exists('includes/languages/'.$_GET['lang'].'.php') ){
  $_SESSION['lang'] = $_GET['lang'];
}
if ( !isset($_SESSION['lang']) ){
  // Default language
  $_SESSION['lang'] = 'en';
}
include_once('includes/languages/'.$_SESSION['lang'].'.php');

6 Comments

So this would be something like using <?php the code you put ?> before everything and then changing the links?
Yes (sorry for the delay!)
Don't forget session_start() !
This seems like the most simple and effective. I'll try it and let you know how I get on. Thank you
When I go between links after changing language it reverts to the ENG lang file. How do I avoid this so it stays in the current language? I'm assuming by adding something in the link php?
|
1

One way to do this is by using sessions.

  1. Make a lang.php file that will be used to change between languages.

    <?php
            //Start session
            session_start();
            //Do we get a lang variable
            if (isset($_GET['lang'])) {
                    //Make sure we only get the lang filename
                    $lang = basename($_GET['lang']);
                    //If the file exists, then save it to session
                    if (file_exists('includes/languages/' . $lang . '.php'))
                            $_SESSION['lang'] = $lang;
            }
            //If the client were refered here (via hyperlink) send them back
            if (isset($_SERVER['HTTP_REFERER']))
                    header('location: ' + $_SERVER['HTTP_REFERER']);
    ?>
    
  2. In the header of the files you want multiple languages, insert.

    <?php
            //Start session
            session_start();
            //Default language
            $lang = 'english';
            //If the client have set a language, use that instead
            if (isset($_SESSION['lang']))
                    $lang = $_SESSION['lang'];
            //Load language file
            include('includes/languages/' . $lang . '.php');
    ?>
    

The links to change language will then be like this:

<a href="lang.php?lang=espanol">Español</a>|<a href="lang.php?lang=english">English</a>

Out can also take the code from the lang.php file and put in a included file that will be loaded before the inclusion of language file and remove the HTTP_REFERER redirection.

The links to change language will then be like this:

<a href="?lang=espanol">Español</a>|<a href="?lang=english">English</a>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.