-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce createLanguageDetector (#29)
Introduce createLanguageDetector to provide greater flexibility with language detection. This change means the current detector (ReactNativeLanguageDetector) will be deprecated in favor of the more adaptable option.
- Loading branch information
Showing
3 changed files
with
89 additions
and
20 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
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 |
---|---|---|
@@ -1,25 +1,79 @@ | ||
import { getLanguage, setLanguage } from './api'; | ||
import { getLanguage, getLanguageAsync, setLanguage } from './api'; | ||
|
||
interface I18nLanguageDetectorModule { | ||
type: 'languageDetector'; | ||
init?(): void; | ||
detect(): string | readonly string[] | undefined; | ||
cacheUserLanguage?(lang: string): void; | ||
} | ||
interface I18nLanguageDetectorAsyncModule { | ||
type: 'languageDetector'; | ||
async: true; | ||
init?(): void; | ||
detect( | ||
callback: (lng: string | readonly string[] | undefined) => void | undefined | ||
): void | Promise<string | readonly string[] | undefined>; | ||
cacheUserLanguage?(lng: string): void | Promise<void>; | ||
} | ||
|
||
type LanguageDetectorOptions = { | ||
cacheCurrentLanguage?: boolean; | ||
async?: boolean; | ||
}; | ||
|
||
/** | ||
* @deprecated Use createLanguageDetector instead | ||
*/ | ||
export const ReactNativeLanguageDetector: I18nLanguageDetectorModule = { | ||
type: 'languageDetector', | ||
init: () => {}, | ||
detect: () => getLanguage(), | ||
cacheUserLanguage: (lang: string) => setLanguage(lang), | ||
}; | ||
|
||
/** | ||
* i18next language detector | ||
* I18next language detector generator | ||
* @param options - detector options | ||
* @returns I18nLanguageDetectorModule | I18nLanguageDetectorAsyncModule | ||
* @example | ||
* Usage: | ||
* const languageDetector = createLanguageDetector(options); | ||
* i18next | ||
* .use(ReactNativeLanguageDetector) | ||
* .use(languageDetector) | ||
* .init({ | ||
* ... | ||
* }); | ||
*/ | ||
export const ReactNativeLanguageDetector: I18nLanguageDetectorModule = { | ||
type: 'languageDetector', | ||
init: () => {}, | ||
detect: () => getLanguage(), | ||
cacheUserLanguage: (lng: string) => setLanguage(lng), | ||
}; | ||
export const createLanguageDetector = ( | ||
options?: LanguageDetectorOptions | ||
): I18nLanguageDetectorModule | I18nLanguageDetectorAsyncModule => { | ||
const { cacheCurrentLanguage = false } = options || {}; | ||
let skipNextCache = false; | ||
|
||
interface I18nLanguageDetectorModule { | ||
type: 'languageDetector'; | ||
init?(): void; | ||
detect(): string | readonly string[] | undefined; | ||
cacheUserLanguage?(lng: string): void; | ||
} | ||
let languageDetector: | ||
| I18nLanguageDetectorModule | ||
| I18nLanguageDetectorAsyncModule = { | ||
type: 'languageDetector', | ||
init: () => { | ||
skipNextCache = true; | ||
}, | ||
detect: () => getLanguage(), | ||
cacheUserLanguage: (lang: string) => { | ||
if (cacheCurrentLanguage === false && skipNextCache) { | ||
skipNextCache = false; | ||
return; | ||
} | ||
setLanguage(lang); | ||
}, | ||
}; | ||
if (options?.async) { | ||
languageDetector = { | ||
...languageDetector, | ||
async: true, | ||
detect: (callback) => { | ||
getLanguageAsync().then(callback); | ||
}, | ||
}; | ||
} | ||
return languageDetector; | ||
}; |