2018-01-20 14:41:50 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file src/Core/L10n.php
|
|
|
|
*/
|
|
|
|
namespace Friendica\Core;
|
|
|
|
|
2019-07-09 19:44:02 +00:00
|
|
|
use Friendica\Core\L10n\L10n as L10nClass;
|
2018-01-20 14:41:50 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-22 04:16:30 +00:00
|
|
|
* Provide Language, Translation, and Localization functions to the application
|
|
|
|
* Localization can be referred to by the numeronym L10N (as in: "L", followed by ten more letters, and then "N").
|
2018-01-20 14:41:50 +00:00
|
|
|
*/
|
2019-07-09 19:44:02 +00:00
|
|
|
class L10n
|
2018-01-20 14:41:50 +00:00
|
|
|
{
|
|
|
|
/**
|
2019-07-09 19:44:02 +00:00
|
|
|
* @var L10nClass
|
2018-01-20 14:41:50 +00:00
|
|
|
*/
|
2019-07-09 19:44:02 +00:00
|
|
|
private static $l10n;
|
2018-10-22 04:16:30 +00:00
|
|
|
|
|
|
|
/**
|
2019-07-09 19:44:02 +00:00
|
|
|
* Initializes the L10n static wrapper with the instance
|
2018-10-22 04:16:30 +00:00
|
|
|
*
|
2019-07-09 19:44:02 +00:00
|
|
|
* @param L10nClass $l10n The l10n class
|
2018-10-22 04:16:30 +00:00
|
|
|
*/
|
2019-07-09 19:44:02 +00:00
|
|
|
public static function init(L10nClass $l10n)
|
2018-10-22 04:16:30 +00:00
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
self::$l10n = $l10n;
|
2018-10-22 04:16:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the current language code
|
|
|
|
*
|
|
|
|
* @return string Language code
|
|
|
|
*/
|
|
|
|
public static function getCurrentLang()
|
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
return self::$l10n->getCurrentLang();
|
2018-01-20 14:41:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 16:38:01 +00:00
|
|
|
/**
|
2018-10-22 04:16:30 +00:00
|
|
|
* This function should be called before formatting messages in a specific target language
|
|
|
|
* different from the current user/system language.
|
|
|
|
*
|
|
|
|
* It saves the current translation strings in a separate variable and loads new translations strings.
|
|
|
|
*
|
|
|
|
* If called repeatedly, it won't save the translation strings again, just load the new ones.
|
|
|
|
*
|
2019-01-06 21:06:53 +00:00
|
|
|
* @see popLang()
|
2018-10-22 04:16:30 +00:00
|
|
|
* @brief Stores the current language strings and load a different language.
|
|
|
|
* @param string $lang Language code
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-01-21 16:38:01 +00:00
|
|
|
*/
|
2018-10-22 04:16:30 +00:00
|
|
|
public static function pushLang($lang)
|
2018-01-21 16:38:01 +00:00
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
self::$l10n->pushLang($lang);
|
2018-01-20 14:41:50 +00:00
|
|
|
}
|
|
|
|
|
2018-01-21 16:38:01 +00:00
|
|
|
/**
|
2018-10-22 04:16:30 +00:00
|
|
|
* Restores the original user/system language after having used pushLang()
|
2018-01-21 16:38:01 +00:00
|
|
|
*/
|
|
|
|
public static function popLang()
|
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
self::$l10n->popLang();
|
2018-01-20 14:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the localized version of the provided string with optional string interpolation
|
|
|
|
*
|
|
|
|
* This function takes a english string as parameter, and if a localized version
|
|
|
|
* exists for the current language, substitutes it before performing an eventual
|
|
|
|
* string interpolation (sprintf) with additional optional arguments.
|
|
|
|
*
|
|
|
|
* Usages:
|
2018-01-24 02:59:16 +00:00
|
|
|
* - 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)
|
2018-01-20 14:41:50 +00:00
|
|
|
*
|
|
|
|
* @param string $s
|
2018-02-03 18:46:01 +00:00
|
|
|
* @param array $vars Variables to interpolate in the translation string
|
2018-01-20 14:41:50 +00:00
|
|
|
* @return string
|
|
|
|
*/
|
2018-02-03 18:46:01 +00:00
|
|
|
public static function t($s, ...$vars)
|
2018-01-20 14:41:50 +00:00
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
return self::$l10n->t($s, $vars);
|
2018-01-20 14:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return the localized version of a singular/plural string with optional string interpolation
|
|
|
|
*
|
|
|
|
* This function takes two english strings as parameters, singular and plural, as
|
|
|
|
* well as a count. If a localized version exists for the current language, they
|
|
|
|
* are used instead. Discrimination between singular and plural is done using the
|
|
|
|
* localized function if any or the default one. Finally, a string interpolation
|
|
|
|
* is performed using the count as parameter.
|
|
|
|
*
|
|
|
|
* Usages:
|
2018-01-24 02:59:16 +00:00
|
|
|
* - L10n::tt('Like', 'Likes', $count)
|
|
|
|
* - L10n::tt("%s user deleted", "%s users deleted", count($users))
|
2018-01-20 14:41:50 +00:00
|
|
|
*
|
|
|
|
* @param string $singular
|
|
|
|
* @param string $plural
|
2019-01-06 21:06:53 +00:00
|
|
|
* @param int $count
|
2018-01-20 14:41:50 +00:00
|
|
|
* @return string
|
2019-01-06 21:06:53 +00:00
|
|
|
* @throws \Exception
|
2018-01-20 14:41:50 +00:00
|
|
|
*/
|
2019-07-09 19:44:02 +00:00
|
|
|
public static function tt(string $singular, string $plural, int $count)
|
2018-01-20 14:41:50 +00:00
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
return self::$l10n->tt($singular, $plural, $count);
|
2018-01-20 14:41:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Return installed languages codes as associative array
|
|
|
|
*
|
|
|
|
* Scans the view/lang directory for the existence of "strings.php" files, and
|
|
|
|
* returns an alphabetical list of their folder names (@-char language codes).
|
|
|
|
* Adds the english language if it's missing from the list.
|
|
|
|
*
|
|
|
|
* Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-01-21 17:06:27 +00:00
|
|
|
public static function getAvailableLanguages()
|
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
return self::$l10n::getAvailableLanguages();
|
2018-01-20 14:41:50 +00:00
|
|
|
}
|
2018-11-06 14:48:58 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Translate days and months names.
|
|
|
|
*
|
|
|
|
* @param string $s String with day or month name.
|
|
|
|
* @return string Translated string.
|
|
|
|
*/
|
|
|
|
public static function getDay($s)
|
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
return self::$l10n->getDay($s);
|
2018-11-06 14:48:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Translate short days and months names.
|
|
|
|
*
|
|
|
|
* @param string $s String with short day or month name.
|
|
|
|
* @return string Translated string.
|
|
|
|
*/
|
|
|
|
public static function getDayShort($s)
|
|
|
|
{
|
2019-07-09 19:44:02 +00:00
|
|
|
return self::$l10n->getDayShort($s);
|
2018-11-06 14:48:58 +00:00
|
|
|
}
|
2018-01-20 14:41:50 +00:00
|
|
|
}
|