Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
offer possibility to define a language detector detect function as pr…
…omise #1879
- Loading branch information
Showing
7 changed files
with
182 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
import { createInstance } from '../src/index.js'; | ||
import { expect } from 'chai'; | ||
|
||
describe('LanguageDetector with different signatures', () => { | ||
describe('LanguageDetector with minimal sync signature', () => { | ||
let detectingLanguage = 'de-CH'; | ||
let i18n = createInstance(); | ||
i18n.use({ | ||
type: 'languageDetector', | ||
detect: () => detectingLanguage, | ||
}); | ||
|
||
describe('#init', () => { | ||
it('should work as usual', () => { | ||
i18n.init({ initImmediate: false }); | ||
expect(i18n.language).to.eql('de-CH'); | ||
}); | ||
}); | ||
|
||
describe('#changeLanguage', () => { | ||
it('should detect the language accordingly', () => { | ||
detectingLanguage = 'it'; | ||
i18n.changeLanguage(); | ||
expect(i18n.language).to.eql('it'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('LanguageDetector with full sync signature', () => { | ||
let detectingLanguage = 'de-CH'; | ||
let cachedLng; | ||
let i18n = createInstance(); | ||
i18n.use({ | ||
type: 'languageDetector', | ||
init: () => {}, | ||
detect: () => detectingLanguage, | ||
cacheUserLanguage: (l) => (cachedLng = l), | ||
}); | ||
|
||
describe('#init', () => { | ||
it('should work as usual', () => { | ||
i18n.init({ initImmediate: false }); | ||
expect(i18n.language).to.eql('de-CH'); | ||
expect(cachedLng).to.eql('de-CH'); | ||
}); | ||
}); | ||
|
||
describe('#changeLanguage', () => { | ||
it('should detect the language accordingly', () => { | ||
detectingLanguage = 'it'; | ||
i18n.changeLanguage(); | ||
expect(i18n.language).to.eql('it'); | ||
expect(cachedLng).to.eql('it'); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('LanguageDetector with async callback signature', () => { | ||
let detectingLanguage = 'de-CH'; | ||
let i18n = createInstance(); | ||
i18n.use({ | ||
type: 'languageDetector', | ||
async: true, | ||
detect: (clb) => clb(detectingLanguage), | ||
}); | ||
|
||
describe('#init', () => { | ||
it('should work as usual', (done) => { | ||
i18n.init({}, (err) => { | ||
expect(i18n.language).to.eql('de-CH'); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#changeLanguage', () => { | ||
it('should detect the language accordingly', (done) => { | ||
detectingLanguage = 'it'; | ||
i18n.changeLanguage(undefined, (err) => { | ||
expect(i18n.language).to.eql('it'); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('LanguageDetector with async promise signature', () => { | ||
let detectingLanguage = 'de-CH'; | ||
let cachedLng; | ||
let i18n = createInstance(); | ||
i18n.use({ | ||
type: 'languageDetector', | ||
async: true, | ||
detect: async () => Promise.resolve(detectingLanguage), | ||
cacheUserLanguage: async (l) => (cachedLng = l), | ||
}); | ||
|
||
describe('#init', () => { | ||
it('should work as usual', (done) => { | ||
i18n.init({}, (err) => { | ||
expect(i18n.language).to.eql('de-CH'); | ||
expect(cachedLng).to.eql('de-CH'); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('#changeLanguage', () => { | ||
it('should detect the language accordingly', (done) => { | ||
detectingLanguage = 'it'; | ||
i18n.changeLanguage(undefined, (err) => { | ||
expect(i18n.language).to.eql('it'); | ||
expect(cachedLng).to.eql('it'); | ||
done(err); | ||
}); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { createInstance, LanguageDetectorAsyncModule, LanguageDetectorModule } from 'i18next'; | ||
|
||
let i18n = createInstance(); | ||
i18n | ||
.use<LanguageDetectorModule>({ | ||
type: 'languageDetector', | ||
detect: () => 'en', | ||
}) | ||
.init(); | ||
|
||
i18n = createInstance(); | ||
i18n | ||
.use<LanguageDetectorAsyncModule>({ | ||
type: 'languageDetector', | ||
async: true, | ||
detect: (clb) => clb('en'), | ||
}) | ||
.init(); | ||
|
||
i18n = createInstance(); | ||
i18n | ||
.use<LanguageDetectorAsyncModule>({ | ||
type: 'languageDetector', | ||
async: true, | ||
detect: async () => 'en', | ||
}) | ||
.init(); | ||
|
||
i18n = createInstance(); | ||
i18n | ||
.use({ | ||
type: 'languageDetector', | ||
async: true, | ||
detect: (clb: (lng: string) => void) => clb('en'), | ||
}) | ||
.init(); |