Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: missingKeyLogger case #1077

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions __tests__/transCore.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ const nsWithEmpty = {
emptyKey: '',
}

const nsConflictWithKeySeparatorKey = {
'key likes sentence.': 'message 1 conflict',
}

describe('transCore', () => {
test('should return an object of root keys', async () => {
const t = transCore({
Expand Down Expand Up @@ -153,4 +157,17 @@ describe('transCore', () => {
expect(typeof t).toBe('function')
expect(t('nsWithEmpty:emptyKey')).toEqual('nsWithEmpty:emptyKey')
})

test('should return key string when key conficts with separator and log warning.', () => {
const t = transCore({
config: {},
allNamespaces: { nsConflictWithKeySeparatorKey },
lang: 'en',
})

expect(typeof t).toBe('function')
expect(t('nsConflictWithKeySeparatorKey:key likes sentence.')).toEqual(
'nsConflictWithKeySeparatorKey:key likes sentence.'
)
})
})
1 change: 1 addition & 0 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface LoaderConfig extends I18nConfig {
export interface LoggerProps {
namespace: string | undefined
i18nKey: string
isKeyConflictWithKeySeparator?: boolean
}

export interface I18nLogger {
Expand Down
32 changes: 29 additions & 3 deletions src/transCore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ export default function transCore({

const t: Translate = (key = '', query, options) => {
const k = Array.isArray(key) ? key[0] : key
const { nsSeparator = ':', loggerEnvironment = 'browser' } = config
const {
nsSeparator = ':',
loggerEnvironment = 'browser',
keySeparator = '.',
} = config

const { i18nKey, namespace = options?.ns ?? config.defaultNS } = splitNsKey(
k,
Expand All @@ -77,6 +81,17 @@ export default function transCore({
(typeof value === 'object' && !Object.keys(value).length) ||
(value === '' && !allowEmptyStrings)

const keyParts = keySeparator
? keyWithPlural.split(keySeparator)
: [keyWithPlural]

const isKeyConflictWithKeySeparator =
keyWithPlural === keySeparator && options?.returnObjects
? false
: !!keySeparator &&
keyWithPlural.includes(keySeparator) &&
!keyParts.at(-1)

const fallbacks =
typeof options?.fallback === 'string'
? [options.fallback]
Expand All @@ -88,7 +103,7 @@ export default function transCore({
loggerEnvironment ===
(typeof window === 'undefined' ? 'node' : 'browser'))
) {
logger({ namespace, i18nKey })
logger({ namespace, i18nKey, isKeyConflictWithKeySeparator })
}

// Fallbacks
Expand Down Expand Up @@ -268,7 +283,11 @@ function objectInterpolation({
return obj
}

function missingKeyLogger({ namespace, i18nKey }: LoggerProps): void {
function missingKeyLogger({
namespace,
i18nKey,
isKeyConflictWithKeySeparator,
}: LoggerProps): void {
if (process.env.NODE_ENV === 'production') return

// This means that instead of "ns:value", "value" has been misspelled (without namespace)
Expand All @@ -278,6 +297,13 @@ function missingKeyLogger({ namespace, i18nKey }: LoggerProps): void {
)
return
}
// This means that key named likes "i am a sentence." will be regarded as nested keys which losts the last part of the key
if (isKeyConflictWithKeySeparator) {
console.warn(
`[next-translate] "${i18nKey}" is missing its last part of the key after key separator.`
)
return
}
console.warn(
`[next-translate] "${namespace}:${i18nKey}" is missing in current namespace configuration. Try adding "${i18nKey}" to the namespace "${namespace}".`
)
Expand Down