@adminforth/i18n 1.0.0-next.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,76 @@
1
+ <template>
2
+ <p class="text-gray-500 dark:text-gray-400 font-sm text-left mt-3 flex items-center justify-center">
3
+ <Select
4
+ class="w-full"
5
+ v-model="selectedLanguage"
6
+ :options="options"
7
+ :placeholder="$t('Select language')"
8
+ >
9
+ <template #item="{ option }">
10
+ <span class="mr-1">
11
+ <span class="flag-icon"
12
+ :class="`flag-icon-${getCountryCodeFromLangCode(option.value)}`"
13
+ ></span>
14
+
15
+ </span>
16
+ <span>{{ option.label }}</span>
17
+ </template>
18
+
19
+ <template #selected-item="{option}">
20
+ <span class="mr-1">
21
+ <span class="flag-icon"
22
+ :class="`flag-icon-${getCountryCodeFromLangCode(option.value)}`"
23
+ ></span>
24
+ </span>
25
+ <span>{{ option.label }}</span>
26
+ </template>
27
+ </Select>
28
+ </p>
29
+ </template>
30
+
31
+ <script setup>
32
+ import Select from '@/afcl/Select.vue';
33
+ import 'flag-icon-css/css/flag-icons.min.css';
34
+ import { setLang, getCountryCodeFromLangCode, getLocalLang } from './langCommon';
35
+ import { useCoreStore } from '@/stores/core';
36
+
37
+ import { computed, ref, onMounted, watch } from 'vue';
38
+ import { useI18n } from 'vue-i18n';
39
+
40
+ const { setLocaleMessage, locale } = useI18n();
41
+
42
+
43
+ const props = defineProps(['meta', 'resource']);
44
+
45
+ const selectedLanguage = ref('');
46
+ const coreStore = useCoreStore();
47
+
48
+ watch(() => selectedLanguage.value, async (newVal) => {
49
+ await setLang({ setLocaleMessage, locale }, props.meta.pluginInstanceId, newVal);
50
+ coreStore.getPublicConfig();
51
+ });
52
+
53
+
54
+ const options = computed(() => {
55
+ return props.meta.supportedLanguages.map((lang) => {
56
+ return {
57
+ value: lang.code,
58
+ label: lang.name,
59
+ };
60
+ });
61
+ });
62
+
63
+ onMounted(() => {
64
+ console.log('LanguageUnderLogin mounted', props.meta.supportedLanguages);
65
+ selectedLanguage.value = getLocalLang(props.meta.supportedLanguages);
66
+ setLang({ setLocaleMessage, locale }, props.meta.pluginInstanceId, selectedLanguage.value);
67
+ // todo this mounted executed only on this component mount, f5 from another page apart login will not read it
68
+ });
69
+
70
+
71
+
72
+
73
+
74
+
75
+
76
+ </script>
@@ -0,0 +1,89 @@
1
+
2
+ import { callAdminForthApi } from '@/utils';
3
+
4
+
5
+ const messagesCache: Record<
6
+ string,
7
+ {
8
+ ts: number;
9
+ messages: Record<string, string>;
10
+ }
11
+ > = {};
12
+
13
+ // cleanup messages after a 2 minutes (cache for instant switching)
14
+ setInterval(() => {
15
+ const now = Date.now();
16
+ for (const lang in messagesCache) {
17
+ if (now - messagesCache[lang].ts > 10 * 60 * 1000) {
18
+ delete messagesCache[lang];
19
+ }
20
+ }
21
+ }, 60 * 1000);
22
+
23
+ // i18n is vue-i18n instance
24
+ export async function setLang({ setLocaleMessage, locale }: any, pluginInstanceId: string, langIso: string) {
25
+
26
+ if (!messagesCache[langIso]) {
27
+ const messages = await callAdminForthApi({
28
+ path: `/plugin/${pluginInstanceId}/frontend_messages?lang=${langIso}`,
29
+ method: 'GET',
30
+ });
31
+ messagesCache[langIso] = {
32
+ ts: Date.now(),
33
+ messages: messages
34
+ };
35
+ }
36
+
37
+ // set locale and locale message
38
+ setLocaleMessage(langIso, messagesCache[langIso].messages);
39
+
40
+ // set the language
41
+ locale.value = langIso;
42
+
43
+ document.querySelector('html').setAttribute('lang', langIso);
44
+ setLocalLang(langIso);
45
+ }
46
+
47
+ // only remap the country code for the languages where language code is different from the country code
48
+ // don't include es: es, fr: fr, etc, only include the ones where language code is different from the country code
49
+ const countryISO31661ByLangISO6391 = {
50
+ en: 'us', // English → United States
51
+ zh: 'cn', // Chinese → China
52
+ hi: 'in', // Hindi → India
53
+ ar: 'sa', // Arabic → Saudi Arabia
54
+ ko: 'kr', // Korean → South Korea
55
+ ja: 'jp', // Japanese → Japan
56
+ uk: 'ua', // Ukrainian → Ukraine
57
+ ur: 'pk', // Urdu → Pakistan
58
+ };
59
+
60
+ export function getCountryCodeFromLangCode(langCode) {
61
+ return countryISO31661ByLangISO6391[langCode] || langCode;
62
+ }
63
+
64
+
65
+ const LS_LANG_KEY = `afLanguage`;
66
+
67
+ export function getLocalLang(supportedLanguages: {code}[]): string {
68
+ let lsLang = localStorage.getItem(LS_LANG_KEY);
69
+ // if someone screwed up the local storage or we stopped language support, lets check if it is in supported languages
70
+ if (lsLang && !supportedLanguages.find((l) => l.code == lsLang)) {
71
+ lsLang = null;
72
+ }
73
+ if (lsLang) {
74
+ return lsLang;
75
+ }
76
+ // read lang from navigator and try find what we have in supported languages
77
+ const lang = navigator.language.split('-')[0];
78
+ const foundLang = supportedLanguages.find((l) => l.code == lang);
79
+ if (foundLang) {
80
+ return foundLang.code;
81
+ }
82
+ return supportedLanguages[0].code;
83
+ }
84
+
85
+ export function setLocalLang(lang: string) {
86
+ localStorage.setItem(LS_LANG_KEY, lang);
87
+ }
88
+
89
+
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "custom",
3
+ "version": "1.0.0",
4
+ "lockfileVersion": 3,
5
+ "requires": true,
6
+ "packages": {
7
+ "": {
8
+ "name": "custom",
9
+ "version": "1.0.0",
10
+ "license": "ISC",
11
+ "devDependencies": {
12
+ "flag-icon-css": "^4.1.7"
13
+ }
14
+ },
15
+ "node_modules/flag-icon-css": {
16
+ "version": "4.1.7",
17
+ "resolved": "https://registry.npmjs.org/flag-icon-css/-/flag-icon-css-4.1.7.tgz",
18
+ "integrity": "sha512-AFjSU+fv98XbU0vnTQ32vcLj89UEr1MhwDFcooQv14qWJCjg9fGZzfh9BVyDhAhIOZW/pGmJmq38RqpgPaeybQ==",
19
+ "deprecated": "The project has been renamed to flag-icons",
20
+ "dev": true,
21
+ "license": "MIT"
22
+ }
23
+ }
24
+ }
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "custom",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "scripts": {
6
+ "test": "echo \"Error: no test specified\" && exit 1"
7
+ },
8
+ "keywords": [],
9
+ "author": "",
10
+ "license": "ISC",
11
+ "description": "",
12
+ "devDependencies": {
13
+ "flag-icon-css": "^4.1.7"
14
+ }
15
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "compilerOptions": {
3
+ "baseUrl": ".", // This should point to your project root
4
+ "paths": {
5
+ "@/*": [
6
+ // "node_modules/adminforth/dist/spa/src/*"
7
+ "../../../spa/src/*"
8
+ ],
9
+ "*": [
10
+ // "node_modules/adminforth/dist/spa/node_modules/*"
11
+ "../../../spa/node_modules/*"
12
+ ],
13
+ "@@/*": [
14
+ // "node_modules/adminforth/dist/spa/src/*"
15
+ "."
16
+ ]
17
+ }
18
+ }
19
+ }