I18n (Internationalization)

Fuwafuwa Framework supports internationalization (i18n) to cater to users from different regions and languages, allowing you to create multilingual applications.

Internationalization, often abbreviated as i18n (because there are 18 letters between the first 'i' and the last 'n'), is the process of designing and developing your application so that it can be adapted to different languages and regions without requiring major engineering changes.

Fuwafuwa's i18n system is designed to be simple and intuitive, making it easy to create applications that support multiple languages. Whether you're building a small website for a local audience or a large application for a global market, Fuwafuwa's translation system has you covered.

✨ Enhanced Translation System

Fuwafuwa includes an enhanced translation system with named parameters, pluralization, fallback languages, and a web-based editor. Learn more →

Language and Locale

The framework uses a language and locale system to manage translations. This system allows you to support multiple languages and regions, each with their own translations.

Languages are identified by their ISO 639-1 two-letter codes, while locales add region-specific information using ISO 3166-1 alpha-2 country codes. This combination allows you to create language/region-specific translations.

The framework uses a language and locale system to manage translations:

  • Language code (e.g., en, id, fr) - Represents the language
  • Locale code (e.g., en_US, id_ID, fr_FR) - Specifies the region or country

Set the default language in app/configs/config.ini:

[APP]
lang = en
fallback_lang = en

Translation File Structure

Fuwafuwa organizes translation files in a logical directory structure that makes it easy to manage translations for multiple languages. Each language has its own directory, and within each language directory, translations are organized into files and subdirectories.

This structure allows you to group related translations together, making it easier to find and maintain them. You can create as many translation files as you need, and organize them into subdirectories to reflect your application's structure.

app/i18n/trans/
├── en/
│   ├── common.php
│   ├── navigation.php
│   └── forms/
│       ├── login.php
│       └── registration.php
└── id/
    ├── common.php
    ├── navigation.php
    └── forms/
        ├── login.php
        └── registration.php

Each language directory uses its ISO 639-1 two-letter code.

Translation File Format

Translation files in Fuwafuwa are simple PHP arrays with key-value pairs. Each file contains translations for a specific category or section of your application.

The PHP format is used because it allows for fast loading and parsing, and it also supports complex data structures if needed. However, for most cases, you'll only need simple key-value pairs.

Translation files are PHP arrays with key-value pairs:

// app/i18n/trans/en/common.php
<?php
return [
    'hello' => 'Hello, world!',
    'greeting' => 'Welcome to our website',
    'record_saved' => '{0} saved successfully',
];

Using Translations

Using translations in your Fuwafuwa application is simple and intuitive. The framework provides a global t() function that you can use to retrieve translations from anywhere in your application.

The t() function accepts a translation key in dot notation, where each segment of the key corresponds to a directory, file, or key in your translation files.

Basic Usage

Use the t() function with dot-notation keys:

<!-- In templates -->
<p>{{ t('common.hello') }}</p>
<p>{{ t('forms.login.username') }}</p>
// In controllers
echo t('common.greeting');
echo t('record_saved', 'User');  // Output: "User saved successfully"

Key Structure

Keys follow the directory structure:

File PathTranslation Key
en/common.phpcommon.key
en/forms/login.phpforms.login.key
en/navigation.phpnavigation.key

Translation Parameters

Translations often need to include dynamic content, such as user names, dates, or other variable information. Fuwafuwa supports both indexed and named parameters in translations, allowing you to pass variable content to your translations.

Legacy Format (Indexed)

// Translation file
'record_saved' => '{0} has been saved at {1}'

// Usage
echo t('record_saved', 'User', '14:30');
// Output: "User has been saved at 14:30"

Named Parameters Enhanced

// Translation file
'welcome' => 'Hello, :name! You have :count messages'

// Usage
echo t('welcome', ['name' => 'John', 'count' => 5]);
// Output: "Hello, John! You have 5 messages"

See the Translation System documentation for full details on named parameters, pluralization, and advanced features.

Web-Based Translation Editor

Fuwafuwa includes a browser-based translation management interface at /admin/i18n (requires admin authentication). This interface makes it easy to manage your translations without having to manually edit PHP files.

The web editor provides a visual interface for editing, adding, and deleting translations. It also supports searching and filtering translations, making it easy to find the translations you need to modify.

Fuwafuwa includes a browser-based translation management interface at /admin/i18n (requires admin authentication).

Editor Features
  • Visual editing of all translation files
  • Support for nested translation keys
  • Add, edit, and delete translations
  • Search and filter translations
  • Export to PHP files