*/
namespace Friendica\Core;
-use Friendica\Core\Config;
-use dba;
+use Friendica\BaseObject;
+use Friendica\Database\DBA;
+use Friendica\Core\System;
+require_once 'boot.php';
require_once 'include/dba.php';
/**
* Provide Languange, Translation, and Localisation functions to the application
* Localisation can be referred to by the numeronym L10N (as in: "L", followed by ten more letters, and then "N").
*/
-class L10n
+class L10n extends BaseObject
{
/**
* @brief get the prefered language from the HTTP_ACCEPT_LANGUAGE header
*/
public static function pushLang($language)
{
- global $lang, $a;
+ $a = self::getApp();
- $a->langsave = $lang;
+ $a->langsave = Config::get('system', 'language');
- if ($language === $lang) {
+ if ($language === $a->langsave) {
return;
}
}
$a->strings = [];
self::loadTranslationTable($language);
- $lang = $language;
+ Config::set('system', 'language', $language);
}
/**
*/
public static function popLang()
{
- global $lang, $a;
+ $a = self::getApp();
- if ($lang === $a->langsave) {
+ if (Config::get('system', 'language') === $a->langsave) {
return;
}
$a->strings = [];
}
- $lang = $a->langsave;
+ Config::set('system', 'language', $a->langsave);
}
/**
*/
public static function loadTranslationTable($lang)
{
- $a = get_app();
+ $a = self::getApp();
$a->strings = [];
// load enabled addons strings
- $addons = dba::select('addon', ['name'], ['installed' => true]);
- while ($p = dba::fetch($addons)) {
+ $addons = DBA::select('addon', ['name'], ['installed' => true]);
+ while ($p = DBA::fetch($addons)) {
$name = $p['name'];
if (file_exists("addon/$name/lang/$lang/strings.php")) {
- include("addon/$name/lang/$lang/strings.php");
+ include "addon/$name/lang/$lang/strings.php";
}
}
if (file_exists("view/lang/$lang/strings.php")) {
- include("view/lang/$lang/strings.php");
+ include "view/lang/$lang/strings.php";
}
}
* string interpolation (sprintf) with additional optional arguments.
*
* Usages:
- * - t('This is an example')
- * - t('URL %s returned no result', $url)
- * - t('Current version: %s, new version: %s', $current_version, $new_version)
+ * - L10n::t('This is an example')
+ * - L10n::t('URL %s returned no result', $url)
+ * - L10n::t('Current version: %s, new version: %s', $current_version, $new_version)
*
* @param string $s
+ * @param array $vars Variables to interpolate in the translation string
* @return string
*/
- public static function t($s)
+ public static function t($s, ...$vars)
{
- $a = get_app();
+ $a = self::getApp();
+
+ if (empty($s)) {
+ return '';
+ }
if (x($a->strings, $s)) {
$t = $a->strings[$s];
$s = is_array($t) ? $t[0] : $t;
}
- if (func_num_args() > 1) {
- $args = array_slice(func_get_args(), 1);
- $s = @vsprintf($s, $args);
+
+ if (count($vars) > 0) {
+ $s = sprintf($s, ...$vars);
}
return $s;
* is performed using the count as parameter.
*
* Usages:
- * - tt('Like', 'Likes', $count)
- * - tt("%s user deleted", "%s users deleted", count($users))
+ * - L10n::tt('Like', 'Likes', $count)
+ * - L10n::tt("%s user deleted", "%s users deleted", count($users))
*
- * @global type $lang
* @param string $singular
* @param string $plural
* @param int $count
* @return string
*/
- function tt($singular, $plural, $count)
+ public static function tt($singular, $plural, $count)
{
- global $lang;
- $a = get_app();
+ $a = self::getApp();
+
+ if (!is_numeric($count)) {
+ logger('Non numeric count called by ' . System::callstack(20));
+ }
+
+ $lang = Config::get('system', 'language');
- if (x($a->strings, $singular)) {
+ if (!empty($a->strings[$singular])) {
$t = $a->strings[$singular];
if (is_array($t)) {
$plural_function = 'string_plural_select_' . str_replace('-', '_', $lang);
if (function_exists($plural_function)) {
- $plural_function = 'string_plural_select_default';
+ $i = $plural_function($count);
+ } else {
+ $i = self::stringPluralSelectDefault($count);
+ }
+
+ // for some languages there is only a single array item
+ if (!isset($t[$i])) {
+ $s = $t[0];
+ } else {
+ $s = $t[$i];
}
- $i = $plural_function($count);
- $s = $t[$i];
} else {
$s = $t;
}
- } elseif (string_plural_select_default($count)) {
+ } elseif (self::stringPluralSelectDefault($count)) {
$s = $plural;
} else {
$s = $singular;
return $s;
}
- // provide a fallback which will not collide with
- // a function defined in any language file
- function string_plural_select_default($n)
+ /**
+ * Provide a fallback which will not collide with a function defined in any language file
+ */
+ private static function stringPluralSelectDefault($n)
{
return $n != 1;
}
*
* @return array
*/
- function get_available_languages() {
+ public static function getAvailableLanguages()
+ {
$langs = [];
$strings_file_paths = glob('view/lang/*/strings.php');