friendica/src/Core/L10n.php

137 lines
3.6 KiB
PHP
Raw Normal View History

<?php
/**
* @file src/Core/L10n.php
*/
namespace Friendica\Core;
use Friendica\Core\L10n\L10n as L10nClass;
use Friendica\DI;
/**
* 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").
*/
class L10n
{
/**
* Returns the current language code
*
* @return string Language code
*/
public static function getCurrentLang()
{
return DI::l10n()->getCurrentLang();
}
/**
* @param string $lang
*
* @return L10nClass The new L10n class with the new language
*
* @throws \Friendica\Network\HTTPException\InternalServerErrorException
*/
public static function withLang(string $lang)
{
return DI::l10n()->withLang($lang);
}
/**
2020-01-19 01:05:23 -05:00
* 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:
* - DI::l10n()->t('This is an example')
* - DI::l10n()->t('URL %s returned no result', $url)
* - DI::l10n()->t('Current version: %s, new version: %s', $current_version, $new_version)
*
* @param string $s
2018-02-03 13:46:01 -05:00
* @param array $vars Variables to interpolate in the translation string
2019-09-03 10:07:05 -04:00
*
* @return string
*/
2018-02-03 13:46:01 -05:00
public static function t($s, ...$vars)
{
return DI::l10n()->t($s, ...$vars);
}
/**
2020-01-19 01:05:23 -05:00
* 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:
* - DI::l10n()->tt('Like', 'Likes', $count)
* - DI::l10n()->tt("%s user deleted", "%s users deleted", count($users))
*
* @param string $singular
* @param string $plural
2019-01-06 16:06:53 -05:00
* @param int $count
2019-09-03 10:07:05 -04:00
*
* @return string
2019-01-06 16:06:53 -05:00
* @throws \Exception
*/
public static function tt(string $singular, string $plural, int $count)
{
return DI::l10n()->tt($singular, $plural, $count);
}
/**
2020-01-19 01:05:23 -05:00
* 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
*/
public static function getAvailableLanguages()
{
return L10nClass::getAvailableLanguages();
}
/**
2020-01-19 01:05:23 -05:00
* Translate days and months names.
*
* @param string $s String with day or month name.
2019-09-03 10:07:05 -04:00
*
* @return string Translated string.
*/
public static function getDay($s)
{
return DI::l10n()->getDay($s);
}
/**
2020-01-19 01:05:23 -05:00
* Translate short days and months names.
*
* @param string $s String with short day or month name.
2019-09-03 10:07:05 -04:00
*
* @return string Translated string.
*/
public static function getDayShort($s)
{
return DI::l10n()->getDayShort($s);
}
2019-09-03 10:07:05 -04:00
/**
* Load poke verbs
*
* @return array index is present tense verb
* value is array containing past tense verb, translation of present, translation of past
* @hook poke_verbs pokes array
*/
public static function getPokeVerbs()
{
return DI::l10n()->getPokeVerbs();
2019-09-03 10:07:05 -04:00
}
}