language_functions.php
Table of Contents
Functions
- lang_or_i18n_get_translated() : string
- Translates field names / values using two methods: First it checks if $text exists in the current $lang (after $text is sanitized and $mixedprefix - one by one if an array - and $suffix are added). If not found in the $lang, it tries to translate $text using the i18n_get_translated function.
- i18n_get_translated() : string
- For field names / values using the i18n syntax, return the version in the current user's language. Format is ~en:Somename~es:Someothername
- i18n_get_collection_name() : string
- Translates collection names
- i18n_get_indexable() : string
- For field names / values using the i18n syntax, return all language versions, as necessary for indexing.
- i18n_get_translations() : array<string|int, mixed>
- For a string in the language format, return all translations as an associative array E.g. "en"->"English translation"; "fr"->"French translation"
- str_replace_formatted_placeholder() : string
- Returns a string with all occurrences of the $mixedplaceholder in $subject replaced with the $mixedreplace.
- ucfirstletter() : string|array<string|int, mixed>|null
- Returns a string with the first LETTER of $string capitalized.
- ucfirstletter_callback() : void
- Callback used by ucfirstletter
- normalize_keyword() : string
- Normalize the text if function available
- remove_accents() : string
- This function and seems_utf8 are reused from WordPress. See documentation/licenses/wordpress.txt for license information
- seems_utf8() : bool
- Looks for particular patterns to attempt to determine if the provided string is in UTF8 format
- http_get_preferred_language() : string
- Use the browser settings to determine the default / preferred language
- setLanguage() : string
- Set the user's current language based on get/post/cookie values as appropriate.
- lang_load_site_text() : void
- Load all site text for the given page and language into the global $lang array
- i18n_get_all_translations() : array<string|int, mixed>
- Return an array of all available language strings for the given id, with the language code as the key
- i18n_merge_translations() : array<string|int, mixed>
- Merge values for across multiple translation strings.
- get_display_language() : string
- Get the name of a language in your own language plus its native name.
- is_i18n_language_string() : bool
- Simple check to determine if the text string supplied contains i18n language translations.
Functions
lang_or_i18n_get_translated()
Translates field names / values using two methods: First it checks if $text exists in the current $lang (after $text is sanitized and $mixedprefix - one by one if an array - and $suffix are added). If not found in the $lang, it tries to translate $text using the i18n_get_translated function.
lang_or_i18n_get_translated(string $text, string $mixedprefix[, string $suffix = "" ]) : string
Parameters
- $text : string
- $mixedprefix : string
- $suffix : string = ""
Return values
stringi18n_get_translated()
For field names / values using the i18n syntax, return the version in the current user's language. Format is ~en:Somename~es:Someothername
i18n_get_translated(string|null $text[, string|null $lang_domain = null ]) : string
Parameters
- $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.
Tags
Return values
stringi18n_get_collection_name()
Translates collection names
i18n_get_collection_name(mixed $mixedcollection[, string $index = "name" ]) : string
Parameters
- $mixedcollection : mixed
- $index : string = "name"
Return values
stringi18n_get_indexable()
For field names / values using the i18n syntax, return all language versions, as necessary for indexing.
i18n_get_indexable(string $text) : string
Parameters
- $text : string
-
The text to process
Return values
stringi18n_get_translations()
For a string in the language format, return all translations as an associative array E.g. "en"->"English translation"; "fr"->"French translation"
i18n_get_translations(string $value) : array<string|int, mixed>
Parameters
- $value : string
-
A string in the language format
Return values
array<string|int, mixed>str_replace_formatted_placeholder()
Returns a string with all occurrences of the $mixedplaceholder in $subject replaced with the $mixedreplace.
str_replace_formatted_placeholder(mixed $mixedplaceholder, mixed $mixedreplace, mixed $subject[, mixed $question_mark = false ][, string $separator = ", " ]) : string
If $mixedplaceholder is a string but $mixedreplace is an array, the $mixedreplace is imploded to a string using $separator. The replace values are formatted according to the formatting of the placeholders. The placeholders may be written in UPPERCASE, lowercase or Uppercasefirst. Each placeholder will be replaced by the replace value, written with the same case as the placeholder. It's possible to also include "?" as a placeholder for legacy reasons.
Example #1: str_replace_formatted_placeholder("%extension", $resource["file_extension"], $lang["originalfileoftype"], true) will search for the three words "%EXTENSION", "%extension" and "%Extension" and also the char "?" in the string $lang["originalfileoftype"]. If the found placeholder is %extension it will be replaced by the value of $resource["file_extension"], written in lowercase. If the found placeholder instead would have been "?" the value would have been written in UPPERCASE.
Example #2: str_replace_formatted_placeholder("%resourcetypes%", $searched_resource_types_names_array, $lang["resourcetypes-collections"], false, $lang["resourcetypes_separator"]) will search for the three words "%RESOURCETYPES%", "%resourcetypes%" and "%Resourcetypes%" in the string $lang["resourcetypes-collections"]. If the found placeholder is %resourcetypes% all elements in $searched_resource_types_names_array will be written in lowercase and separated by $lang["resourcetypes_separator"] before the resulting string will replace the placeholder.
Parameters
- $mixedplaceholder : mixed
- $mixedreplace : mixed
- $subject : mixed
- $question_mark : mixed = false
- $separator : string = ", "
Return values
stringucfirstletter()
Returns a string with the first LETTER of $string capitalized.
ucfirstletter(mixed $string) : string|array<string|int, mixed>|null
Compare with ucfirst($string) which returns a string with first CHAR of $string capitalized: ucfirstletter("abc") / ucfirstletter("%abc") returns "Abc" / "%Abc" ucfirst("abc") / ucfirst("%abc") returns "Abc" / "%abc"
Search for the first letter ([a-zA-Z]), which may or may not be followed by other characters (.*). Replaces the found substring ('$0') with the same substring but now with the first character capitalized, using ucfirst(). Note the /e modifier: If this modifier is set, preg_replace() does normal substitution of backreferences in the replacement string, evaluates it as PHP code, and uses the result for replacing the search string.
Parameters
- $string : mixed
Return values
string|array<string|int, mixed>|nullucfirstletter_callback()
Callback used by ucfirstletter
ucfirstletter_callback(array<string|int, mixed> $matches) : void
Parameters
- $matches : array<string|int, mixed>
normalize_keyword()
Normalize the text if function available
normalize_keyword(string $keyword[, bool $user_language = false ]) : string
Parameters
- $keyword : string
-
Keyword to normalize
- $user_language : bool = false
-
Flag to enable normalizing based on the current user language. Some languages consider characters with accents to be different characters and therefore order them after z and other consider them to be the same as the character without the accent. See https://www.php.net/manual/en/class.normalizer.php for more information
Return values
string —Normalized keyword
remove_accents()
This function and seems_utf8 are reused from WordPress. See documentation/licenses/wordpress.txt for license information
remove_accents(string $string) : string
Converts all accent characters to ASCII characters.
If there are no accent characters, then the string given is just returned.
Parameters
- $string : string
-
Text that might have accent characters
Return values
string —Filtered string with replaced "nice" characters.
seems_utf8()
Looks for particular patterns to attempt to determine if the provided string is in UTF8 format
seems_utf8(string $str) : bool
Parameters
- $str : string
Return values
bool —True if it's possibly UTF8
http_get_preferred_language()
Use the browser settings to determine the default / preferred language
http_get_preferred_language([bool $strict_mode = false ]) : string
Parameters
- $strict_mode : bool = false
Return values
string —The language string the user may prefer
setLanguage()
Set the user's current language based on get/post/cookie values as appropriate.
setLanguage() : string
Return values
string —The language string set
lang_load_site_text()
Load all site text for the given page and language into the global $lang array
lang_load_site_text(array<string|int, mixed> &$lang, string $pagename[, string $language = "" ]) : void
Parameters
- $lang : array<string|int, mixed>
-
Passed by reference
- $pagename : string
-
Pagename
- $language : string = ""
-
Language
i18n_get_all_translations()
Return an array of all available language strings for the given id, with the language code as the key
i18n_get_all_translations(string $langid) : array<string|int, mixed>
Parameters
- $langid : string
-
The identifier of the lang string
Return values
array<string|int, mixed>i18n_merge_translations()
Merge values for across multiple translation strings.
i18n_merge_translations(array<string|int, mixed> $values) : array<string|int, mixed>
Where a value is missing for a language used elsewhere i18n_get_translated will be used. If no value is provided and no English (en) string is found then blank will be returned.
Example input: ["~en:cheese~fr:fromage","~en:bread~fr:pain"]
Example output: ["~en:cheese, bread","~fr:fromage, pain"]
Parameters
- $values : array<string|int, mixed>
-
Translation strings to merge.
Return values
array<string|int, mixed> —Each language string as a comma separated list.
get_display_language()
Get the name of a language in your own language plus its native name.
get_display_language(string $language_key, string $language_value) : string
Used for language selection dropdowns.
Parameters
- $language_key : string
-
Key of $languages array, e.g. "fr"
- $language_value : string
-
The value (full name) from the $languages array, e.g. "Français"
Return values
stringis_i18n_language_string()
Simple check to determine if the text string supplied contains i18n language translations.
is_i18n_language_string(string $text) : bool
Returns true if match found e.g. '~en:test~en-US:test'. Returns false if no code is found. e.g. 'test'.
Parameters
- $text : string
-
Text string to check e.g. a node name or field value.