Skip to content

Commit

Permalink
Improve logger feedback messages (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
aralroca committed Oct 22, 2020
1 parent 1728a48 commit cceada9
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
26 changes: 26 additions & 0 deletions __tests__/Trans.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,32 @@ describe('Trans', () => {
expect(console.warn).toBeCalledWith(expected)
})

test('should log correctly if the value includes a ":", for example an URL', () => {
console.warn = jest.fn()
const i18nKey = 'ns:https://linkinsomelanguage.com'
const expected =
'[next-translate] "ns:https://linkinsomelanguage.com" is missing in current namespace configuration. Try adding "https://linkinsomelanguage.com" to the namespace "ns".'

const withSingular = {}
render(
<TestEnglish namespaces={{ ns: withSingular }} i18nKey={i18nKey} />
)
expect(console.warn).toBeCalledWith(expected)
})

test('should not log when the translation have ":" inside', () => {
console.warn = jest.fn()
const i18nKey = 'Some text without namespace'
const expected =
'[next-translate] The text "Some text without namespace" has no namespace in front of it.'

const withSingular = {}
render(
<TestEnglish namespaces={{ ns: withSingular }} i18nKey={i18nKey} />
)
expect(console.warn).toBeCalledWith(expected)
})

test('should log a warn key if a nested key does not exist in the namespace', () => {
console.warn = jest.fn()
const i18nKey = 'ns:parent.child'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "next-translate",
"version": "0.18.0-canary.1",
"version": "0.18.0-canary.2",
"description": "Next.js utility to translate pages without the need of a server (static i18n pages generator).",
"license": "MIT",
"keywords": [
Expand Down
10 changes: 9 additions & 1 deletion src/I18nProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ function objectInterpolation(obj, query) {

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

// This means that instead of "ns:value", "value" has been misspelled (without namespace)
if (!i18nKey) {
console.warn(
`[next-translate] The text "${namespace}" has no namespace in front of it.`
)
return
}
console.warn(
`[next-translate] "${namespace}:${i18nKey}" is missing in current namespace configuration. Try adding "${i18nKey}" to the namespace "${namespace}".`
)
Expand All @@ -77,7 +85,7 @@ export default function I18nProvider({

function t(key = '', query, options) {
const k = Array.isArray(key) ? key[0] : key
const [namespace, i18nKey] = k.split(':')
const [namespace, i18nKey] = k.split(/:(.+)/)
const dic = allNamespaces[namespace] || {}
const keyWithPlural = plural(dic, i18nKey, query)
const value = getDicValue(dic, keyWithPlural, options)
Expand Down

0 comments on commit cceada9

Please sign in to comment.