PHP - how to translate a website into multiple languages? - php

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?

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.

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.

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.

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

Related

How to make a small website multilingual?

I have this really tiny (static) site, that is basically just my online business card and I want to have it in English and German. I detect the browser language and also let the user chose manually; so I have the right $lang variable ready.
My question is how are the multiple languages best implemented (or loaded)? Right now I do this with PHP switch statements but I feel this is not proper at all. My next guess is to use XMLs, one for every language, then load the right one but I wonder if there is a more clean, simple and proper way to do this; meaning easy to maintain and extend, easy to understand, compliant with standards and fast.
Thanks for the help!
Have an array with all the texts, and place them in seperate files (one for German, one for English):
<? return array(
'contact' => 'Kontakt'
);
Load that array and use it to display the text.
If you're using a CMS (content management system) there are other options.
The standard PHP way is to use gettext functions and .po/.mo files as explained here

Writing a mutiple language website: the webdesigner's (best) options

They are some questions already about this on stackoverflow, but none is really clear about the 'best practice'.
For the content design, what are the options and what is the better option?
Some options I know are using folders: site.com/en/ and site.com/fr/ or redirects site.com/index.php?language=en
An even easier practice is using a new url: en.site.com and fr.site.com
But what if I want to keep site.com/index.php and nothing more ? What are my options for that?
For example, if you change the language on LinkedIn, there's nothing changing in the URL. How do they work there ?
update: in my case the website is a platform, using LAMP stack. Technical advice is also welcome (like how to store/link all the different language files)
You have some options, and each has its own advantages.
If you have a web app, which should not be indexed by search engines, then you are free to do whatever you want. You can keep a language setting in your Session and show strings in the chosen language. This will simplify URLs and management.
However, if you have a standard website which should go into Google, then your options are restricted. If you use the former approach, google will be confused and will index only one language, or, worse, make an ugly mix of the languages. Google does not keep sessions when indexing your page, so if you have two versions of the same page in two different languages, they need to have different URLs. And passing a language as a GET parameter each time is ugly, error prone, and not user friendly.
So you should either have languages as folders (eg. site.com/en/), which is the best options, or use subdomains. This can be a problem, however, because each subdomain is indexed as if it were a separate website, so things like pagerank and site reputation are split among the two.
Here are my recommendations based on multi-languages web sites experience
You can determine the language of the browser in Javascript (see this), which is a good start in having a language that likely fits the user demand
Use utf8 for encoding, everywhere (browser headers, programming, database). The only drawback I know of utf8 is that in some cases, the number of bytes it takes is bigger than another encoding matching more closely a given language (utf8 is opened to any). Big advantage, it covers any language, the ASCII part (bytes < 128) is the same as a western encoding (takes only one byte) ...
Store the user preferences (not in URL).
either in a cookie (+ : user does not have to be logged it on his computer to keep her prefs ; - : when user accesses the page from another computer, the cookie is not present and he/she has to reselect the pref).
or in a session (requires user login to deterime which user is currently using the site)
Regarding the site structure, if you don't store the user preferences, the common /us /fr /jp... solution has advantages: search engines robots will find all languages from root (the page display doesn't depend on user choice). Or you could load only a language dependent Javascript that displays the page immediately (after JS download) in the language of choice, without the need of a page reload / redirect.
You can tell Google what the parameters in the URL mean through Google's Webmaster Tools. So if you use a standard convention like index.php for the main page and a parameter like ?lang=fr for let's say French content in regard to SEO as long as it's a real translation. Likewise when Google.fr crawls the site you would present their users with the French version of the page based on their default setting in their browser. This will increase stickiness on the site and then increase the rank for the selected search terms in French. You can check their default language with this in PHP to make it less heavy on the user's end:
<?php
$lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
echo $lang;
?>
Then you would simply concatenate the language selected (as an overlay) to the end of the query string (if it's not the default), bypassing the need for storing a cookie. This should also help from a user experience standpoint. It's not as "pretty" but if people aren't typing it in, it's much more user friendly in that:
People can switch languages by changing the variable
People can provide a link to a user in another country and they can be presented with the page in their native language intuitively when PHP checks their language. "Do you want to see this content in French?"
The native language could in theory be translated on the fly with something like the Google Translate Widget or Google Chrome's built-in functionality.
Content is only served on one URI per page meaning you would not have to rewrite several redirects between versions in the event of stale content. As you're not likely translating into both languages at the same time.
From Wikipedia on Chrome:
As of February 2013, according to StatCounter, Google Chrome has a 37%
worldwide usage share of web browsers making it the most widely used
web browser in the world.
As for storing the files, you can make separate tables in the database for each language, enter in the content in the native language, then duplicate that content to the other language's table. Then you could translate that content live on screen if so desired, or allow someone access to your CMS who could translate the content. Both records would have the same ID in the database so the page would be served appropriately when the language was looked up.
Large corporate sites do the handling of the URLs in a couple of ways, they would show french content at http://www.some_website.fr/ and English content at http://www.some_website.com/. Sites like wikipedia use a subdns so http://fr.wikipedia.org/ and http://en.wikipedia.org.
HTML5 supports the lang='fr' approach to declaring content on screen.
I don't know what's your situation but, imho, you shouldn't use new urls.. it would only be a useless waste of resources (all kinds of resources).. If your website's client-side is javascript-based, you could use libraries like i18next, which gives you great support in localizations.. That's a valid alternative if you agree leaving the localization at you application's client-side.
For server-side localization in php, i wouldn't be of any help..

Website translation onload

I have a dating site which is currently in English language.
As per client requirement he told me to convert the website in Polish language.
If I will create language file for the website, then I need to rework on whole project.
Is there any technique so that my website is automatically translated into Polish language when it is loading in browser. There also some hidden div's that will be displayed when a specific event occurs.
The website is created in php with smarty templating.
Please help me for this.
Thanks
If you want a custom solution, you're going to have to do some editing of your current code.
Option 1
Store all your strings in YAML files or within a database, or flat file, or however else you want them stored. The fact of the matter is that, as soon as your application loads, it'll query the database for the strings in your language (your url will have /en, /fr, /de etc at the end) and place those onto your page.
This means you'll have to code the retrieval of this data, and placement within your application.
Option 2
Use Google's "Auto Translate" as per Dainis' suggestion in the comments. This isn't as customisable, but means you don't have to do any reworking of your current code.
Option 3
Use Bing's translate API and manually request strings for the data you want translated on each request. Access to the Bing API is free for developers and has loads of free translations included - but this will be data intensive unless you cache the translations for future use.
With Option 3, you'll still have to code your own custom solution. So if you're on a tight deadline or you're just plain lazy, go with Option 2.

a bilingual application on Agile Toolkit ? with language selector for the user?

I am planning on developing a bilingual PHP app with Agile Toolkit, with a language selector on every page ( French & English ) .. Can ATK4 accommodate this ? I am thinking yes .. but .. how would a newbie like me go about this with ATK4 ?? any cues would be greatly appreciated... I wish to create one single file ( some kind of database file that holds the translations) and access everything through variables for the various page views ( even the menu items ). Any assistance or cues on this will be greatly appreciated.
thx
Don
PHP already have the localization function, and all you need to do is make sure that all the internal messages of Agile Toolkit is being routed through the localization function. Agile Toolkit's API contains a _() method too. You need to re-define it inside your "lib/Frontend.php" and route it either through the get text or SQL, whichever way you want.
The other big part of translation you will need to perform is template translation. Templates usually contain graphics and texts which may be different depending on language. The "PathFinder" is ideal for this. Agile Toolkit website actually uses that technique for Polish translation:
https://github.com/atk4/atk4-web/blob/master/lib/AgileToolkitWeb.php#L8
https://github.com/atk4/atk4-web/blob/master/main.php
as you pass 3nd argument to the constructor, it instructs PathFinder to look into a different directory for the localized template files. If it does not find them there, it defaults to regular locations.

Multi languages site

I have a site with texts in 2 languages. How should I go between the pages so that from the point that he clicked to change the language he will see it and the moment he clicks the other language, it will change back?
An option is that when the user clicks the link to change the language I will store it into the session and use it. But I've heard it won't work well with the search engines.
A second option is to pass the language variable through the URL to every page.
Third option is to smartly use the zend extension ability.
(Using PHP + Zend Framework).
SEO is very important to me.
EDIT:
The texts that are in the site(and that are in several languages), are entered by an admin. It works now with languages objects that I've created with the option for the admin to add texts in each of them. So when I'll enter the first page(of which the text can be changed) I'll see it in English and when I click the Russian flag it will display that page(and the others later) in Russian.
Using Zend_Locale or translate won't work for me(I think), and passing through the URL is an option. The question is if it is the best one?
It is best to pass it along as a parameter in the URL, google uses locale=en for this. It would also be ok to store it in the session only if the user is logged into your website and selected a preferred locale or something. That way search engines can index the different locales with different urls, but your users would see pages in whatever locale they have chosen.
Zend Framework provides a pretty solid set of tools for this.
Start with Zend_Locale and Zend_Translate
It ought to be possible to tweak ZF's routing to contain a segment that indicates the locale. So you'd end up something like /:locale/:controller/:action (or similar). Your URLs would then look like http://example.com/en/some-controller/some-action.
Note that there's more than just translation of language. You can also localize the display of numbers, currency, dates, etc.
Come back with more specific questions once you've played with those.

Categories