@@ -63,7 +63,7 @@ const DICTONARY: any = {}; // { translationsDir: {lang: {key: translation} } }
63
63
* @param {String } translationsDir Relative path to directory containing JSON files with translations
64
64
* @returns Promise that resolves with a list of language codes that where found after translations have been reloaded from files
65
65
*/
66
- export const reloadTranslations = async ( translationsDir : string = DEFAULT_TRANSLATIONS_DIR ) : Promise < void > => {
66
+ export const reloadTranslations = async ( translationsDir : string = DEFAULT_TRANSLATIONS_DIR ) : Promise < string [ ] > => {
67
67
if ( ! translationsDir ) translationsDir = DEFAULT_TRANSLATIONS_DIR ;
68
68
LANGUAGES [ translationsDir ] = [ ] ;
69
69
DICTONARY [ translationsDir ] = [ ] ;
@@ -135,7 +135,7 @@ const _getTranslations = (
135
135
defaultLang : string ,
136
136
translationKeys : string [ ] | false ,
137
137
translationsDir : string = DEFAULT_TRANSLATIONS_DIR ,
138
- ) => {
138
+ ) : { [ key : string ] : string } => {
139
139
translationKeys = ! translationKeys || translationKeys . length === 0 ? false : translationKeys ;
140
140
const dictornary = DICTONARY [ translationsDir ]
141
141
? DICTONARY [ translationsDir ] [ lang ] || DICTONARY [ translationsDir ] [ defaultLang ] || { }
@@ -156,9 +156,9 @@ const _getTranslations = (
156
156
export const getTranslations = async (
157
157
lang : string ,
158
158
defaultLang : string ,
159
- translationKeys = [ ] ,
159
+ translationKeys : string [ ] = [ ] ,
160
160
translationsDir : string = DEFAULT_TRANSLATIONS_DIR ,
161
- ) : Promise < { } > => {
161
+ ) : Promise < { [ key : string ] : string } > => {
162
162
if ( ! translationsDir ) translationsDir = DEFAULT_TRANSLATIONS_DIR ;
163
163
if ( ! DICTONARY [ translationsDir ] ) await reloadTranslations ( translationsDir ) ;
164
164
return _getTranslations ( lang , defaultLang , translationKeys , translationsDir ) ;
@@ -169,7 +169,7 @@ export const getTranslations = async (
169
169
* Looksup following keys in the translations 'LANGUAGE_NAME_<lang>'
170
170
* @returns {<lang>: "<native name>" }
171
171
*/
172
- export const getLanguageNames = async ( translationsDir : string = DEFAULT_TRANSLATIONS_DIR ) : Promise < { } > => {
172
+ export const getLanguageNames = async ( translationsDir : string = DEFAULT_TRANSLATIONS_DIR ) : Promise < Object > => {
173
173
if ( ! translationsDir ) translationsDir = DEFAULT_TRANSLATIONS_DIR ;
174
174
if ( ! DICTONARY [ translationsDir ] ) await reloadTranslations ( translationsDir ) ;
175
175
@@ -248,7 +248,7 @@ export const getTranslationFileContentSync = (
248
248
* (if not defined 'DEFAULT_REQUEST_PROCESSED_PATH_ATTR' will be used) <br>
249
249
* @returns function(req, res, next) that is designed for being set as middleware to pre-handle incoming requests
250
250
*/
251
- export const LanguageRouter = (
251
+ export const LanguageRouter = async (
252
252
options : any = {
253
253
default : DEFAULT_LANGUAGE ,
254
254
languages : DEFAULT_LANGUAGES ,
@@ -302,13 +302,11 @@ export const LanguageRouter = (
302
302
const languages = new Set ( languagesArr ) ;
303
303
304
304
if ( loadTranslations ) {
305
- reloadTranslations ( translationsDir ) . then ( ( ls : any ) => {
306
- if ( ! languagesFromTranslations ) return ;
307
- for ( const l of ls ) languages . add ( l ) ;
308
- } ) ;
305
+ const ls = await reloadTranslations ( translationsDir ) ;
306
+ if ( languagesFromTranslations ) for ( const l of ls ) languages . add ( l ) ;
309
307
}
310
308
311
- return ( req : any , res : any , next : any ) => {
309
+ return ( req : any , res : any , next ? : any ) => {
312
310
// Parse URI
313
311
let lang = useUri ? req . url : false ;
314
312
if ( lang ) {
@@ -389,7 +387,7 @@ export const LanguageRouter = (
389
387
390
388
if ( loadTranslations ) req [ translationsAttr ] = _getTranslations ( lang , defaultLang , [ ] , translationsDir ) ;
391
389
392
- next ( ) ;
390
+ if ( next ) next ( ) ;
393
391
} ;
394
392
} ;
395
393
0 commit comments