Using the internationalization (intl) extension in PHP
The internationalization (intl) extension in PHP provides tools for formatting and processing locale‑specific information. On ruachost.com, the intl extension is enabled by default, allowing developers to build applications that support multiple languages, regions, and cultural conventions.
Why Use intl?
Section titled “Why Use intl?”- Format dates, times, numbers, and currencies according to locale.
- Handle text collation (alphabetical sorting rules differ by language).
- Convert between uppercase and lowercase letters in different scripts.
- Support multilingual applications with consistent formatting.
Checking Available Locales
Section titled “Checking Available Locales”To view a list of locales supported on your server:
locale -a- Output will include codes like
en_US(English, United States) orzh_TW.big5(Chinese, Taiwan using Big5 encoding). - Format:
xx_YYwherexx= language code,YY= country/region code.
Example: Setting Locales in PHP
Section titled “Example: Setting Locales in PHP”<?php// Set locale to French (France)setlocale(LC_ALL, 'fr_FR');printRow();
// Set locale to English (United States)setlocale(LC_ALL, 'en_US');printRow();
// Function to display formatted outputfunction printRow() { $number = 133.52; echo "<tr>"; echo "<td>" . setlocale(LC_ALL, 0) . "</td>"; echo "<td>" . strftime('%c') . "</td>"; echo "<td>" . $number . "</td>"; echo "</tr>";}?>Output
Section titled “Output”- In French:
133,52(comma as decimal separator). - In English:
133.52(period as decimal separator). - Date and time formats also differ by locale.
Handling Character Encoding
Section titled “Handling Character Encoding”Some locales require specific character sets. For example, Cyrillic characters may not display correctly unless you specify encoding:
<?phpheader('Content-Type: text/html;charset=iso-8859-5');?>