Passed
Push — master ( c0a3a7...3b84a4 )
by Jeroen
58:51
created

engine/lib/languages.php (1 issue)

1
<?php
2
/**
3
 * Elgg language module
4
 * Functions to manage language and translations.
5
 *
6
 * @package Elgg.Core
7
 * @subpackage Languages
8
 */
9
10
/**
11
 * Given a message key, returns an appropriately translated full-text string
12
 *
13
 * @param string $message_key The short message code
14
 * @param array  $args        An array of arguments to pass through vsprintf().
15
 * @param string $language    Optionally, the standard language code
16
 *                            (defaults to site/user default, then English)
17
 *
18
 * @return string Either the translated string, the English string,
19
 * or the original language string.
20
 */
21
function elgg_echo($message_key, array $args = [], $language = "") {
22 820
	return _elgg_services()->translator->translate($message_key, $args, $language);
23
}
24
25
/**
26
 * Add a translation.
27
 *
28
 * Translations are arrays in the Zend Translation array format, eg:
29
 *
30
 *	$english = array('message1' => 'message1', 'message2' => 'message2');
31
 *  $german = array('message1' => 'Nachricht1','message2' => 'Nachricht2');
32
 *
33
 * @param string $country_code   Standard country code (eg 'en', 'nl', 'es')
34
 * @param array  $language_array Formatted array of strings
35
 *
36
 * @return bool Depending on success
37
 */
38
function add_translation($country_code, $language_array) {
39 31
	return _elgg_services()->translator->addTranslation($country_code, $language_array);
40
}
41
42
/**
43
 * Get the current system/user language or "en".
44
 *
45
 * @return string The language code for the site/user or "en" if not set
46
 */
47
function get_current_language() {
48 33
	return _elgg_services()->translator->getCurrentLanguage();
49
}
50
51
/**
52
 * Detect the current system/user language or false.
53
 *
54
 * @return string The language code (eg "en") or false if not set
55
 */
56
function get_language() {
57
	return _elgg_services()->translator->detectLanguage();
0 ignored issues
show
Bug Best Practice introduced by Steve Clay
The expression return _elgg_services()-...lator->detectLanguage() could also return false which is incompatible with the documented return type string. Did you maybe forget to handle an error condition?

If the returned type also contains false, it is an indicator that maybe an error condition leading to the specific return statement remains unhandled.

Loading history...
58
}
59
60
/**
61
 * When given a full path, finds translation files and loads them
62
 *
63
 * @param string $path     Full path
64
 * @param bool   $load_all If true all languages are loaded, if
65
 *                         false only the current language + en are loaded
66
 * @param string $language Language code if other than current + en
67
 *
68
 * @return bool success
69
 */
70
function register_translations($path, $load_all = false, $language = null) {
71 3
	return _elgg_services()->translator->registerTranslations($path, $load_all, $language);
72
}
73
74
/**
75
 * Reload all translations from all registered paths.
76
 *
77
 * This is only called by functions which need to know all possible translations.
78
 *
79
 * @todo Better on demand loading based on language_paths array
80
 *
81
 * @return void
82
 */
83
function reload_all_translations() {
84
	return _elgg_services()->translator->reloadAllTranslations();
85
}
86
87
/**
88
 * Return an array of installed translations as an associative
89
 * array "two letter code" => "native language name".
90
 *
91
 * @return array
92
 */
93
function get_installed_translations() {
94
	return _elgg_services()->translator->getInstalledTranslations();
95
}
96
97
/**
98
 * Return the level of completeness for a given language code (compared to english)
99
 *
100
 * @param string $language Language
101
 *
102
 * @return int
103
 */
104
function get_language_completeness($language) {
105 487
	return _elgg_services()->translator->getLanguageCompleteness($language);
106
}
107
108
/**
109
 * Return the translation keys missing from a given language,
110
 * or those that are identical to the english version.
111
 *
112
 * @param string $language The language
113
 *
114
 * @return mixed
115
 */
116
function get_missing_language_keys($language) {
117
	return _elgg_services()->translator->getMissingLanguageKeys($language);
118
}
119
120
/**
121
 * Check if a given language key exists.
122
 *
123
 * @note Translators should, whenever creating a "dynamically" named language key, always create an
124
 *       English (fallback) translation as well.
125
 *
126
 * @param string $key      The translation key
127
 * @param string $language The language. Provided an English translation exists for all created keys, then
128
 *                         devs can generally use the default "en", regardless of the site/user language.
129
 *
130
 * @return bool
131
 * @since 1.11
132
 */
133
function elgg_language_key_exists($key, $language = 'en') {
134 519
	return _elgg_services()->translator->languageKeyExists($key, $language);
135
}
136