Documentation

Lang
in package
Uses BackwardCompatibility

Handles the localizable strings shown in SMF's user interface.

Table of Contents

Properties

$decimal_separator  : string
$decimals  : int
$default  : string
$dirs  : array<string|int, mixed>
$editortxt  : array<string|int, mixed>
$forum_copyright  : string
$helptxt  : array<string|int, mixed>
$thousands_separator  : string
$txt  : array<string|int, mixed>
$txtBirthdayEmails  : array<string|int, mixed>
$tztxt  : array<string|int, mixed>
$already_loaded  : array<string|int, mixed>
$backcompat  : array<string|int, mixed>
$localized_copyright  : array<string|int, mixed>

Methods

addDirs()  : mixed
Populates Lang::$dirs with paths to language directories.
censorText()  : string
Replace all vulgar words with respective proper words. (substring or whole words..) What this function does: - it censors the passed string.
exportStatic()  : void
Provides a way to export a class's public static properties and methods to global namespace.
get()  : array<string|int, mixed>
Attempt to reload our known languages.
load()  : string
Load a language file.
numberFormat()  : string
Wrapper for number_format() that uses Lang::$txt['number_format'] to figure out the parameters to pass to number_format().
sentenceList()  : string
Concatenates an array of strings into a grammatically correct sentence list.
tokenTxtReplace()  : string
Replaces tokens in a string with values from Lang::$txt.

Properties

$decimal_separator

public static string $decimal_separator

Decimal separator to use in Lang::numberFormat.

$decimals

public static int $decimals

Default number of decimal places to use for floats in Lang::numberFormat.

$default

public static string $default

Local copy of SMF\Config::$language

$dirs

public static array<string|int, mixed> $dirs = []

Language file directories.

$editortxt

public static array<string|int, mixed> $editortxt = []

Array of localized strings for the editor UI.

public static string $forum_copyright = '<a href="%3$s?action=credits" title="License" target="_blank" rel="noopener">%1$s &copy; %2$s</a>, <a href="https://www.simplemachines.org" title="Simple Machines" target="_blank" rel="noopener">Simple Machines</a>'

sprintf format string to show the SMF copyright. The default value will be overwritten when a language is loaded.

$helptxt

public static array<string|int, mixed> $helptxt = []

Array of localized strings for the admin help popup.

$thousands_separator

public static string $thousands_separator

Thousands separator to use in Lang::numberFormat.

$txt

public static array<string|int, mixed> $txt = []

Array of localized strings for the UI.

$txtBirthdayEmails

public static array<string|int, mixed> $txtBirthdayEmails = []

Array of localized strings for birthday emails.

$tztxt

public static array<string|int, mixed> $tztxt = []

Array of localized strings for time zone "meta-zones".

$already_loaded

private static array<string|int, mixed> $already_loaded = []

Tracks which langauge files we have loaded.

$backcompat

private static array<string|int, mixed> $backcompat = ['func_names' => ['load' => 'loadLanguage', 'get' => 'getLanguages', 'censorText' => 'censorText', 'tokenTxtReplace' => 'tokenTxtReplace', 'sentenceList' => 'sentence_list', 'numberFormat' => 'comma_format'], 'prop_names' => ['txt' => 'txt', 'tztxt' => 'tztxt', 'editortxt' => 'editortxt', 'helptxt' => 'helptxt', 'txtBirthdayEmails' => 'txtBirthdayEmails', 'forum_copyright' => 'forum_copyright']]

BackwardCompatibility settings for this class.

private static array<string|int, mixed> $localized_copyright = []

Tracks the value of $forum_copyright for different languages.

Methods

addDirs()

Populates Lang::$dirs with paths to language directories.

public static addDirs([array<string|int, mixed>|string $custom_dirs = [] ]) : mixed

If $custom_dirs is empty, Lang::$dirs will be populated with the standard language directories in the current theme, the current theme's base theme (if applicable), and the default theme.

If $custom_dirs is set to one or more directory paths, those paths will be prepended to Lang::$dirs.

Parameters
$custom_dirs : array<string|int, mixed>|string = []

Optional custom directories to include.

censorText()

Replace all vulgar words with respective proper words. (substring or whole words..) What this function does: - it censors the passed string.

public static censorText(string &$text[, bool $force = false ]) : string
  • if the theme setting allow_no_censored is on, and the theme option show_no_censored is enabled, does not censor, unless force is also set.
  • it caches the list of censored words to reduce parsing.
Parameters
$text : string

The text to censor

$force : bool = false

Whether to censor the text regardless of settings

Return values
string

The censored text

exportStatic()

Provides a way to export a class's public static properties and methods to global namespace.

public static exportStatic() : void

To do so:

  1. Use this trait in the class.
  2. At the END of the class's file, call its exportStatic() method.

Although it might not seem that way at first glance, this approach conforms to section 2.3 of PSR 1, since executing this method is simply a dynamic means of declaring functions when the file is included; it has no other side effects.

Regarding the $backcompat items:

A class's static properties are not exported to global variables unless explicitly included in $backcompat['prop_names']. Likewise, a class's static methods are not exported as global functions unless explicitly included in $backcompat['func_names'].

$backcompat['prop_names'] is a simple array where the keys are the names of one or more of a class's static properties, and the values are the names of global variables. In each case, the global variable will be set to a reference to the static property. Static properties that are not named in this array will not be exported.

$backcompat['func_names'] is a simple array where the keys are the names of one or more of a class's static methods, and the values are the names that should be used for global functions that will encapsulate those methods. Methods that are not named in this array will not be exported.

Adding non-static properties or methods to the $backcompat arrays will produce runtime errors. It is the responsibility of the developer to make sure not to do this.

get()

Attempt to reload our known languages.

public static get([bool $use_cache = true ]) : array<string|int, mixed>

It will try to choose only utf8 or non-utf8 languages.

Parameters
$use_cache : bool = true

Whether or not to use the cache

Return values
array<string|int, mixed>

An array of information about available languages

load()

Load a language file.

public static load(string $template_name[, string $lang = '' ][, bool $fatal = true ][, bool $force_reload = false ]) : string

Tries the current and default themes as well as the user and global languages.

Parameters
$template_name : string

The name of a template file.

$lang : string = ''

A specific language to load this file from.

$fatal : bool = true

Whether to die with an error if it can't be loaded.

$force_reload : bool = false

Whether to load the file again if it's already loaded.

Return values
string

The language actually loaded.

numberFormat()

Wrapper for number_format() that uses Lang::$txt['number_format'] to figure out the parameters to pass to number_format().

public static numberFormat(int|float $number[, int $decimals = null ]) : string
Parameters
$number : int|float

A number.

$decimals : int = null

If set, will use the specified number of decimal places. Otherwise it's automatically determined.

Return values
string

A formatted number

sentenceList()

Concatenates an array of strings into a grammatically correct sentence list.

public static sentenceList(array<string|int, mixed> $list) : string

Uses formats defined in the language files to build the list according to the rules for the currently loaded language.

Parameters
$list : array<string|int, mixed>

An array of strings to concatenate.

Return values
string

The localized sentence list.

tokenTxtReplace()

Replaces tokens in a string with values from Lang::$txt.

public static tokenTxtReplace([string $string = '' ]) : string

Tokens take the form of '{key}', where 'key' is the key of some element in the Lang::$txt array.

Parameters
$string : string = ''

The string in which to make replacements.

Return values
string

The updated string.


        
On this page

Search results