Internationalization And Localisation
The process of creating a software program that can be readily translated into different languages and geographical areas without requiring significant modifications to the core code is known as internationalization (I18n). It includes externalising strings, icons, and pictures, making your program compatible with other character sets, and enabling locale-specific formatting features (such dates, currencies, and numbers) to be shown.
By adding locale-specific elements and translating text, localisation (L10n) is the act of modifying internationalised software for a particular area or language. Many people think that localisation is just translation. Localisation comprises setting up the software to deal with the unique customs of a target area, even though content translation is a component of localisation.
Localisation is essentially made feasible by internationalization. You prepare your program for multiple locales by first internationalising it, and then you localise it for particular ones.
A locale is a collection of parameters that specify language-specific settings including currency formats, date/time formats, number formats, and character attributes like capital/lowercase mapping. Typical locale identifiers are a two-letter lowercase language abbreviation, followed by a two-letter uppercase country code and an optional underscore. En_US means English in the US and de_DE means German in Germany. Many locales govern language-dependent attributes, including LC_ALL (all aspects), LC_COLLATE (string comparison), LC_CTYPE (character classification and conversion), LC_MONETARY, LC_NUMERIC, and LC_TIME.
PHP supports localisation and internationaliation through a number of extensions and functions. For certain categories or all categories (LC_ALL), the current locale can be set or queried using the setlocale() function. PHP functions such as mb_strtoupper() and mb_strtolower() from the mbstring extension can handle uppercase/lowercase conversion for different characters by considering Unicode properties; however, depending on the locale, the standard function library of the operating system should also support this. Dates can be formatted using date and time functions that take locale into consideration. Careful PHP handling is necessary when formatting monetary and numerical data according to the locale.
PHP offers an API layer to the GNU gettext package called gettext() for processing translatable text messages. The GNU gettext system was created to facilitate the creation of programs that communicate with users in the language of their choice. Finding a particularly mentioned string in the code (such as message ID or msgid) and substituting it with a locale-specific translation (such as message string or msgstr) is how it operates. Translation tables are used for this, and they are usually found in files ending in.po (editable Portable Object) and.mo (binary Machine Object).
To use gettext() in PHP:
- Setlocale() sets locale. Putenv() may also be needed to set LC_ALL.
- Using bindtextdomain(), determine where the binary translation files (.mo files) are located and bind them to a “domain” (a name for your message catalogue).
- Textdomain() is used to attach the application to the domain.
- Wrap the strings you wish to translate in gettext() calls. The gettext() function is frequently aliased to _() for conciseness.
Example:
<?php
// Set language to French
putenv('LC_ALL=fr_FR');
setlocale(LC_ALL, 'fr_FR');
// Specify location of translation tables for 'messages' domain
bindtextdomain('messages', './locale');
// Attach to domain
// Perform translation
// Example: echo gettext("How are you?"); // Should output "Comment allez-vous?" if translation exists
// Example: echo _("Welcome"); // Using the common alias for gettext()
?>
It is easier to manage localisation if locale-specific files are arranged in locale-named subdirectories and prefixed to PHP’s include_path. Another method is to utilise objects to hold the message catalogue for a language. This might allow message catalogues for related languages (like Canadian English from American English) to extend from a base object and only alter the words and phrases that are required.
PHP is compatible with both single-byte and multi-byte character sets. The iconv and mbstring extensions offer conversion capabilities between several character sets, such as UTF-8 and ISO-8859-15 (Latin 9). This mbstring extension contains multi-byte string operations such as mb_substr() and mb_strpos().
Performance Optimization
Improving the performance of PHP scripts is essential for scalability and efficiency. It is best to begin performance optimisation during the design phase. Avoid optimising code too late because it might result in problems, side effects, and more difficult-to-read and maintain code. Generally speaking, optimising code takes longer to develop and makes it harder to read. Thus, make sure your code is operating correctly first, and then look for “bottlenecks” or slower areas. When a user feels that a page is taking too long to load, optimisation is required.
The provide a range of performance optimisation strategies:
- Caching: Utilising caching techniques is said to be among the best methods for boosting scalability and speed.
- External Performance Tunings: Compiler caches, optimisers, HTTP accelerators, reverse proxies, proxy caches, and operating system tuning are examples of external performance tunings. PHP accelerators such as Zend Performance Suite and APC (Advanced PHP Cache) are mentioned. Content compression, compiled code caching, dynamic content caching, and automatic optimisation are all features of Zend Performance Suite.
- Data Component Caching: This article discusses using caching techniques in the PHP code itself. Caching data components may be necessary for this.
- Output Caching: PHP script output caching is discussed. PHP‘s output buffering routines, such as ob_start(), can be used for this. Another method is output compression, which may be accomplished with ob_gzhandler and output buffering.
- Templating Engine Caching: Some templating engines, such as HTML_Template_Xipe, cache the final output (HTML) or compile templates into PHP files.
- Optimizing Code: Methods for identifying various code-related improvements.
- Micro-Benchmarks: Writing brief tests to gauge the effectiveness of particular code strategies, such as contrasting str_replace() with preg_replace(), is known as micro-benchmarking. This provides an answer to the question of the fastest method.
- Rewrite in C: The efficiency of particularly important code areas can be enhanced by rewriting them as PHP extensions in C.
- OO Versus Procedural Code: Assessing the effects of switching between the two on performance.
- Simple Optimizations: The optimisation of general code is mentioned.
- Profiling: Employing instruments to pinpoint application bottlenecks. Profilers are useful for identifying the most time-consuming code segments. For profiling, programs like Xdebug and APD (Advanced PHP Debugger) are utilised.
- The declare(ticks=n) structure is useful for debugging and profiling. It permits a certain function to execute every n lines of code in a block.
- An example (using declare(ticks)):
- Note that this necessitates the definition and registration of a function someFunction, as detailed in the source.
- Database Optimization: Optimising database queries and table architecture improves application speed.
- Server and Operating System Tuning: Optimising the operating system and web server settings (Apache, Nginx) improves performance. This optimises the HTTP server by merging and lowering CSS and JavaScript files to reduce HTTP requests.
In summary
Optimisation Performance involves code optimisation, benchmarking and profiling to detect bottlenecks, strategic design, effective caching, and fine-tuning the database, web server, and operating system.