Jump to content

[SOLVED] adding languages to site guidance needed


xcoderx

Recommended Posts

hi i want to add different languages to my site can someone tell me how to do it?

 

i seen it some place long back they do it something  like

 

$lang['username'] = 'Username';

$lang['password'] = 'Password';

$lang['login'] = 'login';

$lang['register'] = 'Register';

$lang['features'] = 'Features';

$lang['terms'] = 'Terms And Conditions';

$lang['home'] = 'Home';

$lang['notmember'] = 'Not Member Yet?';

$lang['birthday'] = 'Birthday';

 

can someone tell me how to get it done? it should work when a user is not logged in and once logged in

Link to comment
Share on other sites

Okay

Just say on my site I have this

 

<?php
$LangCode = (!empty($_GET['lang']))?$_GET['lang']:"en";

//include language file
$LangFile = "lang.".$LangCode.".php"; //ie lang.en.php
if(file_exists($LangFile)) include_once $LangFile;

//echo some stuff
echo $lang['Welcome'];
?>

 

 

Now for English (EN) i have this file

a language file

<?php
$lang = array();
$lang['Welcome'] = "Welcome to my site";
$lang['username'] = 'Username';
?>

 

for spanish (ES) i have this file

<?php
$lang = array();
$lang['Welcome'] = "Bienvenido a mi sitio web";
$lang['username'] = 'Nombre de usuario';
?>

 

Now when I visit www.mysite.com?lang=es

I get

Bienvenido a mi sitio web

 

but with out anything or with lang=en

I get

Welcome to my site
Link to comment
Share on other sites

<?php
$LangCode = (!empty($_GET['lang']))?$_GET['lang']:"en";

//include language file
$LangFile = "lang.".$LangCode.".php"; //ie lang.en.php
if(file_exists($LangFile)) include_once $LangFile;

//echo some stuff
echo $lang['Welcome'];
?>

 

What if i pass lang=alien? Then it would load no language at all..

 

require_once '/path/to/default/language.php'; // in case lang defines a non-supported language
$LangFile = "lang.".$LangCode.".php"; //ie lang.en.php
if(file_exists($LangFile)) require_once $LangFile; // overwrites the default language.

Link to comment
Share on other sites

Wot if i store language in a dir as language how to cal it?

 

$languageDirectory = '/path/to/languages/directory';
$languageFile = "lang.$lang.php";
require_once $languageDirectory . DIRECTORY_SEPARATOR . $languageFile;

 

Wil it work with and without login to site?

 

Login has nothing to do with it.

Link to comment
Share on other sites

Supposing a registered user login and he had previously selected spanish language wil it not do any harm with session?

And im lil bit confused using your code with madtechiez code, as in esembling both urz and his codes together. Help please.

Link to comment
Share on other sites

$languageCode = (!empty($_GET['language']) && is_lang($_GET['language'])) ? $_GET['language'] : 'en';

$languagesDirectory = '/path/to/languages/directory';
$languagesFile = "lang.$langCode.php"; //ie lang.en.php

require_once $languagesDirectory . DIRECTORY_SEPARATOR . 'lang.en.php'; // default language (has all the applications translations)
if(file_exists($langFile)) require_once $languagesDirectory . DIRECTORY_SEPARATOR . $langFile; // overwrites the default language

//echo some stuff
echo $lang['Welcome'];// if not overwritten outputs english otherwise the chosen language

function is_lang($lang) {
    return preg_match('/[a-z]{2}/', $lang);
}

Link to comment
Share on other sites

A much cleaner version:

 

session_start();

$defaultLanguage = 'en';
$languagesDirectory = '/path/to/languages/directory';
language_load($defaultLanguage);
if (!empty($_GET['language']) && language_is_valid_code($_GET['language']) && language_is_supported($_GET['language'])) {
    $_SESSION['language'] = $_GET['language'];
    language_load($_GET['language']);
}

function language_is_valid_code($language_code) {
    return preg_match('/[a-z]{2}/', $language_code);
}

function language_get_path($language) {
    global $languageDirectory;
    return implode(DIRECTORY_SEPARATOR, array($languageDirectory, "lang.$language.php"));
}

function language_is_supported($language) {
    global $languagesDirectory;
    return file_exists(language_get_path($language));
}

function language_load($language) {
    require_once language_get_path($language);
}

Link to comment
Share on other sites

What if i pass lang=alien? Then it would load no language at all..

 

It was on a concept.. not a complete solution,

also your "cleaner version" is slight over kill on the functions side! and would suite a OOP solution better,

when you could add an simply else statement to mine! to fix the problem

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.