# Using the internationalization (intl) extension in PHP

> Use the PHP intl extension to format dates, numbers and currencies and sort text correctly for different locales.

Source: https://helpcenter.ruachost.com/developers/php/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?

- 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) or `zh_TW.big5` (Chinese, Taiwan using Big5 encoding).
- Format: `xx_YY` where `xx` = 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:**
- Use `utf8` or `utf8mb4` encoding for modern applications.
- Always validate locale availability with `locale -a`.
- Test formatting across multiple languages to ensure consistency.
- Combine intl functions with database collation settings for full multilingual support.
