Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

149 lines
4.0 KiB

  1. <?php
  2. /**
  3. * @file src/Core/L10n.php
  4. */
  5. namespace Friendica\Core;
  6. use Friendica\Core\L10n\L10n as L10nClass;
  7. /**
  8. * Provide Language, Translation, and Localization functions to the application
  9. * Localization can be referred to by the numeronym L10N (as in: "L", followed by ten more letters, and then "N").
  10. */
  11. class L10n
  12. {
  13. /**
  14. * @var L10nClass
  15. */
  16. private static $l10n;
  17. /**
  18. * Initializes the L10n static wrapper with the instance
  19. *
  20. * @param L10nClass $l10n The l10n class
  21. */
  22. public static function init(L10nClass $l10n)
  23. {
  24. self::$l10n = $l10n;
  25. }
  26. /**
  27. * Returns the current language code
  28. *
  29. * @return string Language code
  30. */
  31. public static function getCurrentLang()
  32. {
  33. return self::$l10n->getCurrentLang();
  34. }
  35. /**
  36. * This function should be called before formatting messages in a specific target language
  37. * different from the current user/system language.
  38. *
  39. * It saves the current translation strings in a separate variable and loads new translations strings.
  40. *
  41. * If called repeatedly, it won't save the translation strings again, just load the new ones.
  42. *
  43. * @see popLang()
  44. * @brief Stores the current language strings and load a different language.
  45. * @param string $lang Language code
  46. * @throws \Exception
  47. */
  48. public static function pushLang($lang)
  49. {
  50. self::$l10n->pushLang($lang);
  51. }
  52. /**
  53. * Restores the original user/system language after having used pushLang()
  54. */
  55. public static function popLang()
  56. {
  57. self::$l10n->popLang();
  58. }
  59. /**
  60. * @brief Return the localized version of the provided string with optional string interpolation
  61. *
  62. * This function takes a english string as parameter, and if a localized version
  63. * exists for the current language, substitutes it before performing an eventual
  64. * string interpolation (sprintf) with additional optional arguments.
  65. *
  66. * Usages:
  67. * - L10n::t('This is an example')
  68. * - L10n::t('URL %s returned no result', $url)
  69. * - L10n::t('Current version: %s, new version: %s', $current_version, $new_version)
  70. *
  71. * @param string $s
  72. * @param array $vars Variables to interpolate in the translation string
  73. * @return string
  74. */
  75. public static function t($s, ...$vars)
  76. {
  77. return self::$l10n->t($s, ...$vars);
  78. }
  79. /**
  80. * @brief Return the localized version of a singular/plural string with optional string interpolation
  81. *
  82. * This function takes two english strings as parameters, singular and plural, as
  83. * well as a count. If a localized version exists for the current language, they
  84. * are used instead. Discrimination between singular and plural is done using the
  85. * localized function if any or the default one. Finally, a string interpolation
  86. * is performed using the count as parameter.
  87. *
  88. * Usages:
  89. * - L10n::tt('Like', 'Likes', $count)
  90. * - L10n::tt("%s user deleted", "%s users deleted", count($users))
  91. *
  92. * @param string $singular
  93. * @param string $plural
  94. * @param int $count
  95. * @return string
  96. * @throws \Exception
  97. */
  98. public static function tt(string $singular, string $plural, int $count)
  99. {
  100. return self::$l10n->tt($singular, $plural, $count);
  101. }
  102. /**
  103. * @brief Return installed languages codes as associative array
  104. *
  105. * Scans the view/lang directory for the existence of "strings.php" files, and
  106. * returns an alphabetical list of their folder names (@-char language codes).
  107. * Adds the english language if it's missing from the list.
  108. *
  109. * Ex: array('de' => 'de', 'en' => 'en', 'fr' => 'fr', ...)
  110. *
  111. * @return array
  112. */
  113. public static function getAvailableLanguages()
  114. {
  115. return self::$l10n::getAvailableLanguages();
  116. }
  117. /**
  118. * @brief Translate days and months names.
  119. *
  120. * @param string $s String with day or month name.
  121. * @return string Translated string.
  122. */
  123. public static function getDay($s)
  124. {
  125. return self::$l10n->getDay($s);
  126. }
  127. /**
  128. * @brief Translate short days and months names.
  129. *
  130. * @param string $s String with short day or month name.
  131. * @return string Translated string.
  132. */
  133. public static function getDayShort($s)
  134. {
  135. return self::$l10n->getDayShort($s);
  136. }
  137. }