Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
simplesamlphp
Manage
Activity
Members
Labels
Plan
Jira
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Analyze
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
Perun
Perun ProxyIdP
v1
simplesamlphp
Commits
e72501a9
Commit
e72501a9
authored
8 years ago
by
Hanne Moa
Browse files
Options
Downloads
Patches
Plain Diff
Load translation files safely
parent
80c1a463
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
lib/SimpleSAML/Locale/Localization.php
+47
-6
47 additions, 6 deletions
lib/SimpleSAML/Locale/Localization.php
with
47 additions
and
6 deletions
lib/SimpleSAML/Locale/Localization.php
+
47
−
6
View file @
e72501a9
...
...
@@ -80,6 +80,38 @@ class Localization
$this
->
localeDomainMap
[
$domain
]
=
$localeDir
;
}
/*
* Get and check path of localization file
*
* @param string $domain Name of localization domain
* @throws Exception If the path does not exist even for the default, fallback language
*/
public
function
getLangPath
(
$domain
=
self
::
DEFAULT_DOMAIN
)
{
$langcode
=
explode
(
'_'
,
$this
->
langcode
);
$langcode
=
$langcode
[
0
];
$localeDir
=
$this
->
localeDomainMap
[
$domain
];
$langPath
=
$localeDir
.
'/'
.
$langcode
.
'/LC_MESSAGES/'
;
if
(
is_dir
(
$langPath
)
&&
is_readable
(
$langPath
))
{
return
$langPath
;
}
// Language not found, fall back to default
$defLangcode
=
$this
->
language
->
getDefaultLanguage
();
$langPath
=
$localeDir
.
'/'
.
$defLangcode
.
'/LC_MESSAGES/'
;
if
(
is_dir
(
$langPath
)
&&
is_readable
(
$langPath
))
{
// Report that the localization for the preferred language is missing
$error
=
"Localization not found for langcode '
$langcode
' at '
$langPath
', falling back to langcode '
$defLangcode
'"
;
\SimpleSAML_Logger
::
error
(
$_SERVER
[
'PHP_SELF'
]
.
' - '
.
$error
);
return
$langPath
;
}
// Locale for default language missing even, error out
$error
=
"Localization directory missing/broken for langcode '
$langcode
' and domain '
$domain
'"
;
\SimpleSAML_Logger
::
critical
(
$_SERVER
[
'PHP_SELF'
]
.
' - '
.
$error
);
throw
new
Exception
(
$error
);
}
/**
* Load translation domain from Gettext/Gettext using .po
...
...
@@ -87,14 +119,23 @@ class Localization
* @param string $domain Name of domain
*/
private
function
loadGettextGettextFromPO
(
$domain
=
self
::
DEFAULT_DOMAIN
)
{
$langcode
=
explode
(
'_'
,
$this
->
langcode
);
$langcode
=
$langcode
[
0
];
$localeDir
=
$this
->
localeDomainMap
[
$domain
];
$poPath
=
$localeDir
.
'/'
.
$langcode
.
'/LC_MESSAGES/'
.
$domain
.
'.po'
;
$translations
=
Translations
::
fromPoFile
(
$poPath
);
$t
=
new
Translator
();
$t
->
loadTranslations
(
$translations
);
$t
->
register
();
try
{
$langPath
=
$this
->
getLangPath
(
$domain
);
}
catch
(
\Exception
$e
)
{
// bail out!
return
;
}
$poFile
=
$domain
.
'.po'
;
$poPath
=
$langPath
.
$poFile
;
if
(
file_exists
(
$poPath
)
&&
is_readable
(
$poPath
))
{
$translations
=
Translations
::
fromPoFile
(
$poPath
);
$t
->
loadTranslations
(
$translations
);
}
else
{
$error
=
"Localization file '
$poFile
' not found in '
$langPath
', falling back to default"
;
\SimpleSAML_Logger
::
error
(
$_SERVER
[
'PHP_SELF'
]
.
' - '
.
$error
);
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment