Localization In PHP:
Localization is the process of adapting a product or content to a specific locale or market. It allows you converting the existing language of the website to the native language as per the preference. We have worked on several projects which had a special requirement of displaying the texts in their regional language. For ex. If a website is going to be published in Spain so customers want the text to be displayed in Spanish language along with the default English language depending on the specific locale or market.
Usually, programs are written and documented in English, and use English at execution time for interacting with users. So, we use a gettext which is used for localization and internationalization the language. As we have created a HTML page with PHP Script so we write a function for echoing (Display the content on User Interface) with using of gettext.
So, the next question could be how it works? We have created an English website using T_() function, the function which contains the English statement or words and then we have created a .PO file.
What PO file is:
It is translation file which contains the words which you have written in the function.
Suppose,you want to display a string 'Hello World' in 10 different languages, then you would need to write the string like this: T_(“Hello World”) and you need to make 10 different directory with same file name 'messages.po' which contains the word with the language you want. For each converted .po file, you need to add actual conversion of that word in .po file. So, for Chinese we have created a 'zh_CN' directory which have a 'message.po' file and that po file contains English word and the appropriate Chinese word. By using a program (In our case a Dropdown on web page) it will select a language directory and word with matching to that English word and display to user.
How it Works:
When we create a po file using of Poedit (Gettext Translations Editor) Software which is open source. It will compile it & create a mo file. i.e. 'message.mo' which is a binary file. So when we upload the localization on Production server then we don't need a po file. We need only mo file which is a binary file which can't read by end user. Only Computer can read & replace the word and create a HTML and pass to the User interface. So all the words and strings are hidden and secure if you give only a mo file to the Production server.
This all is an one side of the coin. To make it more automated, we have created a web scraper which will ask you which language you have and which language you want. If you provide the desired preference, this web scraper will take your .PO file and read all the texts that needs to be converted and puts in Google Translate. Whatever is the converted string/word, it puts in separate 'message.po' file for the required language. So with the help of this web scraper, you can change any language to any one within few minutes.