mirror of
https://github.com/digitalocean/nginxconfig.io.git
synced 2025-11-06 11:56:15 +08:00
Make language packs load async (#204)
* Webpack magic to make language packs async * Fix jest failing * Ensure the native language names are always present * Add loading state for language packs
This commit is contained in:
committed by
GitHub
parent
9b0a7dc4ac
commit
de76ad9a43
@@ -24,11 +24,9 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import * as i18nPacks from '../i18n';
|
||||
import { fromSep } from './language_pack_name';
|
||||
|
||||
const toPack = locale => locale.split('-', 2)[0].toLowerCase() + (locale.split('-', 2)[1] || '').toUpperCase();
|
||||
|
||||
export default () => {
|
||||
export default availablePacks => {
|
||||
if (typeof window === 'object' && typeof window.navigator === 'object') {
|
||||
const userLocales = new Set();
|
||||
|
||||
@@ -42,12 +40,11 @@ export default () => {
|
||||
userLocales.add(Intl.DateTimeFormat().resolvedOptions().locale);
|
||||
|
||||
// Try to find an exact region/language match
|
||||
const i18nPackLocales = Object.keys(i18nPacks);
|
||||
const exactMatch = [...userLocales.values()].find(locale => i18nPackLocales.includes(toPack(locale)));
|
||||
if (exactMatch) return toPack(exactMatch);
|
||||
const exactMatch = [...userLocales.values()].find(locale => availablePacks.includes(fromSep(locale, '-')));
|
||||
if (exactMatch) return fromSep(exactMatch, '-');
|
||||
|
||||
// Build a map of languages to pack
|
||||
const i18nPackLanguages = i18nPackLocales.reduce((map, pack) => {
|
||||
const i18nPackLanguages = availablePacks.reduce((map, pack) => {
|
||||
const lang = pack.match(/^[a-z]+/)[0];
|
||||
if (!(lang in map)) map[lang] = pack;
|
||||
return map;
|
||||
|
||||
37
src/nginxconfig/util/language_pack_context.js
Normal file
37
src/nginxconfig/util/language_pack_context.js
Normal file
@@ -0,0 +1,37 @@
|
||||
/*
|
||||
Copyright 2020 DigitalOcean
|
||||
|
||||
This code is licensed under the MIT License.
|
||||
You may obtain a copy of the License at
|
||||
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
import { fromSep } from './language_pack_name';
|
||||
|
||||
// Use webpack magic to only build chunks for lang/index.js, not subdirectories (e.g. lang/templates/index.js)
|
||||
export const languagePackContext = require.context('../i18n', true, /^\.\/[^/]+\/index\.js$/, 'lazy');
|
||||
|
||||
// Webpack magic to get all the packs that are available
|
||||
export const availablePacks = Object.freeze(languagePackContext
|
||||
.keys()
|
||||
.map(pack => pack.match(/^\.\/([^/]+)\/index\.js$/))
|
||||
.filter(pack => pack !== null)
|
||||
.map(pack => fromSep(pack[1], '-')));
|
||||
30
src/nginxconfig/util/language_pack_default.js
Normal file
30
src/nginxconfig/util/language_pack_default.js
Normal file
@@ -0,0 +1,30 @@
|
||||
/*
|
||||
Copyright 2020 DigitalOcean
|
||||
|
||||
This code is licensed under the MIT License.
|
||||
You may obtain a copy of the License at
|
||||
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
export const defaultPack = 'en';
|
||||
|
||||
export { default as defaultPackData } from '../i18n/en';
|
||||
34
src/nginxconfig/util/language_pack_name.js
Normal file
34
src/nginxconfig/util/language_pack_name.js
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
Copyright 2020 DigitalOcean
|
||||
|
||||
This code is licensed under the MIT License.
|
||||
You may obtain a copy of the License at
|
||||
https://github.com/digitalocean/nginxconfig.io/blob/master/LICENSE or https://mit-license.org/
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions :
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
|
||||
export const toSep = (pack, sep) => pack
|
||||
.match(/^([a-z]+)([A-Z]*)$/)
|
||||
.slice(1)
|
||||
.map(x => x.toLowerCase())
|
||||
.filter(x => !!x)
|
||||
.join(sep);
|
||||
|
||||
export const fromSep = (pack, sep) => pack.split(sep, 2)[0].toLowerCase() + (pack.split(sep, 2)[1] || '').toUpperCase();
|
||||
Reference in New Issue
Block a user