Implementing Multi-Language Support

Share this article

Setting up a multilingual site may be a good way to attract new customers to your business or gain more participants in your project. Translating a simple site with a few static pages probably won’t probably be complicated, but more complex PHP web applications may require a lot of work when launching multiple language support. In this article I’ll present different types of content that need to be taken under consideration when internationalizing a site. Read on to get to know how to handle translating them into different languages.

Multi-language Static Content

First of all, your site probably contains text content hard coded in your project files that needs to be translated into another languages. Text strings in template files or notification messages that are being handled in a PHP script are examples of such content. If you didn’t plan on going international when writing these parts of code, you will have to walk through each line and handle translating the text strings stored there.

But how should the actual translations be done? The common way of handling multiple language content is to use Gettext – a software solution created to handle translations in applications written in different programming languages. It is also available in PHP as a separate extension. Using Gettext allows you to separate the app translations from the source code. In result, the person responsible for preparing the translations doesn’t have to dig into the code and can work independently from a web developer. When the translations are ready, they are being put in a separate file which is being read by the PHP script. The application matches the translations with source text strings stored in the code and finally the end user can see a site being displayed in the right language.
What you need to do as a developer is to transform plain text strings in a PHP application into strings that can be handed by Gettext. You have to wrap the text in the gettext() function which is commonly accessed using the alias _() (the function name is an underscore). By doing this, you specify that a certain text string needs to be translated and can be handled by Gettext.
If you want to get to know the details on how to manage Gettext translations in a PHP application, there is a complete tutorial explaining the subject in details, written by Abdullah Abouzekry: Localizing PHP Applications “The Right Way”. I encourage you to read it to see some practical examples of the logic I’ve described above.
Also remember that if you’re using a framework as a base for your PHP application, it probably has a component responsible for managing the translations. Review your framework’s documentation to check if there are any tools that simplify handling the multiple language support.

Database content

In more complex applications, a large part of the site’s content may be stored in a database. If the site supports only one language, you just need to save one version of a resource which is being fetched when needed. But implementing a multiple language site requires you to change the way of storing the data in a database. Language-specific resources now have to be identified by a language code and have to be fetched in the language version set by a visitor. What it means to you as a developer, is that you will probably have to change your database structure to be able to handle the translations.

To get to know what are the best practices on how to create a database structure for a multiple language site, you may just look at the solutions that are being used by different frameworks. Some frameworks offer behaviors (modules that provide an additional functionality to models) responsible for handling the translations of a specified model. After attaching a translation behavior to a model, the framework will write and read different language versions of data using a database structure that is capable of handling multiple language support.

For example, the CakePHP framework offers the Translate behavior that may be attached to your models. To do so, you need to specify which model fields are language-dependent. Let’s imagine you have the articles table which has two such fields: title and text:

To store the translations, you need to move these language-dependent fields into a separate translations table. Each row in the translations table is identified by the language version, the model and the field name referring to the base table. So the articles table will now look as follows:

The article title and text has been moved to a separate table that holds the translations. As you can see, a single translation of each of these fields is stored in a separate row:

What is most important, the CakePHP framework handles the database reads and writes of the translated data automatically. After attaching the Translate behavior, you can call your model methods just as before and the framework links the main table with the translations table itself. You don’t have to worry about making joins in your select statements or inserting data into multiple tables when saving a new row. Just browse the documentation to see some code examples of handling translations this way.

Other frameworks often offer similar solutions like the one described above. If you’re using the Symfony2 framework with the Doctrine ORM, just check the Doctrine translatable behavior to see an example. If your site doesn’t run on any framework, you can refer to the solutions described above when implementing your own way of handling the translations in a database.

User submitted content

If your site allows the visitors to write comments or reviews connected to a post or a product, you will have to handle the translations of such content as well.

First of all, you need to save the language version of the content entered by the user when he/she submits a form. You will probably just have to assume that it equals to the language version of the site set by the user. You can also employ an external API (e.g. the Google Translate API) to detect the language of a specific text.

Then, you need to translate the given text into all the language versions that are supported by your site. Instead of doing it manually, you can just use an external API that will provide you with machine translations of the content submitted by the visitors of your site. The ProgrammableWeb site lists over 60 translation APIs available over the Web so you will probably easily find a solution that suits your needs. If you’re looking for a specific tutorial on how to implement a translation API in an PHP application, I encourage you to read my articles on the Google Translate API:
Using Google Translate API with PHP which explains the basics on integrating a PHP script with the API,
Auto-translating User Submitted Content Using Google Translate API, which contains a complete code example on how to handle translating user submitted content on a PHP website.

Of course you may choose not to translate the user submitted content and display it in the original language versions. But in my opinion displaying machine translations is better than displaying content in a different language, even if the translations fetched from the APIs aren’t perfect.

Resources

As your site probably consists not only of the text content, you will also have to handle the translations of various resources that are being shared in your webpages. Images, videos, attachments, PDF files – all of these types of content have to be handled as well. To simplify the way of displaying the proper version of a file at a webpage, you can store all the files in a directory structure that reflects the languages available on the site. The English version of a file will be stored under en directory, the French version under fr and so on. Then you can write a simple helper method that will fetch the proper file basing on the language version that is currently being set by a user. If there is a default version of a specific resource, you can extend the method and fetch the default file if the language-specific version hasn’t been found.

Other types of content

Handling the translations of the types of content described above is often not sufficient to end up with a site that supports different language versions completely. Remember that a PHP website often includes other sources of content. JavaScript code that modifies the DOM tree or displays notification messages often contains text strings that have to be translated. Also don’t forget setting the proper language version when using external APIs or widgets that are attached to your webpages (e.g. social plugins). Browsing through your site before implementing multiple language support may provide you with more types of content that have to be taken under consideration when preparing translations.

Summary

As you can see, launching a site that offers multiple language support requires you to handle translating different types of content in different ways. If you plan to translate your site, I encourage you to make a checklist of the types of the translations that need to be done. It may serve as a good starting point for assessing the amount of work that needs to be done and implementing the translations.

If you have any questions or comments regarding the article, feel free to post them below. You can also contact me through Google Plus.

Frequently Asked Questions on Implementing Multi-Language Support

What are the key considerations when implementing multi-language support in PHP?

When implementing multi-language support in PHP, there are several key considerations to keep in mind. Firstly, you need to decide on the languages you want to support. This decision should be based on your target audience and the languages they speak. Secondly, you need to consider the structure of your website. You can either create separate pages for each language or use a single page with language-specific content. Thirdly, you need to consider the translation process. You can either manually translate the content or use automated translation tools. Lastly, you need to consider the impact on SEO. Multi-language websites can potentially improve your SEO, but you need to ensure that each language version is properly optimized.

How can I use gettext for multi-language support in PHP?

Gettext is a powerful tool that can be used for implementing multi-language support in PHP. It works by using a system of translation files, which are then compiled into binary format for efficient use by the gettext functions. To use gettext, you first need to install it on your server. Then, you can create .po files for each language you want to support, containing the translations for each string in your website. You can then use the gettext functions in your PHP code to display the translated strings.

What are the potential challenges when implementing multi-language support in PHP?

Implementing multi-language support in PHP can present several challenges. One of the main challenges is managing the translation process. This can be time-consuming and costly, especially if you need to support multiple languages. Another challenge is ensuring that the website structure supports multiple languages without negatively impacting the user experience or SEO. Additionally, you need to consider the impact on website performance, as loading multiple language versions can potentially slow down your website.

How can I optimize my multi-language website for SEO?

Optimizing a multi-language website for SEO involves several steps. Firstly, you need to ensure that each language version is accessible to search engines. This can be done by using hreflang tags, which tell search engines which language you are using on a specific page. Secondly, you need to optimize each language version for relevant keywords. This involves conducting keyword research for each language and optimizing your content accordingly. Lastly, you need to ensure that each language version has high-quality, unique content. Duplicate content can negatively impact your SEO, so it’s important to avoid simply translating the same content into different languages.

Can I use automated translation tools for my multi-language website?

Yes, you can use automated translation tools for your multi-language website. However, it’s important to keep in mind that while these tools can be useful for providing basic translations, they may not always provide accurate or culturally appropriate translations. Therefore, it’s often recommended to use a combination of automated translation tools and human translators to ensure the quality of your translations.

How can I manage the translation process for my multi-language website?

Managing the translation process for a multi-language website can be challenging, but there are several strategies you can use. One approach is to use a translation management system, which can help you manage and track the translation process. Another approach is to work with professional translators or a translation agency. This can ensure the quality of your translations, but it can also be more costly. Alternatively, you can use community translation, where you invite your users to help translate your website. This can be a cost-effective solution, but it requires careful management to ensure the quality of the translations.

How can I test my multi-language website?

Testing a multi-language website involves several steps. Firstly, you need to test the functionality of the language switcher to ensure that users can easily switch between different language versions. Secondly, you need to test the translations to ensure that they are accurate and culturally appropriate. This can involve working with native speakers of each language. Lastly, you need to test the website performance to ensure that loading multiple language versions does not slow down your website.

How can I maintain my multi-language website?

Maintaining a multi-language website involves regularly updating the translations, testing the website functionality, and optimizing for SEO. This can involve working with translators, conducting user testing, and regularly reviewing your SEO strategy. It’s also important to monitor your website analytics to understand how users are interacting with each language version and make adjustments as necessary.

Can I use PHP frameworks for multi-language support?

Yes, many PHP frameworks provide built-in support for multi-language websites. This can make it easier to implement and manage multi-language support. However, it’s important to understand the specific features and limitations of each framework before deciding which one to use.

How can I improve the user experience on my multi-language website?

Improving the user experience on a multi-language website involves several strategies. Firstly, you need to ensure that users can easily switch between different language versions. This can be done by using a prominent language switcher. Secondly, you need to ensure that the translations are accurate and culturally appropriate. This can involve working with native speakers of each language. Lastly, you need to consider the design and layout of each language version. Different languages can have different reading patterns and design conventions, so it’s important to adapt your design accordingly.

Jacek BareckiJacek Barecki
View Author

Jacek is a web developer specialized in building extensive web applications, mainly e-commerce solutions. The technologies he uses on a daily basis include PHP, MySQL, HTML+CSS and JS+jQuery. During the last few years he was the head developer of a highly customized online store platform in Poland. Now he's working on the development of several e-commerce websites running in Poland and Germany, often having hundreds of thousands pageviews a day. To take a break from coding, he does challenging crossfit workouts, goes out to taste some new food or dives into an interesting psychology magazine or book.

gettexti18ninternationalizationl10nlocalizationPHPtranslation
Share this article
Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week