Adding a New Language
Tutorial: Adding a New Language
This guide explains how to add a new language (localization) to the application. The system is designed to be easily extensible. By following these steps, you can add any language and make it available to your users.
What You Will Do
We will add Spanish (`es`) as an example. The process involves three main steps:
- Add a flag icon for the new language.
- Create a new language file with the translated texts.
- Update the user profile page to include the new language in the selection dropdown.
Step 1: Add the Flag Icon
Flag icons are used for visual representation in the language switcher. The application looks for them in a specific folder.
- Directory: Place the new flag icon in the media/flags/ directory.
- File Name: The file must be named after the two-letter language code (ISO 639-1). For Spanish, the file name should be es.svg.
- Format: SVG is the recommended format for flags as it scales perfectly without losing quality.
Example: To add a flag for Spain, you would add the file media/flags/es.svg.
Step 2: Create the Language File
The language file contains all the translated strings (UI text) for the application. The easiest way to create a new one is to copy an existing file.
- Navigate to the languages/ directory.
- Copy the existing en.php file.
- Rename the copied file to match the new language code. For Spanish, this would be es.php.
- Open the new es.php file and start translating the values.
Example: languages/es.php
<?php
// Spanish language file
return [
// General
'my_website' => 'INtex Proyecto X',
'learn_more' => 'Aprende más',
'copyright' => '© %s INtex Publishing. Todos los derechos reservados.',
// This key is very important! It's the name shown in the dropdown.
'language_native_name' => 'Español',
// Sidebar Menu (menu.php)
'menu_home' => 'Inicio',
'menu_contact' => 'Contacto',
// ... translate all other keys ...
];
'language_native_name' is mandatory. Its value is the name of the language as it will appear in the selection dropdown (e.g., "Español", "Français", "Deutsch").
Step 3: Add the Language to the User Profile Dropdown
Finally, you need to tell the application that the new language is available. This is done by adding it to the language array in the user profile page.
Open the file userprofile.php and find the section where the languages are defined. Add your new language code to the $available_languages array.
<?php
// ... other code in userprofile.php ...
// Define available languages
$available_languages = ['de', 'en']; // Original
// --- Add your new language code here ---
$available_languages = ['de', 'en', 'es']; // Modified
// ... rest of the code ...
?>
Once you save this change, "Español" (or whatever you defined in language_native_name) will automatically appear as an option in the "Preferred Language" dropdown in every user's profile.
That's It!
You have successfully added a new language to the application. Users can now select it in their profile to change the interface language.