![]() To be on the safe side I’ve overridden its assumption with UTF-8 using the -from-code option. xgettext assumes any file is ASCII by default, and its output may contain unexpected results if the source strings contain any non-ASCII characters. This instructs xgettext to extract messages from all the PHP files in the current directory. Open a terminal window, go to the Test18N directory, and run the following command: xgettext -from-code=UTF-8 -o messages.pot *.php If you were to extract the strings by hand, you’d have to sort them and remove any duplicates when creating your translation file. And like a real-world app would have, you’ll notice that some messages are repeated in more than one file. Now you should have three files to emulate a slightly larger application. "" Īnd finally, create a new file named test_page_2.php with the following contents: " Įcho sprintf(ngettext("%d file", "%d files", 1), 1). "" Įcho dgettext("errors", "Error saving data"). Then, create a new file named test_page_1.php with the following contents: " Įcho dgettext("errors", "Error getting content"). Open test_locale.php and replace its contents with the following code: " xgettext is not PHP specific you can use it to extract strings from code written in over 15 popular programming languages, including C, C++, C#, Java, Perl, PHP, and Python to name just a few.īefore you begin, make sure your Test18N directory is up to date with the following structure created in the previous parts of this series. Xgettext is a command-line tool that is part of the gettext library which you downloaded and installed in Part 1… a very useful tool indeed! Its purpose is to simplify the extraction of strings from your source code and generate a domain template, thereby saving you time and hassle. ![]() Extracting strings for use as msgids and organizing them by hand is a daunting task, even with just a small codebase. But what about ongoing maintenance? As your application matures, text strings are sure to be added, updated, and removed. You’ve seen how powerful gettext is, and how easy it was to incorporate localization into your applications.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |