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