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

$_SERVER['HTTP_ACCEPT_LANGUAGE']

 

Defines the languages the user "understands" and is indicated by a quality ranging from 0 to 1 (inclusive) where 1=Very Good and 0=Very Poor. For more information on http accept headers:

 

http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html

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

<?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.

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.

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.

$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);
}

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);
}

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.