33 private $translations = [];
38 private $defaultPath = null;
43 private $current_language = null;
64 private $language_paths = [];
69 private $was_reloaded =
false;
78 $this->config = $config;
79 $this->locale = $locale;
81 $this->defaultPath = dirname(dirname(dirname(dirname(__DIR__)))) .
"/languages/";
92 return $this->translations;
107 if (!is_string($message_key) ||
strlen($message_key) < 1) {
108 $this->
getLogger()->warning(
'$message_key needs to be a string in ' . __METHOD__ .
'(), ' . gettype($message_key) .
' provided');
136 $string = $message_key;
137 foreach (array_keys($langs) as $try_lang) {
140 if (isset($this->translations[$try_lang][$message_key])) {
141 $string = $this->translations[$try_lang][$message_key];
147 $string = vsprintf($string,
$args);
149 if ($string ===
false) {
150 $string = $message_key;
152 $this->
getLogger()->warning(
"Translation error for key '{$message_key}': Too few arguments provided (" . var_export(
$args,
true) .
')');
154 }
catch (\ValueError $e) {
156 $string = $message_key;
158 $this->
getLogger()->warning(
"Translation error for key '{$message_key}': " . $e->getMessage());
165 'Missing %s translation for "%s" language key',
166 ($try_lang ===
'en') ?
'English' : $try_lang,
170 if ($try_lang ===
'en') {
195 public function addTranslation($country_code, $language_array, $ensure_translations_loaded =
true) {
196 $country_code = strtolower($country_code);
197 $country_code =
trim($country_code);
199 if (!is_array($language_array) || empty($language_array) || $country_code ===
"") {
203 if (!isset($this->translations[$country_code])) {
204 $this->translations[$country_code] = [];
206 if ($ensure_translations_loaded) {
213 $this->translations[$country_code] = $language_array + $this->translations[$country_code];
224 if (!isset($this->current_language)) {
228 if (!$this->current_language) {
229 $this->current_language =
'en';
232 return $this->current_language;
258 if (!empty($url_lang)) {
261 $cookie = new \ElggCookie(
'language');
262 $cookie->value = $url_lang;
270 if (!empty($cookie)) {
275 if (!empty(
$user) && !empty(
$user->language)) {
276 return $user->language;
280 if ($this->config->language_detect_from_browser) {
282 if (!empty($browserlangs)) {
283 $browserlang = explode(
'_', $browserlangs[0]);
285 return $browserlang[0];
331 if (is_array(
$data)) {
340 $translations =
elgg_extract($language, $this->translations, []);
360 if (!is_dir($path)) {
361 $this->
getLogger()->info(
"No translations could be loaded from: {$path}");
368 $this->
getLogger()->info(
"Translations loaded from: {$path}");
371 $load_language_files = [
"$language.php"];
377 $load_language_files = [
379 "$current_language.php" 382 $load_language_files = array_unique($load_language_files);
386 if ($handle = opendir($path)) {
387 while (
false !== ($language_file = readdir($handle))) {
389 if (
substr($language_file, 0, 1) ==
'.' ||
substr($language_file, -4) !==
'.php') {
393 if (in_array($language_file, $load_language_files) || $load_all) {
399 $this->
getLogger()->error(
"Could not open language path: {$path}");
422 $this->
getLogger()->warning(
"Language file did not return an array: {$path}");
437 if ($this->was_reloaded) {
449 $this->was_reloaded =
true;
461 if ($calculate_completeness) {
476 if (($language !==
'en') && $calculate_completeness) {
478 $value .=
" (" . $completeness .
"% " . $this->
translate(
'complete') .
")";
505 $en =
count($this->translations[
'en']);
509 $missing =
count($missing);
514 $lang = $en - $missing;
516 return round((
$lang / $en) * 100, 2);
536 foreach ($this->translations[
'en'] as $k => $v) {
537 if ((!isset($this->translations[$language][$k]))
538 || ($this->translations[$language][$k] == $this->translations[
'en'][$k])) {
543 if (
count($missing)) {
566 if (!array_key_exists(
$language, $this->translations)) {
570 return array_key_exists(
$key, $this->translations[
$language]);
586 $iterator = new \DirectoryIterator($path);
591 foreach ($iterator as $file) {
592 if ($file->isDir()) {
596 if ($file->getExtension() !==
'php') {
609 $languages = array_keys($languages);
611 return _elgg_services()->hooks->trigger(
'languages',
'translations', [], $languages);
621 if (isset($this->allowed_languages)) {
657 if (isset($this->language_paths[$path])) {
661 if (!is_dir($path)) {
665 $this->language_paths[
$path] =
true;
676 return array_keys($this->language_paths);
687 if (isset($this->translations[$language])) {
static includeFile($file)
Include a file with as little context as possible.
setCurrentLanguage($language=null)
Sets current system language.
loadTranslations(string $language)
Load both core and plugin translations.
registerTranslations(string $path, bool $load_all=false, string $language=null)
When given a full path, finds translation files and loads them.
translate($message_key, array $args=[], $language="")
Given a message key, returns an appropriately translated full-text string.
if(elgg_trigger_plugin_hook('usersettings:save', 'user', $hooks_params, true)) foreach($request->validation() ->all() as $item) $data
addTranslation($country_code, $language_array, $ensure_translations_loaded=true)
Add a translation.
getAvailableLanguages()
Returns an array of all available language keys.
$args
Some servers don't allow PHP to check the rewrite, so try via AJAX.
Provides locale related features.
registerLanguagePath(string $path)
Registers a path for potential translation files.
trait Loggable
Enables adding a logger.
detectLanguage()
Detect the current system/user language or false.
reloadAllTranslations()
Reload all translations from all registered paths.
getLanguagePaths()
Returns a unique array with locations of translation files.
getLoadedTranslations()
Get a map of all loaded translations.
bootTranslations()
Ensures all needed translations are loaded.
__construct(Config $config, LocaleService $locale)
Constructor.
getCurrentLanguage()
Get the current system/user language or "en".
getLanguageCompleteness($language)
Return the level of completeness for a given language code (compared to english)
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
elgg_set_cookie(\ElggCookie $cookie)
Set a cookie, but allow plugins to customize it first.
if($container instanceof ElggGroup &&$container->guid!=elgg_get_page_owner_guid()) $key
getLogger()
Returns logger.
elgg_load_system_cache($type)
Retrieve the contents of a system cache.
getAllowedLanguages()
Returns an array of allowed languages as configured by the site admin.
includeLanguageFile(string $path)
Load cached or include a language file by its path.
ensureTranslationsLoaded(string $language)
Make sure translations are loaded.
languageKeyExists($key, $language= 'en')
Check if a given language key exists.
getMissingLanguageKeys(string $language)
Return the translation keys missing from a given language, or those that are identical to the english...
static sanitize($path, $append_slash=true)
Sanitize file paths ensuring that they begin and end with slashes etc.
_elgg_services()
Get the global service provider.
elgg_save_system_cache($type, $data, int $expire_after=null)
Saves a system cache.
getInstalledTranslations(bool $calculate_completeness=false)
Return an array of installed translations as an associative array "two letter code" => "native langua...