SarmenHB
SarmenHB

Reputation: 527

PHP - how to translate a website into multiple languages?

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?

Upvotes: 3

Views: 8576

Answers (4)

Pro777
Pro777

Reputation: 1704

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.

Upvotes: 6

da5id
da5id

Reputation: 9136

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

Upvotes: 1

JasonV
JasonV

Reputation: 596

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

Upvotes: -4

chustar
chustar

Reputation: 12455

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.

Upvotes: -2

Related Questions