Skip to content

functions_essentials_translation

Daniel Spors edited this page Feb 14, 2023 · 2 revisions

Functions in file essentials/translation.php

translation_init

Initializes the translation essential.

Definition: public function translation_init()

Returns: void

translation_do_includes

INTERNAL Includes the translation files

translation_add_function

Adds a custom translation function. Use this to add your own placeholder replacer function. Should accept a single argument which contains the string and return the ready string.

function my_trans($text)	
{	
return str_replace('{username}',"It's Me, Mario!",$text);	
}	
translation_add_function('my_trans');	

Definition: public function translation_add_function($func)

Returns: void

Parameters:

  • string $func Name of translation function

__translate_callback

INTERNAL preg_replace_callback handler

translation_add_unknown_strings

INTERNAL Used to add some new/unknown strings to the translation system

noTranslate

Ensures that a specific content remains untranslated. This may be useful when automatic translation would match one of you texts. Also very good to prevent user-input from beeing translated!

Definition: public function noTranslate($content)

Returns: string Returns the string containing attributes to ensure it will not be translated (NT)

Parameters:

  • string $content Content to remain untranslated

detect_language

Detects the users supposed language. Uses Localization::detectCulture to detect the users language.

Definition: public function detect_language()

Returns: string ISO2 code of detected language

translation_set_language

Sets the language and return the current one.

Definition: public function translation_set_language($code_or_ci)

Returns: string the previously set language

Parameters:

getStringLang

Like getString(), but for a specific language.

Definition: public function getStringLang($lang, $constant, $arreplace=null, $unbuffered=false)

Returns: string The translated string

Parameters:

  • string $lang Language to get string for

  • string $constant String to translate

  • array $arreplace Array with replacement data

  • bool $unbuffered If true skips buffering

_text

SHORTCUT for getString($constant, $arreplace, $unbuffered, $encoding)

getString

SHORTCUT for getStringOrig($constant, $arreplace, $unbuffered, $encoding)

getStringOrig

Returns a localized string from the current user's language. Replaces all placeholders in string from arreplace i.e. TXT_TEST => "this is a {tt}" with arreplace = aray("{tt}" => "test") => returns "this is a test" Buffers all strings on first access of this function.

Definition: public function getStringOrig($constant, $arreplace=null, $unbuffered=false, $encoding=null)

Returns: string Translated string

Parameters:

  • string $constant Text constant. i.e. TXT_...

  • array $arreplace Replacement array

  • bool $unbuffered Reload from session instead of from cache buffer of current script

  • string $encoding E.g. cp1252. Default "null" => UTF-8 will be returned

getJsString

SHORTCUT for getString but ensuring that it is escaped for use in JS

ReplaceVariables

INTERNAL Replaces variables in strings

getAvailableLanguages

Returns a list of all languages that have enough translated strings to be usable.

Definition: public function getAvailableLanguages($min_percent_translated=false)

Returns: array Array of language codes

Parameters:

  • int $min_percent_translated Specifies how many percent must be translated for a language to be 'available'

checkForExistingLanguage

Checks if there are translations for the given culture.

Definition: public function checkForExistingLanguage($cultureCode)

Returns: string|bool The given culture code or false

Parameters:

  • string $cultureCode Culture code to check for

translation_known_constants

Returns a list of all known constants.

Definition: public function translation_known_constants()

Returns: array List of all constants

translation_skip_buffering

INTERNAL Skips buffering for the current call

translation_string_exists

Checks if a string constant exists. You can use this to test if a string is a translation constant too.

Definition: public function translation_string_exists($constant)

Returns: bool true or false

Parameters:

  • string $constant Constant to check for existance

translation_is_valid_constant

Checks if the given text is a valid constant according to the translation regex pattern.

Definition: public function translation_is_valid_constant($constant)

Returns: bool

Parameters:

  • string $constant Text to be tested

translation_ensure_nt

INTERNAL Ensures that a string will not be translated

default_string

'Registers' a string in the translation system with a default value. This is used in ScavixWDF when components require user-interaction without forcing the implementor to create 100ths of strings as the first he must do.

Definition: public function default_string($constant, $text)

Returns: string The $constant value

Parameters:

  • string $constant Constant name

  • string $text The defauilt text

tds

SHORTCUT for default_string($constant, $text)

set_trans_data

SHORTCUT for clear_trans_data() followed by add_trans_data($name,$data)

add_trans_data

Adds data to the automatic translation system. Use this to add a bunch of data to the translation system. Sample is best to understand:

function SomeControllersInitMethod()	
{	
$str_const = default_string('TXT_TEST','Hello {user.name}! I want to tell you all about {product.name}. Is {user.email} your email address?');	
add_trans_data('user',UserModel::Make()->eq('id',1)->current());	
add_trans_data('product',ProductModel::Make()->eq('id',1)->current());	
$this->content($str_content);	
}	

Definition: public function add_trans_data($name, $data, $depth)

Returns: void

Parameters:

  • string $name Name of the data

  • mixed $data The data do add. Can be int, bool string, object, array, ...

  • int $depth Current recursion depth. Ignore this, it's internal only.

clear_trans_data

Clears the translation data store.

Definition: public function clear_trans_data()

Returns: void

Clone this wiki locally