-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add the ability to pass a getter for i18n instance
- Loading branch information
Showing
5 changed files
with
97 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
import i18next from 'i18next'; | ||
|
||
declare const client: { | ||
applyClientHMR(i18n: i18next.i18n): void; | ||
applyClientHMR(i18nOrGetter: i18next.i18n | (() => i18next.i18n)): void; | ||
}; | ||
|
||
export = client; |
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,29 +1,61 @@ | ||
module.exports = { | ||
extractLangAndNS: function (changedFile, currentNSList) { | ||
const changedFileParts = changedFile.replace(/\\/g, '/').split('/'); | ||
|
||
const firstLongestNSMatchParts = [] | ||
.concat(currentNSList) | ||
.map((ns) => ns.split('/')) | ||
.sort((a, b) => b.length - a.length) | ||
.find((optionalNS) => | ||
optionalNS.every((optionalNSPart) => changedFileParts.includes(optionalNSPart)) | ||
); | ||
|
||
if (!firstLongestNSMatchParts) { | ||
return { lang: null, ns: null }; | ||
} | ||
function extractLangAndNS(changedFile, currentNSList) { | ||
const changedFileParts = changedFile.replace(/\\/g, '/').split('/'); | ||
|
||
const lang = changedFileParts | ||
.filter((part) => !firstLongestNSMatchParts.includes(part)) | ||
.join('/'); | ||
|
||
return { | ||
lang, | ||
ns: firstLongestNSMatchParts.join('/'), | ||
}; | ||
}, | ||
printList: function (list) { | ||
return list.map(item => `${item.lang}/${item.ns}`).join(', ') | ||
const firstLongestNSMatchParts = [] | ||
.concat(currentNSList) | ||
.map((ns) => ns.split('/')) | ||
.sort((a, b) => b.length - a.length) | ||
.find((optionalNS) => | ||
optionalNS.every((optionalNSPart) => changedFileParts.includes(optionalNSPart)) | ||
); | ||
|
||
if (!firstLongestNSMatchParts) { | ||
return { lang: null, ns: null }; | ||
} | ||
|
||
const lang = changedFileParts | ||
.filter((part) => !firstLongestNSMatchParts.includes(part)) | ||
.join('/'); | ||
|
||
return { | ||
lang, | ||
ns: firstLongestNSMatchParts.join('/'), | ||
}; | ||
} | ||
|
||
function printList(list) { | ||
return list.map((item) => `${item.lang}/${item.ns}`).join(', '); | ||
} | ||
|
||
function extractList(changedFiles, currentNSList) { | ||
return changedFiles | ||
.map((changedFile) => extractLangAndNS(changedFile, currentNSList)) | ||
.filter(({ lang, ns }) => Boolean(lang) && Boolean(ns)); | ||
} | ||
|
||
function makeUniqueList(list) { | ||
return [...new Set(list)]; | ||
} | ||
|
||
function createLoggerOnce(log) { | ||
const msgCount = new Map(); | ||
return (msg, type = 'log') => { | ||
if (!msgCount.has(msg)) { | ||
msgCount.set(msg, 0); | ||
} | ||
|
||
if (msgCount.get(msg) >= 1) { | ||
return; | ||
} | ||
|
||
log(msg, type); | ||
msgCount.set(msg, 1); | ||
}; | ||
} | ||
|
||
module.exports = { | ||
printList: printList, | ||
extractList: extractList, | ||
makeUniqueList: makeUniqueList, | ||
createLoggerOnce: createLoggerOnce, | ||
}; |
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,7 +1,7 @@ | ||
import i18next from 'i18next'; | ||
|
||
declare const server: { | ||
applyServerHMR(i18n: i18next.i18n): void; | ||
applyServerHMR(i18nOrGetter: i18next.i18n | (() => i18next.i18n)): void; | ||
}; | ||
|
||
export = server; |