Skip to content

Commit 43df97d

Browse files
authored
fix: implement scopes/namespaces for Transloco (#684)
* Update Transloco documentation hints * Fix comment typo * Implement scopes/namespaces for Transloco
1 parent f08e712 commit 43df97d

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/core/Config.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ export class Config {
198198

199199
static get regexKey(): string {
200200
return this.getConfig('regex.key')
201-
|| this.getConfig('keyMatchRegex') // back compatible, depreacted.
201+
|| this.getConfig('keyMatchRegex') // back compatible, deprecated.
202202
|| (Config.disablePathParsing ? KEY_REG_ALL : KEY_REG_DEFAULT)
203203
}
204204

src/frameworks/transloco.ts

+31-4
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ export default class TranslocoFramework extends Framework {
2020
]
2121

2222
usageMatchRegex = [
23-
// https://netbasal.gitbook.io/transloco/translation-in-the-template/pipe
23+
// https://ngneat.github.io/transloco/docs/translation-in-the-template#pipe
2424
'[`\'"]({key})[`\'"][\\s\\n]*\\|[\\s\\n]*transloco',
25-
// https://netbasal.gitbook.io/transloco/translation-in-the-template/structural-directive
26-
'[^\\w\\d](?:t|translate|selectTranslate|getTranslateObject|selectTranslateObject|getTranslation|setTranslationKey)\\([\\s\\n]*[\'"`]({key})[\'"`]',
27-
// https://netbasal.gitbook.io/transloco/translation-in-the-template/attribute-directive
25+
// https://ngneat.github.io/transloco/docs/translation-in-the-template#structural-directive
26+
'[^\\w\\d](?:t)\\([\\s\\n]*[\'"`]({key})[\'"`]',
27+
// https://ngneat.github.io/transloco/docs/translation-api
28+
'[^\\w\\d](?:translate|selectTranslate|getTranslateObject|selectTranslateObject|getTranslation|setTranslationKey)\\([\\s\\n]*(.*?)[\\s\\n]*\\)',
29+
// https://ngneat.github.io/transloco/docs/translation-in-the-template#attribute-directive
2830
'[^*\\w\\d]transloco=[\'"`]({key})[\'"`]',
2931
]
3032

@@ -36,6 +38,31 @@ export default class TranslocoFramework extends Framework {
3638
]
3739
}
3840

41+
rewriteKeys(key: string) {
42+
// find extra scope
43+
const regex = /[\'"`]([\w.]+)[\'"`]/gm
44+
let index = 0
45+
let match, actualKey, scope
46+
47+
// eslint-disable-next-line no-cond-assign
48+
while ((match = regex.exec(key)) !== null) {
49+
// this is necessary to avoid infinite loops with zero-width matches
50+
if (match.index === regex.lastIndex)
51+
regex.lastIndex++
52+
53+
if (index === 0)
54+
actualKey = match[1]
55+
56+
if (index === 1)
57+
scope = match[1]
58+
59+
index++
60+
}
61+
62+
// return new key if the extra scope regex matched
63+
return actualKey && scope ? `${scope}.${actualKey}` : key
64+
}
65+
3966
// support for `read` syntax
4067
// https://ngneat.github.io/transloco/docs/translation-in-the-template#utilizing-the-read-input
4168
getScopeRange(document: TextDocument): ScopeRange[] | undefined {

0 commit comments

Comments
 (0)