60 $this->defaultPath = dirname(__DIR__, 4) .
'/languages/';
110 $string = $message_key;
111 foreach (array_keys($langs) as $try_lang) {
114 if (isset($this->translations[$try_lang][$message_key])) {
115 $string = $this->translations[$try_lang][$message_key];
121 $string = vsprintf($string,
$args);
123 if ($string ===
false) {
124 $string = $message_key;
126 $this->
getLogger()->warning(
"Translation error for key '{$message_key}': Too few arguments provided (" . var_export(
$args,
true) .
')');
128 }
catch (\ValueError $e) {
130 $string = $message_key;
132 $this->
getLogger()->warning(
"Translation error for key '{$message_key}': " . $e->getMessage());
139 'Missing %s translation for "%s" language key',
140 ($try_lang ===
'en') ?
'English' : $try_lang,
144 if ($try_lang ===
'en') {
169 public function addTranslation(
string $country_code, array $language_array,
bool $ensure_translations_loaded =
true): bool {
170 $country_code =
trim(strtolower($country_code));
172 if (empty($language_array) || $country_code ===
'') {
176 if (!isset($this->translations[$country_code])) {
177 $this->translations[$country_code] = [];
179 if ($ensure_translations_loaded) {
186 $this->translations[$country_code] = $language_array + $this->translations[$country_code];
197 if (!isset($this->current_language)) {
201 if (empty($this->current_language)) {
202 $this->current_language =
'en';
231 if (!empty($url_lang)) {
234 $cookie = new \ElggCookie(
'language');
235 $cookie->value = $url_lang;
244 if (!empty($cookie)) {
249 if (!empty(
$user) && !empty(
$user->language)) {
250 return $user->language;
254 if ($this->config->language_detect_from_browser) {
256 if (!empty($browserlangs)) {
257 $browserlang = explode(
'_', $browserlangs[0]);
259 return $browserlang[0];
264 return (
string) $this->config->language;
299 if (is_array(
$data)) {
308 $translations =
elgg_extract($language, $this->translations, []);
328 if (!is_dir($path)) {
329 $this->
getLogger()->info(
"No translations could be loaded from: {$path}");
336 $this->
getLogger()->info(
"Translations loaded from: {$path}");
339 $load_language_files = [
"{$language}.php"];
345 $load_language_files = [
347 "{$current_language}.php" 350 $load_language_files = array_unique($load_language_files);
353 $handle = opendir($path);
354 if (empty($handle)) {
355 $this->
getLogger()->error(
"Could not open language path: {$path}");
360 while (($language_file = readdir($handle)) !==
false) {
362 if (str_starts_with($language_file,
'.') || !str_ends_with($language_file,
'.php')) {
366 if (in_array($language_file, $load_language_files) || $load_all) {
392 $this->
getLogger()->warning(
"Language file did not return an array: {$path}");
407 if ($this->was_reloaded) {
419 $this->was_reloaded =
true;
431 if ($calculate_completeness) {
446 if (($language !==
'en') && $calculate_completeness) {
448 $value .=
' (' . $completeness .
'% ' . $this->
translate(
'complete') .
')';
467 if ($language ===
'en') {
474 $en =
count($this->translations[
'en']);
478 $lang = $en - $missing;
480 return round((
$lang / $en) * 100, 2);
499 foreach ($this->translations[
'en'] as $k => $v) {
500 if (!isset($this->translations[$language][$k]) || ($this->translations[$language][$k] === $this->translations[
'en'][$k])) {
524 if (!array_key_exists(
$language, $this->translations)) {
528 return array_key_exists($key, $this->translations[
$language]);
540 $allowed_languages = $this->locale->getLanguageCodes();
544 $iterator = new \DirectoryIterator($path);
549 foreach ($iterator as $file) {
550 if ($file->isDir()) {
554 if ($file->getExtension() !==
'php') {
567 return _elgg_services()->events->triggerResults(
'languages',
'translations', [], array_keys($languages));
577 if (isset($this->allowed_languages)) {
581 $allowed_languages = $this->config->allowed_languages;
582 if (!empty($allowed_languages)) {
583 $allowed_languages = explode(
',', $allowed_languages);
584 $allowed_languages = array_filter(array_unique($allowed_languages));
589 if (!in_array($this->config->language, $allowed_languages)) {
591 $allowed_languages[] = $this->config->language;
594 if (!in_array(
'en', $allowed_languages)) {
596 $allowed_languages[] =
'en';
614 if (isset($this->language_paths[$path])) {
618 if (!is_dir($path)) {
622 $this->language_paths[
$path] =
true;
633 return array_keys($this->language_paths);
644 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.
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.
__construct(Config $config, LocaleService $locale)
Constructor.
getCurrentLanguage()
Get the current system/user language or 'en'.
elgg_save_system_cache(string $type, $data, int $expire_after=null)
Saves a system cache.
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.
getInstalledTranslations(bool $calculate_completeness=false)
Return an array of installed translations as an associative array "two letter code" => "native langua...