When you plan to achieve a wider audience for your website – SEO (Search Engine Optimization), is an important task. Translating your website is an additional, however essential step, to rank higher in the search engine and drive more traffic into your website.
While there is no standard way to translate a custom-built multilingual PHP website, there are various parts of your website that requires special localization configuration/implementation. Including, a backend (dynamic content from DB) and website interface (UI). This blog will collectively shed light on different aspects and implementation methods.
Internationalization usually is a one-time effort, but it is a challenging one and often underestimated by programmers. Various aspects need to be planned when structuring different components of the website/ app UI and the backend.
There are fundamental differences between languages that need to be taken into consideration when localizing your website. These include language nuances, date formats, grammar rules (such as pluralization).
For more tips on RTL localization, please visit localization for RTL languages.
Methods and Tools for Internationalization
For a multilingual website, you need to consider the following features:
– A language switcher
– Load translated content based on geolocation or browser language
– URL routing
PHP Array Files
PHP associative arrays are the easiest way to translate a site. Translated strings are stored in a file for each language.
The strings are then retrieved and displayed inline according to the selected language.
This approach has been used for a long time and seems to be simple however, it is hard to maintain with a larger app scale, and in a variety of languages.
For example, the pluralization for English works differently from other languages. In Arabic, this implies a challenge and would require some conditional switching which might clutter up the code.
Some frameworks that use this approach are, Laravel, CodeIgniter, Kohana, and Zend.
The classic GETTEXT function has been around for a long time, where it used to be considered a rule of thumb for the internalization of applications and websites in many programming languages. Using the extension may be a little problematic in PHP, where you cannot install the extension into your hosting environment. In this case, PHP-GETTEXT can be used.
Many frameworks support internationalization through GETTEXT like Yii, Zend, CakePHP, Drupal, and WordPress. So, it is better to rely on a framework to build a multilingual website, rather than building a custom website and handling it yourself in the code.
For a custom PHP website, you will need to set up your .PO files to store translations in plain text or use a tool to extract the strings for you. When the translations are completed, the .PO files need to be compiled into .MO files and stored in a translation directory.
Database Table Translation
Implementing a simple but efficient database design is key for performance and scalability.
Approach 1: For every translatable entity in the database, a table is created, and corresponding translated columns are added for each language.
This approach might work for small websites and few languages, but for large websites and 15 languages, this solution is not the best in terms of performance and scalability.
Approach 2: A table is created for each language.
This will work best with small websites and a couple of languages.
Approach 3: As you can see below, this is a simple translation implementation on the database that can be generalized to all translatable entities in the database. This approach is simple and scalable as we add new languages to the website, it will not require any alteration to the database schema. Also, the queries developed to extract content based on the language are not complex.
When building a website, it is useful to consider localization best practices from the beginning, especially for business that targets to expand globally.
Most CMS’s and frameworks have built-in localization capabilities that make a developer’s life easier to build a multilingual website out of the box. Although Python, Node.js, and Ruby on Rails, are widely used, flat PHP websites are still being preferred by many developers.