import { Framework } from './base' import { RewriteKeySource, RewriteKeyContext } from '~/core' import { LanguageId } from '~/utils' class I18nextFramework extends Framework { id ='i18next' display = 'i18next' namespaceDelimiter = ':' // both `/` and `:` should work as delimiter, #425 namespaceDelimiters = [':', '/'] namespaceDelimitersRegex = /[:/]/g detection = { packageJSON: { any: [ 'i18next', ], none: [ 'react-i18next', ], }, } languageIds: LanguageId[] = [ 'javascript', 'typescript', 'javascriptreact', 'typescriptreact', 'ejs', ] // for visualize the regex, you can use https://regexper.com/ usageMatchRegex = [ '(?:i18next|i18n|req)\\.t\\(\\s*[\'"`]({key})[\'"`]', ] derivedKeyRules = [ '{key}_plural', '{key}_0', '{key}_1', '{key}_2', '{key}_3', '{key}_4', '{key}_5', '{key}_6', '{key}_7', '{key}_8', '{key}_9', // support v4 format as well as v3 '{key}_zero', '{key}_one', '{key}_two', '{key}_few', '{key}_many', '{key}_other' ] refactorTemplates(keypath: string) { return [ keypath, ] } rewriteKeys(key: string, source: RewriteKeySource, context: RewriteKeyContext = {}) { const dottedKey = key.split(this.namespaceDelimitersRegex).join('.') // when explicitly set the namespace, ignore current namespace scope if ( this.namespaceDelimiters.some(d => key.includes(d)) && context.namespace && dottedKey.startsWith(context.namespace.split(this.namespaceDelimitersRegex).join('.')) ) // +1 for the an extra `.` key = key.slice(context.namespace.length + 1) // replace colons return dottedKey } } export default I18nextFramework