Skip to content

Commit

Permalink
Merge pull request #109 from nextcloud/fix/only-log-in-debug-mode
Browse files Browse the repository at this point in the history
Only log to console.warn when debug mode is active
  • Loading branch information
ChristophWurst authored Mar 31, 2020
2 parents 1801b66 + 560f921 commit e3526a0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
14 changes: 11 additions & 3 deletions lib/gettext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class GettextBuilder {

private locale?: string
private translations = {}
private debug = false

setLanguage(language: string): GettextBuilder {
this.locale = language
Expand All @@ -21,8 +22,13 @@ class GettextBuilder {
return this
}

enableDebugMode(): GettextBuilder {
this.debug = true
return this
}

build(): GettextWrapper {
return new GettextWrapper(this.locale || 'en', this.translations)
return new GettextWrapper(this.locale || 'en', this.translations, this.debug)
}

}
Expand All @@ -31,8 +37,10 @@ class GettextWrapper {

private gt: GetText

constructor(locale: string, data: any) {
this.gt = new GetText()
constructor(locale: string, data: any, debug: boolean) {
this.gt = new GetText({
debug,
})

for (let key in data) {
this.gt.addTranslations(key, 'messages', data[key])
Expand Down
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
},
"dependencies": {
"core-js": "^3.6.4",
"node-gettext": "^2.0.0"
"node-gettext": "^3.0.0"
},
"devDependencies": {
"@babel/cli": "^7.8.4",
Expand Down
30 changes: 30 additions & 0 deletions test/gettext.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ import { po } from 'gettext-parser'
import { getGettextBuilder } from '../lib/gettext'

describe('gettext', () => {

beforeEach(() => {
jest.spyOn(console, 'warn')
})

afterEach(() => {
console.warn.mockRestore()
})

it('falls back to the original string', () => {
const gt = getGettextBuilder()
.setLanguage('de')
Expand All @@ -13,6 +22,27 @@ describe('gettext', () => {
expect(translation).toEqual('Settings')
})

it('does not log in production', () => {
const gt = getGettextBuilder()
.setLanguage('de')
.build()

gt.gettext('Settings')

expect(console.warn).not.toHaveBeenCalled()
})

it('has optional debug logs', () => {
const gt = getGettextBuilder()
.setLanguage('de')
.enableDebugMode()
.build()

gt.gettext('Settings')

expect(console.warn).toHaveBeenCalled()
})

it('falls back to the original singular string', () => {
const gt = getGettextBuilder()
.setLanguage('en')
Expand Down

0 comments on commit e3526a0

Please sign in to comment.