feat(scoped): support css selector scoped (#42)

* feat(scoped): support css selector scoped

https://github.com/journey-ad/sd-webui-bilingual-localization/issues/41

* fix: avoid empty

* .
main
bluelovers 2023-08-29 08:28:34 +08:00 committed by GitHub
parent cf54c4eb03
commit e79b78402f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 15 additions and 4 deletions

View File

@ -116,7 +116,7 @@
logger.log('Bilingual Localization initialized.')
// Load localization file
const regex_scope = /^##(?<scope>\S+)##(?<skey>\S+)$/ // ##scope##.skey
const regex_scope = /^##(?<scope>.+)##(?<skey>.+)$/ // ##scope##.skey
i18n = JSON.parse(readFile(dirs[file]), (key, value) => {
// parse regex translations
if (key.startsWith('@@')) {
@ -126,7 +126,18 @@
}
} else if (regex_scope.test(key)) {
// parse scope translations
const { scope, skey } = key.match(regex_scope).groups
let { scope, skey } = key.match(regex_scope).groups
if (scope.startsWith('@')) {
scope = scope.slice(1)
} else {
scope = '#' + scope
}
if (!scope.length) {
return value
}
i18nScope[scope] ||= {}
i18nScope[scope][skey] = value
@ -267,9 +278,9 @@
scopes = scopedSource[source]
if (scopes) {
console.log('scope', el, source);
console.log('scope', el, source, scopes);
for (let scope of scopes) {
if (el.parentElement.closest(`#${scope}`)) {
if (el.parentElement.closest(scope)) {
translation = i18nScope[scope][source]
break
}