Skip to content

Commit

Permalink
fix: implement scopes/namespaces for Transloco (#684)
Browse files Browse the repository at this point in the history
* Update Transloco documentation hints
* Fix comment typo
* Implement scopes/namespaces for Transloco
  • Loading branch information
openscript committed May 18, 2023
1 parent f08e712 commit 43df97d
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/core/Config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class Config {

static get regexKey(): string {
return this.getConfig('regex.key')
|| this.getConfig('keyMatchRegex') // back compatible, depreacted.
|| this.getConfig('keyMatchRegex') // back compatible, deprecated.
|| (Config.disablePathParsing ? KEY_REG_ALL : KEY_REG_DEFAULT)
}

Expand Down
35 changes: 31 additions & 4 deletions src/frameworks/transloco.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@ export default class TranslocoFramework extends Framework {
]

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

Expand All @@ -36,6 +38,31 @@ export default class TranslocoFramework extends Framework {
]
}

rewriteKeys(key: string) {
// find extra scope
const regex = /[\'"`]([\w.]+)[\'"`]/gm
let index = 0
let match, actualKey, scope

// eslint-disable-next-line no-cond-assign
while ((match = regex.exec(key)) !== null) {
// this is necessary to avoid infinite loops with zero-width matches
if (match.index === regex.lastIndex)
regex.lastIndex++

if (index === 0)
actualKey = match[1]

if (index === 1)
scope = match[1]

index++
}

// return new key if the extra scope regex matched
return actualKey && scope ? `${scope}.${actualKey}` : key
}

// support for `read` syntax
// https://ngneat.github.io/transloco/docs/translation-in-the-template#utilizing-the-read-input
getScopeRange(document: TextDocument): ScopeRange[] | undefined {
Expand Down

0 comments on commit 43df97d

Please sign in to comment.