ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
SimpleSAML\Locale\Localization Class Reference
+ Collaboration diagram for SimpleSAML\Locale\Localization:

Public Member Functions

 __construct (\SimpleSAML_Configuration $configuration)
 Constructor. More...
 
 getLocaleDir ()
 Dump the default locale directory. More...
 
 getDomainLocaleDir ($domain)
 Get the default locale dir for a specific module aka. More...
 
 addModuleDomain ($module, $localeDir=null)
 
 addDomain ($localeDir, $domain)
 
 getLangPath ($domain=self::DEFAULT_DOMAIN)
 
 isI18NBackendDefault ()
 Test to check if backend is set to default. More...
 
 getRegisteredDomains ()
 Show which domains are registered. More...
 

Data Fields

const DEFAULT_DOMAIN = 'messages'
 The default gettext domain. More...
 
const SSP_I18N_BACKEND = 'SimpleSAMLphp'
 Old internationalization backend included in SimpleSAMLphp. More...
 
const GETTEXT_I18N_BACKEND = 'gettext/gettext'
 An internationalization backend implemented purely in PHP. More...
 

Private Member Functions

 setupTranslator ()
 Setup the translator. More...
 
 loadGettextGettextFromPO ($domain=self::DEFAULT_DOMAIN, $catchException=true)
 Load translation domain from Gettext/Gettext using .po. More...
 
 setupL10N ()
 Set up L18N if configured or fallback to old system. More...
 

Private Attributes

 $configuration
 
 $localeDir
 
 $localeDomainMap = array()
 
 $translator
 

Detailed Description

Definition at line 15 of file Localization.php.

Constructor & Destructor Documentation

◆ __construct()

SimpleSAML\Locale\Localization::__construct ( \SimpleSAML_Configuration  $configuration)

Constructor.

Parameters
\SimpleSAML_Configuration$configurationConfiguration object

Definition at line 61 of file Localization.php.

References SimpleSAML\Locale\Localization\$configuration, and SimpleSAML\Locale\Localization\setupL10N().

62  {
63  $this->configuration = $configuration;
64  $this->localeDir = $this->configuration->resolvePath('locales');
65  $this->language = new Language($configuration);
66  $this->langcode = $this->language->getPosixLanguage($this->language->getLanguage());
67  $this->i18nBackend = $this->configuration->getString('language.i18n.backend', self::SSP_I18N_BACKEND);
68  $this->setupL10N();
69  }
setupL10N()
Set up L18N if configured or fallback to old system.
+ Here is the call graph for this function:

Member Function Documentation

◆ addDomain()

SimpleSAML\Locale\Localization::addDomain (   $localeDir,
  $domain 
)

Definition at line 116 of file Localization.php.

References $domain, SimpleSAML\Locale\Localization\$localeDir, SimpleSAML\Logger\debug(), and SimpleSAML\Locale\Localization\loadGettextGettextFromPO().

Referenced by SimpleSAML\Locale\Localization\addModuleDomain(), and SimpleSAML\Locale\Localization\setupL10N().

117  {
118  $this->localeDomainMap[$domain] = $localeDir;
119  \SimpleSAML\Logger::debug("Localization: load domain '$domain' at '$localeDir'");
121  }
loadGettextGettextFromPO($domain=self::DEFAULT_DOMAIN, $catchException=true)
Load translation domain from Gettext/Gettext using .po.
static debug($string)
Definition: Logger.php:213
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ addModuleDomain()

SimpleSAML\Locale\Localization::addModuleDomain (   $module,
  $localeDir = null 
)

Definition at line 100 of file Localization.php.

References SimpleSAML\Locale\Localization\$localeDir, $module, SimpleSAML\Locale\Localization\addDomain(), and SimpleSAML\Locale\Localization\getDomainLocaleDir().

101  {
102  if (!$localeDir) {
104  }
105  $this->addDomain($localeDir, $module);
106  }
addDomain($localeDir, $domain)
if($modEnd===false) $module
Definition: module.php:59
getDomainLocaleDir($domain)
Get the default locale dir for a specific module aka.
+ Here is the call graph for this function:

◆ getDomainLocaleDir()

SimpleSAML\Locale\Localization::getDomainLocaleDir (   $domain)

Get the default locale dir for a specific module aka.

domain

Parameters
string$domainName of module/domain

Definition at line 86 of file Localization.php.

References $domain, and SimpleSAML\Locale\Localization\$localeDir.

Referenced by SimpleSAML\Locale\Localization\addModuleDomain().

87  {
88  $localeDir = $this->configuration->resolvePath('modules') . '/' . $domain . '/locales';
89  return $localeDir;
90  }
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
+ Here is the caller graph for this function:

◆ getLangPath()

SimpleSAML\Locale\Localization::getLangPath (   $domain = self::DEFAULT_DOMAIN)

Definition at line 129 of file Localization.php.

References $_SERVER, $domain, $error, SimpleSAML\Locale\Localization\$localeDir, SimpleSAML\Logger\critical(), SimpleSAML\Logger\debug(), and SimpleSAML\Logger\error().

Referenced by SimpleSAML\Locale\Localization\loadGettextGettextFromPO().

130  {
131  $langcode = explode('_', $this->langcode);
132  $langcode = $langcode[0];
133  $localeDir = $this->localeDomainMap[$domain];
134  $langPath = $localeDir.'/'.$langcode.'/LC_MESSAGES/';
135  \SimpleSAML\Logger::debug("Trying langpath for '$langcode' as '$langPath'");
136  if (is_dir($langPath) && is_readable($langPath)) {
137  return $langPath;
138  }
139 
140  // Some langcodes have aliases..
141  $alias = $this->language->getLanguageCodeAlias($langcode);
142  if (isset($alias)) {
143  $langPath = $localeDir.'/'.$alias.'/LC_MESSAGES/';
144  \SimpleSAML\Logger::debug("Trying langpath for alternative '$alias' as '$langPath'");
145  if (is_dir($langPath) && is_readable($langPath)) {
146  return $langPath;
147  }
148  }
149 
150  // Language not found, fall back to default
151  $defLangcode = $this->language->getDefaultLanguage();
152  $langPath = $localeDir.'/'.$defLangcode.'/LC_MESSAGES/';
153  if (is_dir($langPath) && is_readable($langPath)) {
154  // Report that the localization for the preferred language is missing
155  $error = "Localization not found for langcode '$langcode' at '$langPath', falling back to langcode '".
156  $defLangcode."'";
157  \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
158  return $langPath;
159  }
160 
161  // Locale for default language missing even, error out
162  $error = "Localization directory missing/broken for langcode '$langcode' and domain '$domain'";
163  \SimpleSAML\Logger::critical($_SERVER['PHP_SELF'].' - '.$error);
164  throw new \Exception($error);
165  }
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
static debug($string)
Definition: Logger.php:213
$error
Definition: Error.php:17
static error($string)
Definition: Logger.php:168
static critical($string)
Definition: Logger.php:146
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ getLocaleDir()

SimpleSAML\Locale\Localization::getLocaleDir ( )

Dump the default locale directory.

Definition at line 75 of file Localization.php.

References SimpleSAML\Locale\Localization\$localeDir.

76  {
77  return $this->localeDir;
78  }

◆ getRegisteredDomains()

SimpleSAML\Locale\Localization::getRegisteredDomains ( )

Show which domains are registered.

Definition at line 247 of file Localization.php.

References SimpleSAML\Locale\Localization\$localeDomainMap.

248  {
249  return $this->localeDomainMap;
250  }

◆ isI18NBackendDefault()

SimpleSAML\Locale\Localization::isI18NBackendDefault ( )

Test to check if backend is set to default.

(if false: backend unset/there's an error)

Definition at line 220 of file Localization.php.

221  {
222  if ($this->i18nBackend === $this::SSP_I18N_BACKEND) {
223  return true;
224  }
225  return false;
226  }

◆ loadGettextGettextFromPO()

SimpleSAML\Locale\Localization::loadGettextGettextFromPO (   $domain = self::DEFAULT_DOMAIN,
  $catchException = true 
)
private

Load translation domain from Gettext/Gettext using .po.

Note: Since Twig I18N does not support domains, all loaded files are merged. Use contexts if identical strings need to be disambiguated.

Parameters
string$domainName of domain
boolean$catchExceptionWhether to catch an exception on error or return early
Exceptions

Definition at line 189 of file Localization.php.

References $_SERVER, $domain, $error, SimpleSAML\Logger\error(), and SimpleSAML\Locale\Localization\getLangPath().

Referenced by SimpleSAML\Locale\Localization\addDomain().

190  {
191  try {
192  $langPath = $this->getLangPath($domain);
193  } catch (\Exception $e) {
194  $error = "Something went wrong when trying to get path to language file, cannot load domain '$domain'.";
195  \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
196  if ($catchException) {
197  // bail out!
198  return;
199  } else {
200  throw $e;
201  }
202  }
203  $poFile = $domain.'.po';
204  $poPath = $langPath.$poFile;
205  if (file_exists($poPath) && is_readable($poPath)) {
206  $translations = Translations::fromPoFile($poPath);
207  $this->translator->loadTranslations($translations);
208  } else {
209  $error = "Localization file '$poFile' not found in '$langPath', falling back to default";
210  \SimpleSAML\Logger::error($_SERVER['PHP_SELF'].' - '.$error);
211  }
212  }
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
$error
Definition: Error.php:17
static error($string)
Definition: Logger.php:168
getLangPath($domain=self::DEFAULT_DOMAIN)
if(!array_key_exists('domain', $_REQUEST)) $domain
Definition: resume.php:8
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setupL10N()

SimpleSAML\Locale\Localization::setupL10N ( )
private

Set up L18N if configured or fallback to old system.

Definition at line 232 of file Localization.php.

References SimpleSAML\Locale\Localization\addDomain(), SimpleSAML\Logger\debug(), and SimpleSAML\Locale\Localization\setupTranslator().

Referenced by SimpleSAML\Locale\Localization\__construct().

233  {
234  if ($this->i18nBackend === self::SSP_I18N_BACKEND) {
235  \SimpleSAML\Logger::debug("Localization: using old system");
236  return;
237  }
238 
239  $this->setupTranslator();
240  // setup default domain
241  $this->addDomain($this->localeDir, self::DEFAULT_DOMAIN);
242  }
addDomain($localeDir, $domain)
setupTranslator()
Setup the translator.
static debug($string)
Definition: Logger.php:213
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ setupTranslator()

SimpleSAML\Locale\Localization::setupTranslator ( )
private

Setup the translator.

Definition at line 171 of file Localization.php.

Referenced by SimpleSAML\Locale\Localization\setupL10N().

172  {
173  $this->translator = new Translator();
174  $this->translator->register();
175  }
+ Here is the caller graph for this function:

Field Documentation

◆ $configuration

SimpleSAML\Locale\Localization::$configuration
private

Definition at line 23 of file Localization.php.

Referenced by SimpleSAML\Locale\Localization\__construct().

◆ $localeDir

◆ $localeDomainMap

SimpleSAML\Locale\Localization::$localeDomainMap = array()
private

◆ $translator

SimpleSAML\Locale\Localization::$translator
private

Definition at line 53 of file Localization.php.

◆ DEFAULT_DOMAIN

const SimpleSAML\Locale\Localization::DEFAULT_DOMAIN = 'messages'

The default gettext domain.

Definition at line 28 of file Localization.php.

◆ GETTEXT_I18N_BACKEND

const SimpleSAML\Locale\Localization::GETTEXT_I18N_BACKEND = 'gettext/gettext'

An internationalization backend implemented purely in PHP.

Definition at line 38 of file Localization.php.

◆ SSP_I18N_BACKEND

const SimpleSAML\Locale\Localization::SSP_I18N_BACKEND = 'SimpleSAMLphp'

Old internationalization backend included in SimpleSAMLphp.

Definition at line 33 of file Localization.php.


The documentation for this class was generated from the following file: