Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
nghyd
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container Registry
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Cassiopée
nghyd
Commits
48a47071
Commit
48a47071
authored
6 years ago
by
mathias.chouet
Browse files
Options
Downloads
Patches
Plain Diff
Traduction: en mode "prod", gestion d'une langue de secours
parent
1811aa97
No related branches found
Branches containing commit
No related tags found
Tags containing commit
1 merge request
!37
Resolve "Refonte du chargement des fichiers de langue et gestion des langues de substitution"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/app/services/internationalisation/internationalisation.service.ts
+63
-21
63 additions, 21 deletions
...ices/internationalisation/internationalisation.service.ts
with
63 additions
and
21 deletions
src/app/services/internationalisation/internationalisation.service.ts
+
63
−
21
View file @
48a47071
import
{
Injectable
}
from
"
@angular/core
"
;
import
{
Injectable
,
isDevMode
}
from
"
@angular/core
"
;
import
{
Message
,
MessageCode
,
Observable
,
Observer
}
from
"
jalhyd
"
;
...
...
@@ -18,6 +18,9 @@ export class I18nService extends Observable implements Observer {
/** localized messages */
private
_Messages
:
StringMap
;
/** localized messages in fallback language (the one in the config file) */
private
_fallbackMessages
:
StringMap
;
constructor
(
private
applicationSetupService
:
ApplicationSetupService
,
private
httpService
:
HttpService
...
...
@@ -27,6 +30,10 @@ export class I18nService extends Observable implements Observer {
fr
:
"
Français
"
,
en
:
"
English
"
};
// load fallback language messages once for all
this
.
httpGetMessages
(
this
.
applicationSetupService
.
fallbackLanguage
).
then
((
res
:
any
)
=>
{
this
.
_fallbackMessages
=
res
;
});
// add language preferences observer
this
.
applicationSetupService
.
addObserver
(
this
);
}
...
...
@@ -53,7 +60,7 @@ export class I18nService extends Observable implements Observer {
public
setLanguage
(
code
:
string
)
{
// is language supported ?
if
(
!
Object
.
keys
(
this
.
_availableLanguages
).
includes
(
code
))
{
throw
new
Error
(
`
ERROR_
LANGUAGE_UNSUPPORTED "
${
code
}
"`
);
throw
new
Error
(
`LANGUAGE_UNSUPPORTED "
${
code
}
"`
);
}
// did language change ?
if
(
this
.
_currentLanguage
!==
code
)
{
...
...
@@ -62,7 +69,8 @@ export class I18nService extends Observable implements Observer {
this
.
_Messages
=
undefined
;
// reload all messages
const
that
=
this
;
this
.
httpGetMessages
().
then
((
res
)
=>
{
this
.
httpGetMessages
(
code
).
then
((
res
:
any
)
=>
{
that
.
_Messages
=
res
;
// propagate language change to all application
that
.
notifyObservers
(
undefined
);
});
...
...
@@ -70,39 +78,73 @@ export class I18nService extends Observable implements Observer {
}
/**
* Loads localized messages from JSON files
,
for the
curr
en
t
language
* (general message file, not calculator-specific ones)
* Loads localized messages from JSON files for the
giv
en language
* (general message
s
file
s
, not calculator-specific ones)
*/
private
httpGetMessages
():
Promise
<
void
>
{
const
that
=
this
;
const
file
Name
=
"
messages.
"
+
this
.
_currentLanguage
+
"
.json
"
;
return
this
.
httpService
.
httpGetRequestPromise
(
"
locale/
"
+
fileName
).
then
(
(
re
s
:
any
)
=>
{
that
.
_Messages
=
res
;
}
);
private
httpGetMessages
(
lang
:
string
):
Promise
<
void
>
{
const
fileName
=
"
messages.
"
+
lang
+
"
.json
"
;
const
file
Path
=
"
locale/
"
+
fileName
;
return
this
.
httpService
.
httpGetRequestPromise
(
filePath
).
then
((
res
:
any
)
=>
{
re
turn
res
;
}
);
}
private
getMessageFromCode
(
c
:
MessageCode
):
string
{
if
(
!
this
.
_Messages
)
{
return
`***
M
essages not loaded yet ***`
;
return
`***
m
essages not loaded yet ***`
;
}
if
(
this
.
_Messages
[
MessageCode
[
c
]]
===
undefined
)
{
return
`***
M
essage
${
MessageCode
[
c
]}
non traduit
***`
;
return
`***
m
essage
not found
${
MessageCode
[
c
]}
***`
;
}
return
this
.
_Messages
[
MessageCode
[
c
]];
}
/**
* Traduit un texte défini dans un fichier de langue, à partir de sa clé
* Translates a text from its key.
*
* In production mode, looks in different messages collections :
* 1. ${msg} if provided
* 2. messages for current language
* 3. messages for fallback language
*
* In dev mode, looks only in 1. if provided, else only in 2. which makes missing
* translations easier to detect
*
* @param textKey id du texte (ex: "ERROR_PARAM_NULL")
*/
public
localizeText
(
textKey
:
string
,
messages
=
this
.
_Messages
)
{
if
(
messages
===
undefined
)
{
return
`*** messages not loaded:
${
this
.
_currentLanguage
}
***`
;
}
if
(
messages
[
textKey
]
===
undefined
)
{
return
`*** message not found:
${
textKey
}
***`
;
public
localizeText
(
textKey
:
string
,
msg
?:
StringMap
)
{
if
(
isDevMode
())
{
// expose missing translations
if
(
msg
)
{
if
(
msg
[
textKey
]
===
undefined
)
{
return
`*** message not found:
${
textKey
}
***`
;
}
return
msg
[
textKey
];
}
else
{
if
(
!
this
.
_Messages
)
{
return
`*** messages not loaded:
${
this
.
_currentLanguage
}
***`
;
}
if
(
this
.
_Messages
[
textKey
]
===
undefined
)
{
return
`*** message not found:
${
textKey
}
***`
;
}
return
this
.
_Messages
[
textKey
];
}
}
else
{
const
messages
=
msg
||
this
.
_Messages
;
if
(
!
messages
)
{
return
`*** messages not loaded:
${
this
.
_currentLanguage
}
***`
;
}
if
(
messages
[
textKey
]
===
undefined
)
{
// try fallback language before giving up
if
(
this
.
_fallbackMessages
[
textKey
]
===
undefined
)
{
return
`*** message not found:
${
textKey
}
***`
;
}
else
{
return
this
.
_fallbackMessages
[
textKey
];
}
}
else
{
return
messages
[
textKey
];
}
}
return
messages
[
textKey
];
}
/**
...
...
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