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?
-
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
To view a list of locales supported on your server:
Bash
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
<?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 output
function 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
-
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
Some locales require specific character sets. For example, Cyrillic characters may not display correctly unless you specify encoding:
<?php
header('Content-Type: text/html;charset=iso-8859-5');
?>
Best Practices
|