/    Sign up×
Community /Pin to ProfileBookmark

Maintaining the selected Language?

Hi,

I have managed to get the code below working with a few tweaks here and there. However, you can see that I have written a small function in the file name local.php to allow the user to select “English” or “German” in the link on the test.php page.

This works fine but it will only work on that particular webpage. As soon as I move away from that page the locale will return back to “en_US”(English). Is there a way I could implement lets say an option dropdown and save the locale across the whole site.

Just in case its relevant I have a MySQL DB available for use if thats appropriate.

local.php

[code=php]<?php

function english(){

$directory = dirname(__FILE__).’/locale’;
$domain = ‘messages’;
$locale =”en_UK.utf8″;
setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, ‘UTF-8′);

}

function german(){

$directory = dirname(__FILE__).’/locale’;
$domain = ‘messages’;
$locale =”de_DE.utf8″;
setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, ‘UTF-8’);

}

//Pagination and normal view switch
if (isset($_GET[‘run’])) $linkchoice=$_GET[‘run’];
else $linkchoice=”;

switch($linkchoice){

case ‘English’ :
english();
break;

case ‘German’ :
german();
break;

default :
english();
break;

}

?>
[/code]

test.php

[code=php]<?php include(“local.php”); ?>
<html><head></head>
<body>
<?php echo “<p><a href=’?run=English’>English</a> | <a href=’?run=German’>German</a> </p>”; ?>
<br />
<?php echo gettext(“Welcome to My PHP Application”);
echo “<br />”;
// Or use the alias _() for gettext()
echo _(“Have a nice day”); ?>
</body>
</html>[/code]

Many thanks in advance!

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@NightcatApr 27.2012 — Set up a session with a session variable $language ?
Copy linkTweet thisAlerts:
@chrisgukauthorApr 27.2012 — Set up a session with a session variable $language ?[/QUOTE]

I get that, I think ?

But how can I be sure that it will use the .mo file in the locale folder. What I am attempting to do is have a backend panel that I can choose the preferred language from a drop down and then hit the save button. The that changes the site wide language.
Copy linkTweet thisAlerts:
@NightcatApr 27.2012 — [code=php]

function language($locale){

$directory = dirname(__FILE__).'/locale';
$domain = 'messages';

setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'UTF-8');

}

[/code]


That's your function


[code=php]

language($session['language']);

[/code]


and this is the way you call it in your code.

read up a bit about "session" and "setting cookies"

Good luck ?
×

Success!

Help @chrisguk spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...