Passed
Push — master ( 9d11d1...a4b028 )
by Jeroen
10:12
created

elgg_get_available_languages()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 2
ccs 1
cts 1
cp 1
crap 1
rs 10
c 0
b 0
f 0
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
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();
0 ignored issues
show
Bug introduced by
Are you sure the usage of _elgg_services()->transl...reloadAllTranslations() targeting Elgg\I18n\Translator::reloadAllTranslations() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
85
}
86
87
/**
88
 * Return an array of installed translations as an associative
89
 * array "two letter code" => "native language name".
90
 *
91
 * @param boolean $calculate_completeness Set to true if you want a completeness postfix added to the language text
92
 *
93
 * @return array
94
 */
95
function get_installed_translations($calculate_completeness = false) {
96
	return _elgg_services()->translator->getInstalledTranslations($calculate_completeness);
97
}
98
99
/**
100
 * Return the level of completeness for a given language code (compared to english)
101
 *
102
 * @param string $language Language
103
 *
104
 * @return int
105 487
 */
106
function get_language_completeness($language) {
107
	return _elgg_services()->translator->getLanguageCompleteness($language);
108
}
109
110
/**
111
 * Check if a given language key exists.
112
 *
113
 * @note Translators should, whenever creating a "dynamically" named language key, always create an
114
 *       English (fallback) translation as well.
115
 *
116
 * @param string $key      The translation key
117
 * @param string $language The language. Provided an English translation exists for all created keys, then
118
 *                         devs can generally use the default "en", regardless of the site/user language.
119
 *
120
 * @return bool
121
 * @since 1.11
122
 */
123
function elgg_language_key_exists($key, $language = 'en') {
124
	return _elgg_services()->translator->languageKeyExists($key, $language);
125
}
126
127
/**
128
 * Returns an array of available languages
129
 *
130
 * @return array
131
 * @since 3.0
132
 */
133
function elgg_get_available_languages() {
134 519
	return _elgg_services()->translator->getAvailableLanguages();
135
}
136