![]() ![]() Unfortunately, gettext doesn’t play very smoothly with PHP so I'd recommend using a framework. Fortunately, you don’t have to try to solve the problems with homemade code. It can be used to complement gettext or frameworks that lack some of the functionality. Intl is a set of convenient helpers for formatting dates, time, numbers and working with currency. Creating Multilingual PHP Apps with Fat-Free Framework (F3)Īnd of course, let's not forget about Laravel, one of the most popular PHP frameworks:.The Ultimate Symfony Tutorial on Internationalization.Check out our other tutorials to see what that could potentially look like: The localization modules of these frameworks can be used as a standalone tool without a lot of overhead code from the framework itself. PHP Arrays, CSV, TBX/TMX, gettext, Qt, XLIFF, INI. Some offer additional features such as classes for currency and date/time formatting. FrameworksĪll major PHP frameworks have built-in support for creating translations. Gettext can handle pluralization but it has no tools for working with numbers, currency, date/time formats. Using the example from above, I create a plural msgid: m sgid "upload" The gettext system supports plural forms. Then the message with the id ‘hello’ is echo’ed which will output the german “Hallo”. This script creates a new gettext enviroment using the de_DE locale. This can be done using the msgfmt command line utility: msgfmt messages.po -o messages.mo I also create a messages.po for the de_DE locale: msgid "hello" Using just a text editor I create en/LC_MESSAGES/messages.po: msgid "hello" po files, a simple plain-text file format. I create the following directory structure: index.php In this example, i want to use English and German. php-gettext it is a drop-in replacement for PHP enviroments where the gettext extension isn’t installed. Or perhaps you are on a hosting plan where the gettext extension isn’t available. If you are running a stock Ubuntu VPS, gettext will only support the locales installed on the machine. Using gettext with PHP can be tricky in some setups. It is widely used and is the de-facto standard for localization in many programming languages. The GNU gettext system has been around for more than 20 years. For example, the pluralization of the world “file” (plik) in the Polish language works like this: 1 plik In the English language appending a ‘s’ is enough, but pluralization works differently in other languages. Not only does this clutter up the code, it doesn’t really solve the problem. What if it was only one file? Maybe do this: 1 file(s) uploaded successfully Consider an application that displays a notification when the user uploads files: 3 files uploaded successfully This approach is simple and easy to use, but it quickly reaches its limits. The appropriate language file is loaded at the beginning of your application code: Translatable strings are stored in an associative array, one file per language. So, let’s talk about PHP Arrays, gettext, frameworks, and Intl.Īssociative Arrays have been the primitive approach of many big and small PHP projects for a long time. Programmers often underestimate the complexity of localization and get stuck with homemade code that is a pain to maintain. Other issues to consider are date and time formats, currency symbols and pluralization. Despite alternative web programming languages like Ruby (on Rails), Python or NodeJS dominating the industry headlines, PHP remains a popular choice for building web applications.ĭespite this fact, there is still no real standard solution for localizing PHP applications.īuilding a truly international application is not just about translating strings. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |