Skip to content

Commit

Permalink
feat(di): add translator to public services
Browse files Browse the repository at this point in the history
Translator is now avaialable via elgg()->translator.
elgg_echo() is now aliases as elgg()->echo().
  • Loading branch information
hypeJunction committed Feb 2, 2018
1 parent 8d19fc4 commit b46e11f
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 44 deletions.
2 changes: 0 additions & 2 deletions actions/login.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@

try {
login($user, $persistent);
// re-register at least the core language file for users with language other than site default
register_translations(dirname(dirname(__FILE__)) . "/languages/");
} catch (LoginException $e) {
return elgg_error_response($e->getMessage());
}
Expand Down
21 changes: 20 additions & 1 deletion engine/classes/Elgg/Di/PublicContainer.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use DI\Container;
use Elgg\Application\Database;
use Elgg\Gatekeeper;
use Elgg\I18n\Translator;
use Elgg\Menu\Service;
use Elgg\Views\TableColumn\ColumnFactory;
use ElggSession;
Expand All @@ -15,8 +16,11 @@
* @property-read Database $db Public database
* @property-read Gatekeeper $gatekeeper Gatekeeper
* @property-read Service $menus Menus
* @property-read ElggSession $session Session
* @property-read ElggSession $session Session
* @property-read ColumnFactory $table_columns Table columns
* @property-read Translator $translator Translator
*
* @method string echo(string $message_key, array $args, string $language) Outputs a translated string
*/
class PublicContainer extends Container {

Expand All @@ -26,4 +30,19 @@ class PublicContainer extends Container {
public function __get($name) {
return $this->get($name);
}

/**
* {@inheritdoc}
*/
public function __call($name, $arguments) {
$proxies = [
'echo' => ['translator', 'translate'],
];

if (!empty($proxies[$name])) {
$svc = $proxies[$name][0];
$method = $proxies[$name][1];
return call_user_func_array([$this->$svc, $method], $arguments);
}
}
}
44 changes: 38 additions & 6 deletions engine/classes/Elgg/I18n/Translator.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@

use Elgg\Config;
use Elgg\Includer;
use PluginException;

/**
* WARNING: API IN FLUX. DO NOT USE DIRECTLY.
* Translator
*
* @access private
* Use elgg()->translator
*
* @since 1.10.0
*/
Expand Down Expand Up @@ -69,14 +70,21 @@ class Translator {
* Constructor
*
* @param Config $config Elgg config
*
* @access private
* @internal
*/
public function __construct(Config $config) {
$this->config = $config;
$this->defaultPath = dirname(dirname(dirname(dirname(__DIR__)))) . "/languages/";
}

/**
* Check if translations were loaded from cache
*
* @return bool
* @access private
* @internal
*/
public function wasLoadedFromCache() {
return $this->loaded_from_cache;
Expand Down Expand Up @@ -227,6 +235,8 @@ public function setCurrentLanguage($language = null) {
* Detect the current system/user language or false.
*
* @return false|string The language code (eg "en") or false if not set
* @access private
* @internal
*/
public function detectLanguage() {
// detect from URL
Expand Down Expand Up @@ -265,6 +275,7 @@ public function detectLanguage() {
* @return void
*
* @access private
* @internal
*/
public function loadTranslations($language = null) {
if (elgg_is_system_cache_enabled()) {
Expand Down Expand Up @@ -312,8 +323,11 @@ public function loadTranslations($language = null) {
* logged in user.
*
* @param string $language Language code
*
* @return void
* @throws \PluginException
* @throws PluginException
* @access private
* @internal
*/
private function loadPluginTranslations($language) {
// Get active plugins
Expand Down Expand Up @@ -345,7 +359,7 @@ private function loadPluginTranslations($language) {

// Register translations from the plugin languages directory
if (!$this->registerTranslations($languages_path, false, $language)) {
throw new \PluginException(vsprintf('Cannot register languages for plugin %s (guid: %s) at %s.',
throw new PluginException(vsprintf('Cannot register languages for plugin %s (guid: %s) at %s.',
[$plugin->getID(), $plugin->guid, $languages_path]));
}
}
Expand All @@ -360,6 +374,9 @@ private function loadPluginTranslations($language) {
* @param string $language Language code
*
* @return bool success
*
* @access private
* @internal
*/
public function registerTranslations($path, $load_all = false, $language = null) {
$path = \Elgg\Project\Paths::sanitize($path);
Expand Down Expand Up @@ -417,6 +434,8 @@ public function registerTranslations($path, $load_all = false, $language = null)
*
* @param string $path Path to file
* @return bool
* @access private
* @internal
*/
protected function includeLanguageFile($path) {
$cache_key = "lang/" . sha1($path);
Expand All @@ -441,6 +460,8 @@ protected function includeLanguageFile($path) {
* @todo Better on demand loading based on language_paths array
*
* @return void
* @access private
* @internal
*/
public function reloadAllTranslations() {
if ($this->was_reloaded) {
Expand Down Expand Up @@ -543,10 +564,11 @@ public function getLanguageCompleteness($language) {
* @param string $language The language
*
* @return mixed
* @access private
* @internal
*/
public function getMissingLanguageKeys($language) {


// Ensure that all possible translations are loaded
$this->reloadAllTranslations();

Expand Down Expand Up @@ -603,7 +625,7 @@ public function getAvailableLanguages() {
foreach ($this->getLanguagePaths() as $path) {
try {
$iterator = new \DirectoryIterator($path);
} catch (Exception $e) {
} catch (\Exception $e) {
continue;
}

Expand Down Expand Up @@ -636,6 +658,8 @@ public function getAvailableLanguages() {
* @param string $path path to a folder that contains translation files
*
* @return void
* @access private
* @internal
*/
public function registerLanguagePath($path) {
$this->language_paths[$path] = true;
Expand All @@ -645,6 +669,8 @@ public function registerLanguagePath($path) {
* Returns a unique array with locations of translation files
*
* @return array
* @access private
* @internal
*/
protected function getLanguagePaths() {
return array_keys($this->language_paths);
Expand All @@ -655,6 +681,8 @@ protected function getLanguagePaths() {
*
* @param string $language Language
* @return void
* @access private
* @internal
*/
private function ensureTranslationsLoaded($language) {
if (!$this->is_initialized) {
Expand All @@ -675,6 +703,8 @@ private function ensureTranslationsLoaded($language) {
* Returns an array of language codes.
*
* @return array
* @access private
* @internal
*/
public static function getAllLanguageCodes() {
return [
Expand Down Expand Up @@ -834,6 +864,8 @@ public static function getAllLanguageCodes() {
* @param string $code Language code
*
* @return string
* @access private
* @internal
*/
public static function normalizeLanguageCode($code) {
$code = strtolower($code);
Expand Down
33 changes: 33 additions & 0 deletions engine/lib/deprecated-3.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -1502,4 +1502,37 @@ function update_data($query, array $params = [], $get_num_rows = false) {
function delete_data($query, array $params = []) {
elgg_deprecated_notice(__FUNCTION__ . ' is deprecated. Use elgg()->db', '3.0');
return elgg()->db->deleteData($query, $params);
}

/**
* When given a full path, finds translation files and loads them
*
* @param string $path Full path
* @param bool $load_all If true all languages are loaded, if
* false only the current language + en are loaded
* @param string $language Language code if other than current + en
*
* @return bool success
* @deprecated 3.0
*/
function register_translations($path, $load_all = false, $language = null) {
elgg_deprecated_notice(__FUNCTION__ . ' is deprecated and should not be used', '3.0');

return elgg()->translator->registerTranslations($path, $load_all, $language);
}

/**
* Reload all translations from all registered paths.
*
* This is only called by functions which need to know all possible translations.
*
* @todo Better on demand loading based on language_paths array
*
* @return void
* @deprecated 3.0
*/
function reload_all_translations() {
elgg_deprecated_notice(__FUNCTION__ . ' is deprecated and should not be used', '3.0');

return elgg()->translator->reloadAllTranslations();
}
43 changes: 8 additions & 35 deletions engine/lib/languages.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* or the original language string.
*/
function elgg_echo($message_key, array $args = [], $language = "") {
return _elgg_services()->translator->translate($message_key, $args, $language);
return elgg()->echo($message_key, $args, $language);
}

/**
Expand All @@ -36,7 +36,7 @@ function elgg_echo($message_key, array $args = [], $language = "") {
* @return bool Depending on success
*/
function add_translation($country_code, $language_array) {
return _elgg_services()->translator->addTranslation($country_code, $language_array);
return elgg()->translator->addTranslation($country_code, $language_array);
}

/**
Expand All @@ -45,7 +45,7 @@ function add_translation($country_code, $language_array) {
* @return string The language code for the site/user or "en" if not set
*/
function get_current_language() {
return _elgg_services()->translator->getCurrentLanguage();
return elgg()->translator->getCurrentLanguage();
}

/**
Expand All @@ -54,34 +54,7 @@ function get_current_language() {
* @return string The language code (eg "en") or false if not set
*/
function get_language() {
return _elgg_services()->translator->detectLanguage();
}

/**
* When given a full path, finds translation files and loads them
*
* @param string $path Full path
* @param bool $load_all If true all languages are loaded, if
* false only the current language + en are loaded
* @param string $language Language code if other than current + en
*
* @return bool success
*/
function register_translations($path, $load_all = false, $language = null) {
return _elgg_services()->translator->registerTranslations($path, $load_all, $language);
}

/**
* Reload all translations from all registered paths.
*
* This is only called by functions which need to know all possible translations.
*
* @todo Better on demand loading based on language_paths array
*
* @return void
*/
function reload_all_translations() {
return _elgg_services()->translator->reloadAllTranslations();
return elgg()->translator->getCurrentLanguage();
}

/**
Expand All @@ -93,7 +66,7 @@ function reload_all_translations() {
* @return array
*/
function get_installed_translations($calculate_completeness = false) {
return _elgg_services()->translator->getInstalledTranslations($calculate_completeness);
return elgg()->translator->getInstalledTranslations($calculate_completeness);
}

/**
Expand All @@ -104,7 +77,7 @@ function get_installed_translations($calculate_completeness = false) {
* @return int
*/
function get_language_completeness($language) {
return _elgg_services()->translator->getLanguageCompleteness($language);
return elgg()->translator->getLanguageCompleteness($language);
}

/**
Expand All @@ -121,7 +94,7 @@ function get_language_completeness($language) {
* @since 1.11
*/
function elgg_language_key_exists($key, $language = 'en') {
return _elgg_services()->translator->languageKeyExists($key, $language);
return elgg()->translator->languageKeyExists($key, $language);
}

/**
Expand All @@ -131,5 +104,5 @@ function elgg_language_key_exists($key, $language = 'en') {
* @since 3.0
*/
function elgg_get_available_languages() {
return _elgg_services()->translator->getAvailableLanguages();
return elgg()->translator->getAvailableLanguages();
}
3 changes: 3 additions & 0 deletions engine/lib/sessions.php
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,9 @@ function login(\ElggUser $user, $persistent = false) {
// use elgg_get_logged_in_user_entity().
$session->setLoggedInUser($user);

// re-register at least the core language file for users with language other than site default
elgg()->translator->registerTranslations(\Elgg\Project\Paths::elgg() . 'languages/');

// if remember me checked, set cookie with token and store hash(token) for user
if ($persistent) {
_elgg_services()->persistentLogin->makeLoginPersistent($user);
Expand Down
2 changes: 2 additions & 0 deletions engine/services.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
use Elgg\Application\Database;
use Elgg\Di\PhpDiResolver;
use Elgg\Gatekeeper;
use Elgg\I18n\Translator;
use Elgg\Menu\Service as MenuService;
use Elgg\Views\TableColumn\ColumnFactory;

Expand All @@ -15,4 +16,5 @@
'menus' => new PhpDiResolver(MenuService::class, 'menus'),
'session' => new PhpDiResolver(ElggSession::class, 'session'),
'table_columns' => new PhpDiResolver(ColumnFactory::class, 'table_columns'),
'translator' => new PhpDiResolver(Translator::class, 'translator'),
];

0 comments on commit b46e11f

Please sign in to comment.