7 use Elgg\Traits\Loggable;
53 $this->defaultPath = dirname(__DIR__, 4) .
'/languages/';
64 return $this->translations;
100 $langs = array_intersect_key($langs, array_flip($this->getAllowedLanguages()));
103 $string = $message_key;
104 foreach (array_keys($langs) as $try_lang) {
105 $this->ensureTranslationsLoaded($try_lang);
107 if (isset($this->translations[$try_lang][$message_key])) {
108 $string = $this->translations[$try_lang][$message_key];
114 $string = vsprintf($string,
$args);
116 if ($string ===
false) {
117 $string = $message_key;
119 $this->getLogger()->warning(
"Translation error for key '{$message_key}': Too few arguments provided (" . var_export(
$args,
true) .
')');
121 }
catch (\ValueError $e) {
123 $string = $message_key;
125 $this->
getLogger()->warning(
"Translation error for key '{$message_key}': " . $e->getMessage());
132 'Missing %s translation for "%s" language key',
133 ($try_lang ===
'en') ?
'English' : $try_lang,
137 if ($try_lang ===
'en') {
162 public function addTranslation(
string $country_code, array $language_array,
bool $ensure_translations_loaded =
true): bool {
163 $country_code = trim(strtolower($country_code));
165 if (empty($language_array) || $country_code ===
'') {
169 if (!isset($this->translations[$country_code])) {
170 $this->translations[$country_code] = [];
172 if ($ensure_translations_loaded) {
174 $this->loadTranslations($country_code);
179 $this->translations[$country_code] = $language_array + $this->translations[$country_code];
190 if (!isset($this->current_language)) {
191 $this->current_language = $this->detectLanguage();
194 if (empty($this->current_language)) {
195 $this->current_language =
'en';
198 return $this->current_language;
224 if (!empty($url_lang)) {
227 $cookie = new \ElggCookie(
'language');
228 $cookie->value = $url_lang;
237 if (!empty($cookie)) {
242 if (!empty(
$user) && !empty(
$user->language)) {
243 return $user->language;
247 if ($this->config->language_detect_from_browser) {
249 if (!empty($browserlangs)) {
250 $browserlang = explode(
'_', $browserlangs[0]);
252 return $browserlang[0];
257 return (
string) $this->config->language;
270 $languages = array_unique([
'en', $this->getCurrentLanguage()]);
292 if (is_array(
$data)) {
297 foreach ($this->getLanguagePaths() as
$path) {
321 if (!is_dir(
$path)) {
322 $this->getLogger()->info(
"No translations could be loaded from: {$path}");
327 $this->registerLanguagePath(
$path);
329 $this->getLogger()->info(
"Translations loaded from: {$path}");
332 $load_language_files = [
"{$language}.php"];
336 $current_language = $this->getCurrentLanguage();
338 $load_language_files = [
340 "{$current_language}.php"
343 $load_language_files = array_unique($load_language_files);
346 $handle = opendir(
$path);
347 if (empty($handle)) {
348 $this->getLogger()->error(
"Could not open language path: {$path}");
353 while (($language_file = readdir($handle)) !==
false) {
355 if (str_starts_with($language_file,
'.') || !str_ends_with($language_file,
'.php')) {
359 if (in_array($language_file, $load_language_files) || $load_all) {
360 $return = $return && $this->includeLanguageFile(
$path . $language_file);
381 $this->addTranslation(basename(
$path,
'.php'),
$result);
385 $this->getLogger()->warning(
"Language file did not return an array: {$path}");
400 if ($this->was_reloaded) {
407 $this->ensureTranslationsLoaded(
$language);
412 $this->was_reloaded =
true;
424 if ($calculate_completeness) {
426 $this->reloadAllTranslations();
439 if ((
$language !==
'en') && $calculate_completeness) {
440 $completeness = $this->getLanguageCompleteness(
$language);
441 $value .=
' (' . $completeness .
'% ' . $this->translate(
'complete') .
')';
465 $this->reloadAllTranslations();
467 $en = count($this->translations[
'en']);
469 $missing = count($this->getMissingLanguageKeys(
$language));
471 $lang = $en - $missing;
473 return round((
$lang / $en) * 100, 2);
488 $this->reloadAllTranslations();
492 foreach ($this->translations[
'en'] as $k => $v) {
493 if (!isset($this->translations[
$language][$k]) || ($this->translations[
$language][$k] === $this->translations[
'en'][$k])) {
515 $this->ensureTranslationsLoaded(
$language);
517 if (!array_key_exists(
$language, $this->translations)) {
521 return array_key_exists(
$key, $this->translations[
$language]);
535 foreach ($this->getLanguagePaths() as
$path) {
537 $iterator = new \DirectoryIterator(
$path);
538 }
catch (\Exception $e) {
542 foreach ($iterator as $file) {
543 if ($file->isDir()) {
547 if ($file->getExtension() !==
'php') {
570 if (isset($this->allowed_languages)) {
607 if (isset($this->language_paths[
$path])) {
611 if (!is_dir(
$path)) {
615 $this->language_paths[
$path] =
true;
626 return array_keys($this->language_paths);
getLogger()
Returns logger.
return[ 'admin/delete_admin_notices'=>['access'=> 'admin'], 'admin/menu/save'=>['access'=> 'admin'], 'admin/plugins/activate'=>['access'=> 'admin'], 'admin/plugins/activate_all'=>['access'=> 'admin'], 'admin/plugins/deactivate'=>['access'=> 'admin'], 'admin/plugins/deactivate_all'=>['access'=> 'admin'], 'admin/plugins/set_priority'=>['access'=> 'admin'], 'admin/security/security_txt'=>['access'=> 'admin'], 'admin/security/settings'=>['access'=> 'admin'], 'admin/security/regenerate_site_secret'=>['access'=> 'admin'], 'admin/site/cache/invalidate'=>['access'=> 'admin'], 'admin/site/flush_cache'=>['access'=> 'admin'], 'admin/site/icons'=>['access'=> 'admin'], 'admin/site/set_maintenance_mode'=>['access'=> 'admin'], 'admin/site/set_robots'=>['access'=> 'admin'], 'admin/site/theme'=>['access'=> 'admin'], 'admin/site/unlock_upgrade'=>['access'=> 'admin'], 'admin/site/settings'=>['access'=> 'admin'], 'admin/upgrade'=>['access'=> 'admin'], 'admin/upgrade/reset'=>['access'=> 'admin'], 'admin/user/ban'=>['access'=> 'admin'], 'admin/user/bulk/ban'=>['access'=> 'admin'], 'admin/user/bulk/delete'=>['access'=> 'admin'], 'admin/user/bulk/unban'=>['access'=> 'admin'], 'admin/user/bulk/validate'=>['access'=> 'admin'], 'admin/user/change_email'=>['access'=> 'admin'], 'admin/user/delete'=>['access'=> 'admin'], 'admin/user/login_as'=>['access'=> 'admin'], 'admin/user/logout_as'=>[], 'admin/user/makeadmin'=>['access'=> 'admin'], 'admin/user/resetpassword'=>['access'=> 'admin'], 'admin/user/removeadmin'=>['access'=> 'admin'], 'admin/user/unban'=>['access'=> 'admin'], 'admin/user/validate'=>['access'=> 'admin'], 'annotation/delete'=>[], 'avatar/upload'=>[], 'comment/save'=>[], 'diagnostics/download'=>['access'=> 'admin'], 'entity/chooserestoredestination'=>[], 'entity/delete'=>[], 'entity/mute'=>[], 'entity/restore'=>[], 'entity/subscribe'=>[], 'entity/trash'=>[], 'entity/unmute'=>[], 'entity/unsubscribe'=>[], 'login'=>['access'=> 'logged_out'], 'logout'=>[], 'notifications/mute'=>['access'=> 'public'], 'plugins/settings/remove'=>['access'=> 'admin'], 'plugins/settings/save'=>['access'=> 'admin'], 'plugins/usersettings/save'=>[], 'register'=>['access'=> 'logged_out', 'middleware'=>[\Elgg\Router\Middleware\RegistrationAllowedGatekeeper::class,],], 'river/delete'=>[], 'settings/notifications'=>[], 'settings/notifications/subscriptions'=>[], 'user/changepassword'=>['access'=> 'public'], 'user/requestnewpassword'=>['access'=> 'public'], 'useradd'=>['access'=> 'admin'], 'usersettings/save'=>[], 'widgets/add'=>[], 'widgets/delete'=>[], 'widgets/move'=>[], 'widgets/save'=>[],]
if(! $entity instanceof \ElggUser) $data
elgg_save_system_cache(string $type, $data, ?int $expire_after=null)
Elgg cache Cache file interface for caching data.
elgg_load_system_cache(string $type)
Retrieve the contents of a system cache.
Provides locale related features.
ensureTranslationsLoaded(string $language)
Make sure translations are loaded.
getLanguagePaths()
Returns a unique array with locations of translation files.
setCurrentLanguage(?string $language=null)
Sets current system language.
__construct(protected Config $config, protected LocaleService $locale)
Constructor.
languageKeyExists(string $key, string $language='en')
Check if a given language key exists.
detectLanguage()
Detect the current system/user language or false.
getCurrentLanguage()
Get the current system/user language or 'en'.
translate(string $message_key, array $args=[], string $language='')
Given a message key, returns an appropriately translated full-text string.
getAllowedLanguages()
Returns an array of allowed languages as configured by the site admin.
getAvailableLanguages()
Returns an array of all available language keys.
reloadAllTranslations()
Reload all translations from all registered paths.
registerTranslations(string $path, bool $load_all=false, ?string $language=null)
When given a full path, finds translation files and loads them.
getLoadedTranslations()
Get a map of all loaded translations.
registerLanguagePath(string $path)
Registers a path for potential translation files.
addTranslation(string $country_code, array $language_array, bool $ensure_translations_loaded=true)
Add a translation.
bootTranslations()
Ensures all needed translations are loaded.
loadTranslations(string $language)
Load both core and plugin translations.
getInstalledTranslations(bool $calculate_completeness=false)
Return an array of installed translations as an associative array "two letter code" => "native langua...
includeLanguageFile(string $path)
Load cached or include a language file by its path.
getLanguageCompleteness(string $language)
Return the level of completeness for a given language code (compared to english)
getMissingLanguageKeys(string $language)
Return the translation keys missing from a given language, or those that are identical to the english...
Allow executing scripts without $this context or local vars.
Find Elgg and project paths.
Functions for use as event handlers or other situations where you need a globally accessible callable...
$config
Advanced site settings, debugging section.
_elgg_services()
Get the global service provider.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
$args
Some servers don't allow PHP to check the rewrite, so try via AJAX.
foreach(array_keys($combine_languages) as $language) $translations
if($container instanceof ElggGroup && $container->guid !=elgg_get_page_owner_guid()) $key
if(parse_url(elgg_get_site_url(), PHP_URL_PATH) !=='/') if(file_exists(elgg_get_root_path() . 'robots.txt'))
Set robots.txt.
elgg_set_cookie(\ElggCookie $cookie)
Set a cookie, but allow plugins to customize it first.