views:

330

answers:

4

I have a website that's currently in English; I want to be able to switch to a different language when a user clicks on a different language (there are little country flag icons on the site). The way I'm currently trying is with arrays, e.g.:

$english = array('index',
           array('h1' => 'this is some h1 text', 
                 'h2' => 'this is some h2 text'));

$japanese = array('index',
            array('h1' => '世界交換への歓迎',
                  'h2' => 世界交換への'));


print $english[index][h1];
print $japanese[index][h2];

As you can see, if I did this for every single page in a separate language, it would be an insane amount of code. What other method can I try?

+1  A: 

You could just use the Google translate api:

http://code.google.com/apis/ajaxlanguage/

It has tons of documentation and there are examples of how to use it on the google code playground:

http://code.google.com/apis/ajax/playground/

Just browse to ajax > translation

JasonV
"Reliance on it, the automatic translation will be applicable, the case of very brave me - not all languages share sentence structure" (Google's automatic translation). Original intent: "Relying on automatic translation sounds very optimistic to me, (...)" Case closed.
Piskvor
A: 

I'd suggest hooking your site into the Windows Live or Google Translate API's. I don't know about the Google one, but the Windows Live API seems really easy to use.

http://msdn.microsoft.com/en-us/magazine/dd569750.aspx

That, or you can write out all the content on the page in two languages, store them both in different tables in a "content" database, then change which table is loaded when the user clicks the change language button. Now, the majority of your drudgery would be in rewriting the content in both languages.

chustar
heh. Microsoft.
JasonV
the google api and live api seem like the same story with how im doing the arrays. to save time im hardcoding it rather than using a db. the way i've layed out the arrays how can i call the english or the japanese based on a parameter in my url? for example: index.php?l=en
SarmenHB
The google api (idk about live), can be done with a url. And, you don't have to store the translated text in arrays, Google does all the work for you. You just pass it the argument of a javascript function. Check out the google code playground for more info
JasonV
Drudgery indeed, but until we have strong AI, machine translation output will be rather useless. Yes, you will get out *something* that appears to be in the desired language, but "looks similarity this to will have it." This may be understandable (with some effort), but do you want to have such text on your website? You'll seem too-cheap-to-hire-a-translator at best, or just plainly illiterate.
Piskvor
+1  A: 
  • Content database (or file I guess)
  • Entry per page for each language
  • Language referenced by ID
  • ID set in $_SESSION variable
  • Optionally remembered in cookie for subsequent visits

I've used this system with a custom CMS @ www.grandhall.eu. It gets especially fun when you need to take things like brochures & other downloads into account.

da5id
+4  A: 

Given that you are looking for full i18n support which will eventually lead to l10n support, I would suggest writing your page in a PHP framework that supports these things out of the box.

Personally I've only done translations with the Symfony framework. They use a combination of i18n table extension in the DB for content, and XLIFF files for translations of the interface. It was fairly transparent once it was setup, and using a framework avoids having to write all this support by hand.

I also know that i18n is supported in Zend, CakePHP, and Code Igniter.

Pro777
I second Symfony it makes it easy to extract strings out and to a file for translation.
JD
+1 for symfony. It makes localization and internationalization easy-peasy!
phidah