53 $this->defaultPath = dirname(__DIR__, 4) .
'/languages/';
103 $string = $message_key;
104 foreach (array_keys($langs) as $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) {
179 $this->translations[$country_code] = $language_array + $this->translations[$country_code];
190 if (!isset($this->current_language)) {
194 if (empty($this->current_language)) {
195 $this->current_language =
'en';
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;
292 if (is_array(
$data)) {
301 $translations =
elgg_extract($language, $this->translations, []);
321 if (!is_dir($path)) {
322 $this->
getLogger()->info(
"No translations could be loaded from: {$path}");
329 $this->
getLogger()->info(
"Translations loaded from: {$path}");
332 $load_language_files = [
"{$language}.php"];
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) {
385 $this->
getLogger()->warning(
"Language file did not return an array: {$path}");
400 if ($this->was_reloaded) {
412 $this->was_reloaded =
true;
424 if ($calculate_completeness) {
439 if (($language !==
'en') && $calculate_completeness) {
441 $value .=
' (' . $completeness .
'% ' . $this->
translate(
'complete') .
')';
460 if ($language ===
'en') {
467 $en =
count($this->translations[
'en']);
471 $lang = $en - $missing;
473 return round((
$lang / $en) * 100, 2);
492 foreach ($this->translations[
'en'] as $k => $v) {
493 if (!isset($this->translations[$language][$k]) || ($this->translations[$language][$k] === $this->translations[
'en'][$k])) {
517 if (!array_key_exists(
$language, $this->translations)) {
521 return array_key_exists($key, $this->translations[
$language]);
533 $allowed_languages = $this->locale->getLanguageCodes();
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)) {
574 $allowed_languages = $this->config->allowed_languages;
575 if (!empty($allowed_languages)) {
576 $allowed_languages = explode(
',', $allowed_languages);
577 $allowed_languages = array_filter(array_unique($allowed_languages));
582 if (!in_array($this->config->language, $allowed_languages)) {
584 $allowed_languages[] = $this->config->language;
587 if (!in_array(
'en', $allowed_languages)) {
589 $allowed_languages[] =
'en';
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);
637 if (isset($this->translations[$language])) {
static includeFile($file)
Include a file with as little context as possible.
setCurrentLanguage(string $language=null)
Sets current system language.
addTranslation(string $country_code, array $language_array, bool $ensure_translations_loaded=true)
Add a translation.
getLanguageCompleteness(string $language)
Return the level of completeness for a given language code (compared to english)
loadTranslations(string $language)
Load both core and plugin translations.
c Accompany it with the information you received as to the offer to distribute corresponding source complete source code means all the source code for all modules it plus any associated interface definition plus the scripts used to control compilation and installation of the executable as a special the source code distributed need not include anything that is normally and so on of the operating system on which the executable unless that component itself accompanies the executable If distribution of executable or object code is made by offering access to copy from a designated then offering equivalent access to copy the source code from the same place counts as distribution of the source even though third parties are not compelled to copy the source along with the object code You may not or distribute the Program except as expressly provided under this License Any attempt otherwise to sublicense or distribute the Program is void
registerTranslations(string $path, bool $load_all=false, string $language=null)
When given a full path, finds translation files and loads them.
getAvailableLanguages()
Returns an array of all available language keys.
$args
Some servers don't allow PHP to check the rewrite, so try via AJAX.
translate(string $message_key, array $args=[], string $language= '')
Given a message key, returns an appropriately translated full-text string.
static isEmpty($value)
Check if a value isn't empty, but allow 0 and '0'.
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
$config
Advanced site settings, debugging section.
Provides locale related features.
if(!$entity instanceof\ElggUser) $data
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.
languageKeyExists(string $key, string $language= 'en')
Check if a given language key exists.
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.
getCurrentLanguage()
Get the current system/user language or 'en'.
elgg_save_system_cache(string $type, $data, int $expire_after=null)
Elgg cache Cache file interface for caching data.
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.
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.
elgg_load_system_cache(string $type)
Retrieve the contents of a system cache.
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.
__construct(protected Config $config, protected LocaleService $locale)
Constructor.
getInstalledTranslations(bool $calculate_completeness=false)
Return an array of installed translations as an associative array "two letter code" => "native langua...