Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Toolkit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
digital blueprint
Web Component Framework
Toolkit
Commits
846e2070
Commit
846e2070
authored
Jul 19, 2022
by
Kocher, Manuel
Browse files
Options
Downloads
Patches
Plain Diff
Add better errorr handling to translation component
parent
eb4d8750
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
packages/common/i18next.js
+1
-1
1 addition, 1 deletion
packages/common/i18next.js
packages/common/src/i18n.js
+2
-2
2 additions, 2 deletions
packages/common/src/i18n.js
packages/common/src/translation.js
+17
-15
17 additions, 15 deletions
packages/common/src/translation.js
with
20 additions
and
18 deletions
packages/common/i18next.js
+
1
−
1
View file @
846e2070
...
@@ -78,7 +78,7 @@ export function humanFileSize(bytes, si = false) {
...
@@ -78,7 +78,7 @@ export function humanFileSize(bytes, si = false) {
* @param {string} namespace The namespace to override
* @param {string} namespace The namespace to override
* @returns {string} The new namespace name
* @returns {string} The new namespace name
*/
*/
function
getOverrideNamespace
(
namespace
)
{
export
function
getOverrideNamespace
(
namespace
)
{
// This just needs to be different to the namespace, make it special
// This just needs to be different to the namespace, make it special
// so it's clear what it is used for in case it ends up in some error
// so it's clear what it is used for in case it ends up in some error
// message or something
// message or something
...
...
This diff is collapsed.
Click to expand it.
packages/common/src/i18n.js
+
2
−
2
View file @
846e2070
import
{
createInstance
as
_createInstance
,
setOverridesByGlobalCache
}
from
'
../i18next.js
'
;
import
{
createInstance
as
_createInstance
,
setOverridesByGlobalCache
,
getOverrideNamespace
}
from
'
../i18next.js
'
;
import
de
from
'
./i18n/de/translation.json
'
;
import
de
from
'
./i18n/de/translation.json
'
;
import
en
from
'
./i18n/en/translation.json
'
;
import
en
from
'
./i18n/en/translation.json
'
;
...
@@ -11,4 +11,4 @@ export function createInstanceGivenResources(en, de) {
...
@@ -11,4 +11,4 @@ export function createInstanceGivenResources(en, de) {
return
_createInstance
({
en
:
en
,
de
:
de
},
'
de
'
,
'
en
'
);
return
_createInstance
({
en
:
en
,
de
:
de
},
'
de
'
,
'
en
'
);
}
}
export
{
setOverridesByGlobalCache
};
export
{
setOverridesByGlobalCache
,
getOverrideNamespace
};
This diff is collapsed.
Click to expand it.
packages/common/src/translation.js
+
17
−
15
View file @
846e2070
...
@@ -2,7 +2,7 @@ import {html} from 'lit';
...
@@ -2,7 +2,7 @@ import {html} from 'lit';
import
{
unsafeHTML
}
from
'
lit/directives/unsafe-html.js
'
;
import
{
unsafeHTML
}
from
'
lit/directives/unsafe-html.js
'
;
import
DBPLitElement
from
'
../dbp-lit-element
'
;
import
DBPLitElement
from
'
../dbp-lit-element
'
;
import
*
as
commonStyles
from
'
../styles.js
'
;
import
*
as
commonStyles
from
'
../styles.js
'
;
import
{
createInstanceGivenResour
ce
s
}
from
'
./i18n.js
'
;
import
{
getOverrideNamespa
ce
}
from
'
./i18n.js
'
;
export
class
Translation
extends
DBPLitElement
{
export
class
Translation
extends
DBPLitElement
{
constructor
()
{
constructor
()
{
...
@@ -33,16 +33,6 @@ export class Translation extends DBPLitElement {
...
@@ -33,16 +33,6 @@ export class Translation extends DBPLitElement {
}
}
connectedCallback
()
{
connectedCallback
()
{
// init objects with empty string as value for key
const
de
=
{};
const
en
=
{};
de
[
this
.
key
]
=
""
;
en
[
this
.
key
]
=
""
;
// create i18n instance with empty translations as default
this
.
_i18n
=
createInstanceGivenResources
(
en
,
de
);
// supercall after default i18n init to override translations only
// supercall after default i18n init to override translations only
// if a override with this tagname is given
// if a override with this tagname is given
super
.
connectedCallback
();
super
.
connectedCallback
();
...
@@ -62,8 +52,14 @@ export class Translation extends DBPLitElement {
...
@@ -62,8 +52,14 @@ export class Translation extends DBPLitElement {
}
}
render
()
{
render
()
{
// get name of overridenamespace
let
overrideNamespace
=
this
.
_i18n
.
options
.
fallbackNS
.
slice
(
0
,
2
)
==
"
--
"
?
this
.
_i18n
.
options
.
fallbackNS
:
getOverrideNamespace
(
this
.
_i18n
.
options
.
fallbackNS
);
// request to i18n translation
// request to i18n translation
cons
t
translation
=
(()
=>
{
le
t
translation
=
(()
=>
{
if
(
this
.
interpolation
&&
this
.
unsafe
)
if
(
this
.
interpolation
&&
this
.
unsafe
)
return
unsafeHTML
(
this
.
_i18n
.
t
(
this
.
key
,
this
.
interpolation
));
return
unsafeHTML
(
this
.
_i18n
.
t
(
this
.
key
,
this
.
interpolation
));
else
if
(
this
.
interpolation
)
else
if
(
this
.
interpolation
)
...
@@ -72,12 +68,18 @@ export class Translation extends DBPLitElement {
...
@@ -72,12 +68,18 @@ export class Translation extends DBPLitElement {
return
this
.
_i18n
.
t
(
this
.
key
);
return
this
.
_i18n
.
t
(
this
.
key
);
})();
})();
// if translation == "" key was not found
// check if overrides have been loaded with overrideNamespace
// and then check if given key exists
let
key
=
""
;
let
key
=
""
;
if
(
t
ranslation
!=
""
)
{
if
(
t
his
.
_i18n
.
exists
(
this
.
key
)
&&
this
.
_i18n
.
hasResourceBundle
(
this
.
lang
,
overrideNamespace
)
)
{
key
=
unsafeHTML
(
"
<!-- key:
"
+
this
.
key
+
"
-->
"
);
key
=
unsafeHTML
(
"
<!-- key:
"
+
this
.
key
+
"
-->
"
);
}
else
if
(
this
.
_i18n
.
hasResourceBundle
(
this
.
lang
,
overrideNamespace
)){
key
=
unsafeHTML
(
"
<!-- key '
"
+
this
.
key
+
"
' not found! -->
"
);
translation
=
""
;
console
.
error
(
"
Key '
"
+
this
.
key
+
"
' not found!
"
);
}
else
{
}
else
{
key
=
unsafeHTML
(
"
<!-- key
\"
"
+
this
.
key
+
"
\"
not found! -->
"
);
key
=
unsafeHTML
(
"
<!-- key '
"
+
this
.
key
+
"
' and namespace '
"
+
overrideNamespace
+
"
' not found! -->
"
);
translation
=
""
;
}
}
// load translation text
// load translation text
...
...
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