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