Jump to content
Larry Ullman's Book Forums

Multi-Language: Any Suggestions?


Recommended Posts

I am using the second example in the book to develop a website, aimed at the European market. I am making good progress and Larry's recent addition about using PayPal instead of Authorize comes as a godsend.

I have made some basic changes to the 'customers' table and the various scripts to capture different formats for zip codes, countries instead of states, etc., and that was quite easy. Slowly, I am starting to form some thoughts on how I could give the user the possibility to change the language of the text from English to another language.

Would anyone have any suggestions as to how do so?

 

Many thanks in advance!

 

 

Michael

Link to comment
Share on other sites

As you're aware, that's a big goal, but definitely do-able. One thing you'll need to decide is how much of the content will be in another language. But to get started, you'll need to represent the common elements using multiple languages (such as navigation). One way of doing that is to create a "words" table with each column being a specific word or phrase. Each row would contain a language's representation of each word. You'll also want a "languages" table that stores the language ID, the language name in English (for your use) and the language name in its native language. You'd relate this table to the "words" table.

 

Next, you'd need to associate all the products and product attributes in other languages. This requires more tables that would bring in the language ID, the product or product attribute ID, and the language-specific version of that product or attribute.

 

That's how you'd start. Let me know if you have any questions or problems.

Link to comment
Share on other sites

Thanks for your suggestions, Larry. Frankly, I am not sure yet how your approach would work, but I am giving you the benefit of the doubt ;-).

 

I have created the tables you suggest. "Languages" was straightforward enough:

 

CREATE TABLE `languages` (

`language_id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,

`language_name` varchar(20) NOT NULL,

`language_description` varchar(20) NOT NULL,

PRIMARY KEY (`language_id`),

UNIQUE KEY `language_name` (`language_name`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8

 

"Words" may be a bit more tricky because first I have to establish all the common elements. I've made the following start:

 

CREATE TABLE `words` (

`words_id` tinyint(3) unsigned NOT NULL AUTO_INCREMENT,

`language_id` tinyint(3) unsigned NOT NULL,

`coffee` varchar(25) NOT NULL,

`goodies` varchar(25) NOT NULL,

`sales` varchar(25) NOT NULL,

`wish list` varchar(25) NOT NULL,

`cart` varchar(25) NOT NULL,

`sales items` varchar(25) NOT NULL,

`view all` varchar(25) NOT NULL,

`products` varchar(25) NOT NULL,

`add to cart` varchar(25) NOT NULL,

`update quantities` varchar(25) NOT NULL,

`checkout` varchar(25) NOT NULL,

`continue onto billing` varchar(25) NOT NULL,

`place order` varchar(25) NOT NULL,

PRIMARY KEY (`words_id`)

) ENGINE=MyISAM DEFAULT CHARSET=utf8

 

I have populated the "languages" table and for most of the elements in the "words" tables, I have the German values. So together with the English ones, that should be enough for testing purposes.

 

What would be the next step for me?

 

Wouldn't most of the "view" files in HTML have versions in different languages, too? Maybe with subdirectories ("/de" for German, "/nl" for Dutch, etc.). That would surely be one of the benefits of the MVC concept.

 

Finally, one would give the user the option to select a language but it would be great to get the default language of the user's browser (or OS) and change to that language by default (while still offering the option to change). Could that be done through PHP? If so, more stuff to be written to the cookies!!

Link to comment
Share on other sites

Well, what you would do next is have the config file or header file or some common file that's run first retrieve all the keywords for a given language and put these into an array. Then the header file, the footer file, and all the view files would print the value from this array where appropriate. I do exactly this in my "PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)" book, in the forum example chapter. If you don't have the book, you can download the code from my main site and see what I mean.

 

As for getting the default language of the browser or OS, you can't do that using PHP, because PHP runs on the server and has no knowledge of the browser. You can do it using JavaScript though.

Link to comment
Share on other sites

Hi Larry,

 

That really helped. I am slowly getting the picture and I am starting to see that probably I won't even need additional view files. So simple and pretty clever. I will try that. And, yes/of course, I have your book. Whenever there is a new edition, I buy it.

I will let you know how I get on, especially if I get stuck :-)

 

Thanks for all your help. I am not that strong in JavaScript so maybe a good occasion to treat myself to your latest book.

 

Greetings from the Republic of Ireland!

 

Michael

Link to comment
Share on other sites

  • 1 month later...

Hi Larry,

 

But couldn't I use the "HTTP_ACCEPT_LANGUAGE" header to determine the browser's language? Or are there disadvantages to that approach? Of course, I would still offer the user the possibility to change the language to one of their preference.

 

Regards,

 

 

Michael

Link to comment
Share on other sites

In theory, yes, you could do that. If the HTTP_ACCEPT_LANGUAGE is provided by the browser, you could use that as the basis and then allow the user to change it. But it may not be provided, it may not be accurate if provided, and you may not parse it properly either.

Link to comment
Share on other sites

Hi Larry,

 

Thanks for your helpful reply. Yes, I have been checking with various browsers and I can see how their difference in handling the HTTP_ACCEPT_LANGUAGE could cause trouble.

I have tackled things differently, while still using the HTTP_ACCEPT_LANGUAGE header. I have invested in the following extensions for my domain: .com, .co.uk, .de, .es and .nl because I want my site to be in English, German, Spanish and Dutch. The language is primarily determined by the country extension but if there is a usable HTTP_ACCEPT_LANGUAGE header, it will be picked up with a simple IF statement, e.g.:

 

$lang = getenv("HTTP_ACCEPT_LANGUAGE");
$set_lang = explode(',', $lang);
IF (SUBSTR($set_lang[0],0,2)=='de' || $_SERVER['SERVER_NAME']=='www.mydomain.de' || $_SERVER['SERVER_NAME']=='mydomain.de')

 

The resulting 'language_id' is then set as a cookie.

 

I have used the concept for the "Message Board" in chapter 17 in your "PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)" book. One little difference: I have created a separate table for all language elements in each .php page (table 'index' for 'index.php', table 'shop' for 'shop.php', etc.) and the $words[] array is created at the start of each page with a pretty basic query:

 

"SELECT * FROM `".SUBSTR(basename($_SERVER['PHP_SELF']),0,-4)."` WHERE language_id = {$_COOKIE['language_id']}"

 

That seems to work very nicely because there is a lot more text than in your example. I am still testing (and translating text!) but I impressed how well that works. Thanks for all your guidance! I couldn't have done it without your help and books!

 

Regards,

 

 

Michael

Link to comment
Share on other sites

Hi all,

 

I came across a very nice bit of jQuery -courtesy of the SpaceNinja (??)- that integrates perfectly with the second example on this site: http://spaceninja.com/2010/02/jquery-slide-down-language-selector

 

You will have to add this in the footer.hmtl file:

 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript" charset="utf-8"></script>
<script>
// scroll the page to display the languages
$('#util-int').toggle(function() {
$("#util-regions").remove().prependTo("#header").slideToggle('swing');
}, function() {
 $("#util-regions").slideToggle('swing');
});
</script>

 

Then include the following in the <ul class="top-links"> on the header.html page:

 

<ul class="top-links" id="utilities">
		   <li><a href="/index.php" class="first"><img alt="" src="/images/icon-home.gif" /></a></li>
		   <li><a href="/cart.php"><img alt="" src="/images/icon-cart.gif" /></a></li>
		   <li><a href="/contact.php"><img alt="" src="/images/icon-mail.gif" /></a></li>
		   <li><a href="/sitemap.php"><img alt="" src="/images/icon-map.gif" /></a></li>

			 <li id="util-int"><a href="#">International</a>
			  <div id="util-regions">
					<h4>Select your language</h4>
	 			  <ul>
						  <li><a href="http://www.mysite.com">English</a></li>
						 <li><a href="http://www.mysite.es">español</a></li>
						 <li><a href="http://www.mysite.de">deutsch</a></li>
					</ul>
				  </div>
			</li>
 </ul>

 

And finally, you make an addition to the style.css file

 

#util-int {
 z-index: 1000;
}
#util-regions {
 display: none; /* hidden by default, will be shown by jQuery */
 width: 948px;
 margin: 0 auto;
 height: 60px;
 position: relative;
}
#placeholder-regions {
 background: #666;
}

 

Of course, you may want to tweak the css but I like the way the page slides down and it looks a lot better than those flags.

I hope, you find this useful.

 

Regards,

 

 

Michael

Link to comment
Share on other sites

 Share

×
×
  • Create New...