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
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
578885ba
Commit
578885ba
authored
2 years ago
by
Kocher, Manuel
Browse files
Options
Downloads
Patches
Plain Diff
Move translation caching to i18next.js
parent
ea6bbee2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
packages/common/i18next.js
+34
-4
34 additions, 4 deletions
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
+2
-29
2 additions, 29 deletions
packages/common/src/translation.js
with
38 additions
and
35 deletions
packages/common/i18next.js
+
34
−
4
View file @
578885ba
import
i18next
from
'
i18next
'
;
// global variable as cache for translations
const
translationCache
=
{};
// fetches overrides for given language
async
function
fetchOverridesByLanguage
(
overrides
,
lng
)
{
let
result
=
await
fetch
(
overrides
+
lng
+
'
/translation.json
'
,
{
headers
:
{
'
Content-Type
'
:
'
application/json
'
},
});
let
json
=
await
result
.
json
();
return
json
;
}
// handles translation cache promises
async
function
cacheOverrides
(
overridesFile
,
lng
)
{
// use global var as cache
if
(
translationCache
[
lng
]
===
undefined
)
{
// get translation.json for each lang
let
response
=
fetchOverridesByLanguage
(
overridesFile
,
lng
);
translationCache
[
lng
]
=
response
;
return
response
;
}
else
{
return
translationCache
[
lng
];
}
}
/**
* Like Intl.DateTimeFormat().format() but uses the current language as locale.
*
...
...
@@ -135,21 +161,25 @@ export function setOverrides(i18n, element, overrides) {
* @param {HTMLElement} element - The element at which the overrides are targeted
* @param {object} overrides - The override data as promise
*/
export
async
function
setOverridesBy
Promis
e
(
i18n
,
element
,
overrides
)
{
export
async
function
setOverridesBy
GlobalCach
e
(
i18n
,
element
)
{
// We add a special namespace which gets used with priority and falls back
// to the original one. This way we an change the overrides at runtime
// and can even remove them.
// The scoped mixin saves the real tag name under data-tag-name
let
tagName
=
((
element
.
dataset
&&
element
.
dataset
.
tagName
)
||
element
.
tagName
).
toLowerCase
();
let
namespace
=
i18n
.
options
.
fallbackNS
;
let
overrideNamespace
=
getOverrideNamespace
(
namespace
);
let
hasOverrides
=
false
;
console
.
log
(
element
.
langDir
);
for
(
let
lng
of
i18n
.
languages
)
{
overrides
[
lng
]
=
await
overrides
[
lng
];
cacheOverrides
(
element
.
langDir
,
lng
);
translationCache
[
lng
]
=
await
translationCache
[
lng
];
i18n
.
removeResourceBundle
(
lng
,
overrideNamespace
);
if
(
overrides
[
lng
]
===
undefined
||
overrides
[
lng
][
tagName
]
===
undefined
)
continue
;
let
resources
=
overrides
[
lng
][
tagName
];
if
(
translationCache
[
lng
]
===
undefined
||
translationCache
[
lng
][
tagName
]
===
undefined
)
continue
;
let
resources
=
translationCache
[
lng
][
tagName
];
hasOverrides
=
true
;
i18n
.
addResourceBundle
(
lng
,
overrideNamespace
,
resources
);
}
...
...
This diff is collapsed.
Click to expand it.
packages/common/src/i18n.js
+
2
−
2
View file @
578885ba
import
{
createInstance
as
_createInstance
,
setOverridesBy
Promis
e
}
from
'
../i18next.js
'
;
import
{
createInstance
as
_createInstance
,
setOverridesBy
GlobalCach
e
}
from
'
../i18next.js
'
;
import
de
from
'
./i18n/de/translation.json
'
;
import
en
from
'
./i18n/en/translation.json
'
;
...
...
@@ -11,4 +11,4 @@ export function createInstanceGivenResources(en, de) {
return
_createInstance
({
en
:
en
,
de
:
de
},
'
de
'
,
'
en
'
);
}
export
{
setOverridesBy
Promis
e
};
export
{
setOverridesBy
GlobalCach
e
};
This diff is collapsed.
Click to expand it.
packages/common/src/translation.js
+
2
−
29
View file @
578885ba
import
{
css
,
html
}
from
'
lit
'
;
import
{
unsafeHTML
}
from
'
lit/directives/unsafe-html.js
'
;
import
DBPLitElement
from
'
../dbp-lit-element
'
;
import
{
createInstanceGivenResources
,
setOverridesByPromise
}
from
'
./i18n.js
'
;
// global variable as cache for translations
const
translationCache
=
{};
// fetches overrides for given language
async
function
fetchOverridesByLanguage
(
overrides
,
lng
)
{
let
result
=
await
fetch
(
overrides
+
lng
+
'
/translation.json
'
,
{
headers
:
{
'
Content-Type
'
:
'
application/json
'
},
});
let
json
=
await
result
.
json
();
return
json
;
}
// handles translation cache promises
async
function
cacheOverrides
(
overridesFile
,
lng
)
{
// use global var as cache
if
(
translationCache
[
lng
]
===
undefined
)
{
// get translation.json for each lang
let
response
=
fetchOverridesByLanguage
(
overridesFile
,
lng
);
translationCache
[
lng
]
=
response
;
return
response
;
}
else
{
return
translationCache
[
lng
];
}
}
import
{
createInstanceGivenResources
,
setOverridesByGlobalCache
}
from
'
./i18n.js
'
;
export
class
Translation
extends
DBPLitElement
{
constructor
()
{
...
...
@@ -72,8 +46,7 @@ export class Translation extends DBPLitElement {
if
(
this
.
langDir
)
{
for
(
let
lng
of
this
.
_i18n
.
languages
)
{
cacheOverrides
(
this
.
langDir
,
lng
);
setOverridesByPromise
(
this
.
_i18n
,
this
,
translationCache
);
setOverridesByGlobalCache
(
this
.
_i18n
,
this
);
}
}
}
...
...
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