Current Location: Home> Latest Articles> Using end() to implement the default language fallback mechanism in multilingual arrays

Using end() to implement the default language fallback mechanism in multilingual arrays

M66 2025-06-02

When developing multilingual websites, you usually need to load the corresponding translated content based on the user's language selection. However, in some cases, the user may choose a language without a corresponding translation, or the content of that language is not found on a specific page. To ensure user experience, we can provide default language content through a fallback mechanism.

PHP provides the end() function, which can help us implement this mechanism easily. Next, we will use a simple example to show how to use the end() function to implement the default language fallback mechanism in a multilingual array.

1. Language array structure

Suppose we have a multilingual website where language data is stored in an associative array, and the data for each language is part of the array. We use a nested array to save translations for each language.

 $lang = [
    'en' => [
        'greeting' => 'Hello',
        'farewell' => 'Goodbye',
    ],
    'fr' => [
        'greeting' => 'Bonjour',
        'farewell' => 'Au revoir',
    ],
    'es' => [
        'greeting' => 'Hola',
        'farewell' => 'Adiós',
    ],
];

2. User selects language

Normally, the user will choose a language, which we can get from $_GET or $_SESSION . If the user does not have a selection, or the translation of the selected language does not exist, then we will fall back to the default language.

 $user_language = isset($_GET['lang']) ? $_GET['lang'] : 'en';  // Use English by default

3. Use the end() function to implement the fallback mechanism

When implementing the fallback mechanism, we can use the end() function to get the last element in the array, which will be the default language we set. First, we need to check whether the user's selected language exists, and if it exists, use the translation of that language; if it does not exist, fall back to the default language (such as English).

 // Check if the user selected language exists
if (isset($lang[$user_language])) {
    $selected_lang = $lang[$user_language];
} else {
    // If the user selected language has no translation,Fallback to default language(English)
    $selected_lang = $lang['en'];
}

// use end() Get the last translation item
$last_translation = end($selected_lang);

echo "The last translation: " . $last_translation;

4. Explain the code

  • end($selected_lang) : This function will move the pointer to the last element of the array and return that element. Through this method, we can ensure that the program can return a default translation item even if the user selects a language without translation content.

  • Check whether the user selected language is valid through isset() , and if it is invalid, it falls back to the default language en .

5. Process the translation content of the URL

If the translated content contains URLs, we can replace the domain name of these URLs where we need it to be m66.net . For example, we want to replace all link domains in the translation to make sure they point to the correct website.