-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1310 from geichelberger/i18n-lazy-loading
Add lazy loading of lang files
- Loading branch information
Showing
9 changed files
with
199 additions
and
36 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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
#!/bin/bash | ||
|
||
declare -A country_language_map | ||
declare -a order | ||
|
||
# Loop through each file in the directory | ||
for file in ??-??.json; do | ||
if [ -f "$file" ]; then | ||
# Extract country name and language code from the filename | ||
country=$(basename "$file" .json | cut -d '-' -f 1) | ||
language_code=$(basename "$file" .json) | ||
|
||
if [ ! "${country_language_map[$country]}" ]; then | ||
order+=("$country") | ||
fi | ||
|
||
# Check if the country already exists in the map | ||
if [ -n "${country_language_map[$country]}" ]; then | ||
country_language_map["$country"]+=" $language_code" | ||
else | ||
country_language_map["$country"]="$language_code" | ||
fi | ||
fi | ||
done | ||
|
||
echo "export const languages = new Map<string, string>([" | ||
# Print the country-language mappings | ||
for i in "${!order[@]}"; do | ||
country=${order[$i]} | ||
languages=() | ||
for language in ${country_language_map[$country]}; do | ||
languages+=("$language") | ||
done | ||
if [ ${#languages[@]} -eq 1 ]; then | ||
echo " [\"$country\", \"${languages[0]}\"]," | ||
else | ||
for language in "${languages[@]}"; do | ||
echo " [\"$language\", \"$language\"]," | ||
done | ||
fi | ||
done | ||
echo "]);" |
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { BackendModule, InitOptions, MultiReadCallback, ReadCallback, ResourceLanguage, Services } from "i18next"; | ||
import { languages } from "./lngs-generated"; | ||
|
||
export default class LazyLoadingPlugin implements BackendModule { | ||
|
||
type: "backend"; | ||
|
||
constructor(_services: Services, _backendOptions: object, _i18nextOptions: InitOptions<object>) { | ||
this.type = "backend"; | ||
} | ||
|
||
init(_services: Services, _backendOptions: object, _i18nextOptions: InitOptions<object>): void { | ||
// no init needed | ||
} | ||
|
||
read(language: string, _namespace: string, callback: ReadCallback): void { | ||
const lng = languages.get(language); | ||
import(`./locales/${lng}.json`).then( | ||
obj => { | ||
callback(null, obj); | ||
} | ||
); | ||
} | ||
|
||
create?(_languages: readonly string[], _namespace: string, _key: string, _fallbackValue: string): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
readMulti?(_languages: readonly string[], _namespaces: readonly string[], _callback: MultiReadCallback): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
save?(_language: string, _namespace: string, _data: ResourceLanguage): void { | ||
throw new Error("Method not implemented."); | ||
} | ||
|
||
} |
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,11 @@ | ||
export const languages = new Map<string, string>([ | ||
["cs", "cs-CZ"], | ||
["de", "de-DE"], | ||
["el", "el-GR"], | ||
["en", "en-US"], | ||
["es", "es-ES"], | ||
["fr", "fr-FR"], | ||
["nl", "nl-NL"], | ||
["zh-CN", "zh-CN"], | ||
["zh-TW", "zh-TW"], | ||
]); |
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