Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

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 42 to 94

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 December 2024 20:05 Europe/London time based on the source file dated 27th November 2024 15:30 Europe/London time.