i18n_get_translated()

Description

For field names / values using the i18n syntax, return the version in the current user's language. Format is ~en:Somename~es:Someothername

Parameters

ColumnTypeDefaultDescription
$text string|null
$lang_domain string|null null As an optional fallback if an i18n string is not provided, check the system language translations for a matching translation instead.

Return

string

Location

include/language_functions.php lines 48 to 102

Definition

 
function i18n_get_translated($text$lang_domain null)
{
    
$text ??= '';

    global 
$language,$defaultlanguage,$lang;

    
$asdefaultlanguage $defaultlanguage;

    if (!isset(
$asdefaultlanguage)) {
        
$asdefaultlanguage 'en';
    }

    
# Split
    
$s explode("~"$text);

    
# Not a translatable field?
    
if (count($s) < 2) {
        if (!
is_null($lang_domain)) {
            
# Search the provided $lang domain for a match and return that if present.
            
$key $lang_domain "-" preg_replace("/[^a-z0-9\-]/"''strtolower(str_replace(" ""-"$text)));
            if (
array_key_exists($key$lang)) {
                return 
$lang[$key];
            }
        }
        return 
$text;
    }

    
# Find the current language and return it
    
$default "";
    for (
$n 1$n count($s); $n++) {
        
# Not a translated string, return as-is
        
if (substr($s[$n], 21) != ":" && substr($s[$n], 51) != ":" && substr($s[$n], 01) != ":") {
            return 
$text;
        }

        
# Support both 2 character and 5 character language codes (for example en, en-US).
        
$p strpos($s[$n], ':');
        
$textLanguage substr($s[$n], 0$p);
        if (
strtolower($textLanguage) == strtolower($language)) {
            return 
substr($s[$n], $p 1);
        }

        if (
strtolower($textLanguage) == strtolower($asdefaultlanguage) || $p == || $n == 1) {
            
$default substr($s[$n], $p 1);
        }
    }

    
# Translation not found? Return default language
    # No default language entry? Then consider this a broken language string and return the string unprocessed.
    
if ($default != "") {
        return 
$default;
    } else {
        return 
$text;
    }
}

This article was last updated 14th July 2025 21:35 Europe/London time based on the source file dated 28th January 2025 09:35 Europe/London time.