Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
C
canifa_note
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Vũ Hoàng Anh
canifa_note
Commits
ec206104
Commit
ec206104
authored
Mar 04, 2024
by
Steven
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: tweak i18n locale
parent
342f341b
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
21 additions
and
38 deletions
+21
-38
package.json
web/package.json
+0
-1
pnpm-lock.yaml
web/pnpm-lock.yaml
+0
-9
LocaleSelect.tsx
web/src/components/LocaleSelect.tsx
+2
-2
i18n.ts
web/src/i18n.ts
+5
-8
global.ts
web/src/store/module/global.ts
+2
-2
i18n.ts
web/src/utils/i18n.ts
+12
-16
No files found.
web/package.json
View file @
ec206104
...
@@ -20,7 +20,6 @@
...
@@ -20,7 +20,6 @@
"fuse.js"
:
"^7.0.0"
,
"fuse.js"
:
"^7.0.0"
,
"highlight.js"
:
"^11.9.0"
,
"highlight.js"
:
"^11.9.0"
,
"i18next"
:
"^21.10.0"
,
"i18next"
:
"^21.10.0"
,
"i18next-browser-languagedetector"
:
"^7.2.0"
,
"katex"
:
"^0.16.9"
,
"katex"
:
"^0.16.9"
,
"lodash-es"
:
"^4.17.21"
,
"lodash-es"
:
"^4.17.21"
,
"lucide-react"
:
"^0.309.0"
,
"lucide-react"
:
"^0.309.0"
,
...
...
web/pnpm-lock.yaml
View file @
ec206104
...
@@ -41,9 +41,6 @@ dependencies:
...
@@ -41,9 +41,6 @@ dependencies:
i18next
:
i18next
:
specifier
:
^21.10.0
specifier
:
^21.10.0
version
:
21.10.0
version
:
21.10.0
i18next-browser-languagedetector
:
specifier
:
^7.2.0
version
:
7.2.0
katex
:
katex
:
specifier
:
^0.16.9
specifier
:
^0.16.9
version
:
0.16.9
version
:
0.16.9
...
@@ -3304,12 +3301,6 @@ packages:
...
@@ -3304,12 +3301,6 @@ packages:
resolution
:
{
integrity
:
sha512-ygGZLjmXfPHj+ZWh6LwbC37l43MhfztxetbFCoYTM2VjkIUpeHgSNn7QIyVFj7YQ1Wl9Cbw5sholVJPzWvC2MQ==
}
resolution
:
{
integrity
:
sha512-ygGZLjmXfPHj+ZWh6LwbC37l43MhfztxetbFCoYTM2VjkIUpeHgSNn7QIyVFj7YQ1Wl9Cbw5sholVJPzWvC2MQ==
}
dev
:
false
dev
:
false
/i18next-browser-languagedetector@7.2.0
:
resolution
:
{
integrity
:
sha512-U00DbDtFIYD3wkWsr2aVGfXGAj2TgnELzOX9qv8bT0aJtvPV9CRO77h+vgmHFBMe7LAxdwvT/7VkCWGya6L3tA==
}
dependencies
:
'
@babel/runtime'
:
7.23.9
dev
:
false
/i18next@21.10.0
:
/i18next@21.10.0
:
resolution
:
{
integrity
:
sha512-YeuIBmFsGjUfO3qBmMOc0rQaun4mIpGKET5WDwvu8lU7gvwpcariZLNtL0Fzj+zazcHUrlXHiptcFhBMFaxzfg==
}
resolution
:
{
integrity
:
sha512-YeuIBmFsGjUfO3qBmMOc0rQaun4mIpGKET5WDwvu8lU7gvwpcariZLNtL0Fzj+zazcHUrlXHiptcFhBMFaxzfg==
}
dependencies
:
dependencies
:
...
...
web/src/components/LocaleSelect.tsx
View file @
ec206104
import
{
Option
,
Select
}
from
"@mui/joy"
;
import
{
Option
,
Select
}
from
"@mui/joy"
;
import
{
FC
}
from
"react"
;
import
{
FC
}
from
"react"
;
import
{
availableL
ocales
}
from
"@/i18n"
;
import
{
l
ocales
}
from
"@/i18n"
;
import
Icon
from
"./Icon"
;
import
Icon
from
"./Icon"
;
interface
Props
{
interface
Props
{
...
@@ -23,7 +23,7 @@ const LocaleSelect: FC<Props> = (props: Props) => {
...
@@ -23,7 +23,7 @@ const LocaleSelect: FC<Props> = (props: Props) => {
value=
{
value
}
value=
{
value
}
onChange=
{
(
_
,
value
)
=>
handleSelectChange
(
value
as
Locale
)
}
onChange=
{
(
_
,
value
)
=>
handleSelectChange
(
value
as
Locale
)
}
>
>
{
availableL
ocales
.
map
((
locale
)
=>
{
{
l
ocales
.
map
((
locale
)
=>
{
try
{
try
{
const
languageName
=
new
Intl
.
DisplayNames
([
locale
],
{
type
:
"language"
}).
of
(
locale
);
const
languageName
=
new
Intl
.
DisplayNames
([
locale
],
{
type
:
"language"
}).
of
(
locale
);
if
(
languageName
)
{
if
(
languageName
)
{
...
...
web/src/i18n.ts
View file @
ec206104
import
i18n
,
{
BackendModule
,
FallbackLng
,
FallbackLngObjList
}
from
"i18next"
;
import
i18n
,
{
BackendModule
,
FallbackLng
,
FallbackLngObjList
}
from
"i18next"
;
import
LanguageDetector
from
"i18next-browser-languagedetector"
;
import
{
initReactI18next
}
from
"react-i18next"
;
import
{
initReactI18next
}
from
"react-i18next"
;
import
{
findNearestMatchedLanguage
}
from
"./utils/i18n"
;
export
const
availableL
ocales
=
[
export
const
l
ocales
=
[
"ar"
,
"ar"
,
"de"
,
"de"
,
"en"
,
"en"
,
...
@@ -38,10 +38,8 @@ const LazyImportPlugin: BackendModule = {
...
@@ -38,10 +38,8 @@ const LazyImportPlugin: BackendModule = {
type
:
"backend"
,
type
:
"backend"
,
init
:
function
()
{},
init
:
function
()
{},
read
:
function
(
language
,
_
,
callback
)
{
read
:
function
(
language
,
_
,
callback
)
{
if
(
fallbacks
[
language
])
{
const
matchedLanguage
=
findNearestMatchedLanguage
(
language
);
language
=
fallbacks
[
language
][
0
];
import
(
`./locales/
${
matchedLanguage
}
.json`
)
}
import
(
`./locales/
${
language
}
.json`
)
.
then
((
translation
:
any
)
=>
{
.
then
((
translation
:
any
)
=>
{
callback
(
null
,
translation
);
callback
(
null
,
translation
);
})
})
...
@@ -53,7 +51,6 @@ const LazyImportPlugin: BackendModule = {
...
@@ -53,7 +51,6 @@ const LazyImportPlugin: BackendModule = {
i18n
i18n
.
use
(
LazyImportPlugin
)
.
use
(
LazyImportPlugin
)
.
use
(
LanguageDetector
)
.
use
(
initReactI18next
)
.
use
(
initReactI18next
)
.
init
({
.
init
({
detection
:
{
detection
:
{
...
@@ -66,4 +63,4 @@ i18n
...
@@ -66,4 +63,4 @@ i18n
});
});
export
default
i18n
;
export
default
i18n
;
export
type
TLocale
=
(
typeof
availableL
ocales
)[
number
];
export
type
TLocale
=
(
typeof
l
ocales
)[
number
];
web/src/store/module/global.ts
View file @
ec206104
import
*
as
api
from
"@/helpers/api"
;
import
*
as
api
from
"@/helpers/api"
;
import
storage
from
"@/helpers/storage"
;
import
storage
from
"@/helpers/storage"
;
import
i18n
from
"@/i18n"
;
import
i18n
from
"@/i18n"
;
import
{
findNearest
LanguageMatch
}
from
"@/utils/i18n"
;
import
{
findNearest
MatchedLanguage
}
from
"@/utils/i18n"
;
import
store
,
{
useAppSelector
}
from
"../"
;
import
store
,
{
useAppSelector
}
from
"../"
;
import
{
setAppearance
,
setGlobalState
,
setLocale
}
from
"../reducer/global"
;
import
{
setAppearance
,
setGlobalState
,
setLocale
}
from
"../reducer/global"
;
...
@@ -44,7 +44,7 @@ export const initialGlobalState = async () => {
...
@@ -44,7 +44,7 @@ export const initialGlobalState = async () => {
// Otherwise, use server's default locale, set to storageLocale.
// Otherwise, use server's default locale, set to storageLocale.
const
{
locale
:
storageLocale
,
appearance
:
storageAppearance
}
=
storage
.
get
([
"locale"
,
"appearance"
]);
const
{
locale
:
storageLocale
,
appearance
:
storageAppearance
}
=
storage
.
get
([
"locale"
,
"appearance"
]);
defaultGlobalState
.
locale
=
defaultGlobalState
.
locale
=
storageLocale
||
defaultGlobalState
.
systemStatus
.
customizedProfile
.
locale
||
findNearest
LanguageMatch
(
i18n
.
language
);
storageLocale
||
defaultGlobalState
.
systemStatus
.
customizedProfile
.
locale
||
findNearest
MatchedLanguage
(
i18n
.
language
);
defaultGlobalState
.
appearance
=
storageAppearance
||
defaultGlobalState
.
systemStatus
.
customizedProfile
.
appearance
;
defaultGlobalState
.
appearance
=
storageAppearance
||
defaultGlobalState
.
systemStatus
.
customizedProfile
.
appearance
;
}
}
store
.
dispatch
(
setGlobalState
(
defaultGlobalState
));
store
.
dispatch
(
setGlobalState
(
defaultGlobalState
));
...
...
web/src/utils/i18n.ts
View file @
ec206104
import
{
FallbackLngObjList
}
from
"i18next"
;
import
{
FallbackLngObjList
}
from
"i18next"
;
import
{
useTranslation
}
from
"react-i18next"
;
import
{
useTranslation
}
from
"react-i18next"
;
import
i18n
,
{
availableL
ocales
,
TLocale
}
from
"@/i18n"
;
import
i18n
,
{
l
ocales
,
TLocale
}
from
"@/i18n"
;
import
locales
from
"@/locales/en.json"
;
import
enTranslation
from
"@/locales/en.json"
;
import
type
{
NestedKeyOf
}
from
"@/types/utils/nestedKeyOf.types"
;
import
type
{
NestedKeyOf
}
from
"@/types/utils/nestedKeyOf.types"
;
export
const
findNearestLanguageMatch
=
(
codename
:
string
):
Locale
=>
{
export
const
findNearestMatchedLanguage
=
(
language
:
string
):
Locale
=>
{
// Find existing translations for full codes (e.g. "en-US", "zh-Hant")
if
(
locales
.
includes
(
language
as
TLocale
))
{
if
(
codename
.
length
>
2
&&
availableLocales
.
includes
(
codename
as
TLocale
))
{
return
language
as
Locale
;
return
codename
as
Locale
;
}
}
// Find fallback in src/i18n.ts
const
i18nFallbacks
=
Object
.
entries
(
i18n
.
store
.
options
.
fallbackLng
as
FallbackLngObjList
);
const
i18nfallbacks
=
Object
.
entries
(
i18n
.
store
.
options
.
fallbackLng
as
FallbackLngObjList
);
for
(
const
[
main
,
fallbacks
]
of
i18nFallbacks
)
{
for
(
const
[
main
,
fallbacks
]
of
i18nfallbacks
)
{
if
(
language
===
main
)
{
if
(
codename
===
main
)
{
return
fallbacks
[
0
]
as
Locale
;
return
fallbacks
[
0
]
as
Locale
;
}
}
}
}
const
shortCode
=
codename
.
substring
(
0
,
2
);
const
shortCode
=
language
.
substring
(
0
,
2
);
if
(
locales
.
includes
(
shortCode
as
TLocale
))
{
// Match existing short code translation
if
(
availableLocales
.
includes
(
shortCode
as
TLocale
))
{
return
shortCode
as
Locale
;
return
shortCode
as
Locale
;
}
}
// Try to match "xx-YY" to existing translation for "xx-ZZ" as a last resort
// Try to match "xx-YY" to existing translation for "xx-ZZ" as a last resort
// If some match is undesired, it can be overriden in src/i18n.ts `fallbacks` option
// If some match is undesired, it can be overriden in src/i18n.ts `fallbacks` option
for
(
const
existing
of
availableL
ocales
)
{
for
(
const
existing
of
l
ocales
)
{
if
(
shortCode
==
existing
.
substring
(
0
,
2
))
{
if
(
shortCode
==
existing
.
substring
(
0
,
2
))
{
return
existing
as
Locale
;
return
existing
as
Locale
;
}
}
...
@@ -38,7 +34,7 @@ export const findNearestLanguageMatch = (codename: string): Locale => {
...
@@ -38,7 +34,7 @@ export const findNearestLanguageMatch = (codename: string): Locale => {
};
};
// Represents the keys of nested translation objects.
// Represents the keys of nested translation objects.
export
type
Translations
=
NestedKeyOf
<
typeof
locales
>
;
export
type
Translations
=
NestedKeyOf
<
typeof
enTranslation
>
;
// Represents a typed translation function.
// Represents a typed translation function.
type
TypedT
=
(
key
:
Translations
,
params
?:
Record
<
string
,
any
>
)
=>
string
;
type
TypedT
=
(
key
:
Translations
,
params
?:
Record
<
string
,
any
>
)
=>
string
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment