Copied!

To translate a string, call the trans function passing either the app native language, or tokens.

String starting with a period are assumed to be a token string ie '.welcome', '.welcome.footer', '.welcome.body' Other strings are assumed the base language

Tokened translations can be more effectively chunked, cached and managed directly in PHP Base language translations are stored in one big array, meaning everything is loaded into memory

Translator supports pluralization similar with Laravel, but with the count being passed in the variable array with an index of 'count'

CloneableInstantiable
Methods
public static clearCache() : void
 

Resets the cache, used by unit testing, as normally the app will not change languages for a single script, where as unit testing will.

public static getBaseLocale() : string
 

The base local is the native language of the app where some of the translation is stored as actual language fragments.

public static getInstalledLanguages() : array
 

Returns an plain array of languages that have been installed by the user.

  • return string[]
public static getTranslationDirectory() : string
 

Returns the current translation directory that should contain generated files with user strings

public static load(string $directory, string $file) : array
 

Loads a translation chuck from the file system for the specified directory and file.

  • return array<string,string[]>
public static setBaseLocale(string $baseLocale = '') : void
 

Set the base locale. Normally called once in the primary include file for a script.

public static setLocale(string $locale = '') : void
 

Set the locale for the translations to return

  • param string $locale can be any string, but the following are reserved:
    • invisible returns empty string for everything, useful for debugging to see untranslated text
    • TRANS returns 'TRANS' string, useful for debugging
    • RAW returns the actual string passed with no pluralization
    • a blank locale will not preform a translation and returns the actual string passed to trans with pluralization enabled
    Other valid locales would be returned by getInstalledLanguages
public static setTranslationDirectory(string $directory = '.') : void
 

Set the translation directory. Normally called once in the primary include file for a script.

public static setTranslationMissing(?PHPFUI\Translation\MissingLogger $missing = NULL) : void
 

If a translation string is not found, the "missing" method will be called on this object. Great for saving missing translations for later translation.

public static setTranslationService(?PHPFUI\Translation\ServiceInterface $serviceInterface = NULL) : void
 

Sets a translation service that is passsed the string to translate and a local. The service should not provide any pluralization.

public static trans(string $text, array $variables = []) : string
 

Translates a string based on the current locale

  • param string $text to be translated, can be chunked (.modules.general) or native (General Modules)
  • param array<string,mixed> $variables associative array of variables to substitute in returned translation string. Pass an index of 'count' for pluralization support.
  • return string of translated text
Properties
protected static string $baseLocale = ''
protected static string $directory = '.'
protected static ?PHPFUI\Translation\Tree $loadedTranslations = NULL
protected static string $locale = ''
protected static ?PHPFUI\Translation\MissingLogger $missing = NULL
protected static ?array $nativeTranslations = NULL
 
  • var ?array<string,array> $nativeTranslations
protected static ?PHPFUI\Translation\ServiceInterface $serviceInterface = NULL
Methods
protected static getKeys(array $variables) : array
 
  • param array<string,string> $variables
  • return string[]
protected static getTranslation(string $text) : string
 

Get the correct translation, but don't pluralize or substitute

protected static pluralize(string $text, int $count) : string
 

Pluralize the text.

Different sections are divided by the vertical bar (|) character. If you need '|' in your text use the HTML entity | $count of 0 or less will select the first section. $count higher than the last section will return the last section. Other counts will select the number section, so 1 would return the 'one' section in the string 'zero|one|two' You can use the [first,last] notation at the start of a section to specify a matching range for the section.

  • is a wild card for matching any count. For example '[0]There are no brands|[1,9]There are under ten brands|[10,99]There are under 100 brands|[*]There are hundreds of brands'
protected static processVariables(string $text, array $variables) : string
 

Replace variables and perform pluralization if the 'count' index is defined in $variables array, then pluralization will be invoked.

  • param array<string,mixed> $variables
Properties
protected static string $baseLocale = ''
protected static string $directory = '.'
protected static ?PHPFUI\Translation\Tree $loadedTranslations = NULL
protected static string $locale = ''
protected static ?PHPFUI\Translation\MissingLogger $missing = NULL
protected static ?array $nativeTranslations = NULL
 
  • var ?array<string,array> $nativeTranslations
protected static ?PHPFUI\Translation\ServiceInterface $serviceInterface = NULL
Methods
public static clearCache() : void
 

Resets the cache, used by unit testing, as normally the app will not change languages for a single script, where as unit testing will.

public static getBaseLocale() : string
 

The base local is the native language of the app where some of the translation is stored as actual language fragments.

public static getInstalledLanguages() : array
 

Returns an plain array of languages that have been installed by the user.

  • return string[]
protected static getKeys(array $variables) : array
 
  • param array<string,string> $variables
  • return string[]
protected static getTranslation(string $text) : string
 

Get the correct translation, but don't pluralize or substitute

public static getTranslationDirectory() : string
 

Returns the current translation directory that should contain generated files with user strings

public static load(string $directory, string $file) : array
 

Loads a translation chuck from the file system for the specified directory and file.

  • return array<string,string[]>
protected static pluralize(string $text, int $count) : string
 

Pluralize the text.

Different sections are divided by the vertical bar (|) character. If you need '|' in your text use the HTML entity | $count of 0 or less will select the first section. $count higher than the last section will return the last section. Other counts will select the number section, so 1 would return the 'one' section in the string 'zero|one|two' You can use the [first,last] notation at the start of a section to specify a matching range for the section.

  • is a wild card for matching any count. For example '[0]There are no brands|[1,9]There are under ten brands|[10,99]There are under 100 brands|[*]There are hundreds of brands'
protected static processVariables(string $text, array $variables) : string
 

Replace variables and perform pluralization if the 'count' index is defined in $variables array, then pluralization will be invoked.

  • param array<string,mixed> $variables
public static setBaseLocale(string $baseLocale = '') : void
 

Set the base locale. Normally called once in the primary include file for a script.

public static setLocale(string $locale = '') : void
 

Set the locale for the translations to return

  • param string $locale can be any string, but the following are reserved:
    • invisible returns empty string for everything, useful for debugging to see untranslated text
    • TRANS returns 'TRANS' string, useful for debugging
    • RAW returns the actual string passed with no pluralization
    • a blank locale will not preform a translation and returns the actual string passed to trans with pluralization enabled
    Other valid locales would be returned by getInstalledLanguages
public static setTranslationDirectory(string $directory = '.') : void
 

Set the translation directory. Normally called once in the primary include file for a script.

public static setTranslationMissing(?PHPFUI\Translation\MissingLogger $missing = NULL) : void
 

If a translation string is not found, the "missing" method will be called on this object. Great for saving missing translations for later translation.

public static setTranslationService(?PHPFUI\Translation\ServiceInterface $serviceInterface = NULL) : void
 

Sets a translation service that is passsed the string to translate and a local. The service should not provide any pluralization.

public static trans(string $text, array $variables = []) : string
 

Translates a string based on the current locale

  • param string $text to be translated, can be chunked (.modules.general) or native (General Modules)
  • param array<string,mixed> $variables associative array of variables to substitute in returned translation string. Pass an index of 'count' for pluralization support.
  • return string of translated text
© 2024 Bruce Wells
Search Namespaces \ Classes
Configuration