Using gettext for multiple languages

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
chrisguk
Forum Newbie
Posts: 1
Joined: Fri Apr 27, 2012 2:13 pm
Location: Germany

Using gettext for multiple languages

Post by chrisguk »

Good evening friends!

I am completely new to this forum as you can see. Up to now I have tried my best to figure things out myself and use Google. But I have hit a brick wall :banghead: this time because for the life of me I cannot find a solution to my problem. The problem is I dont even know where to start. :oops:

Ok my vision ! ..............

When you visit a website you may in in the top right corner a dropdown box to select your preferred language. I only want two for mine and thats English and German.

Now I am currently testing php-gettext and it is very easy to use so I think I will stick with it. I have started building my strings already

Code: Select all

<?php echo _(hello world); ?>
so that part is fine. Here is the code I have so far:

test.php

Code: Select all

<?php  
include("local.php"); ?>
<html>
<head lang="en" >

</head>
<body>
	<div id="navbar"><div class="langbar"><a href="?lang=en_GB">English</a> | <a href="?lang=de_DE">Deutsch</a> |
        
<br />
<?php echo gettext("Welcome to My PHP Application");
echo "<br />";
// Or use the alias _() for gettext()
echo _("Have a nice day");

echo $_SERVER['HTTP_ACCEPT_LANGUAGE'];

 ?>
</body>
</html>
local.php

Code: Select all

<?php	
$directory = dirname(__FILE__).'/locale';
$domain = 'messages';	
$locale ="de_DE.utf8";
//$locale ="en_GB.utf8";
setlocale( LC_MESSAGES, $locale);
bindtextdomain($domain, $directory);
textdomain($domain);
bind_textdomain_codeset($domain, 'iso-8859-1');

?>
My question is:

How do I implement this as a sitewide solution where the user can choose their language?
How do I have it set a default language?

I hope I am making sense but if you need any clarification then just let me know.
Post Reply