Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
M
MVP
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
SO
OC
MVP
Commits
12067371
Commit
12067371
authored
7 months ago
by
Mathieu Massaviol
Browse files
Options
Downloads
Patches
Plain Diff
Refactor translateValue (by @mda)
#29
parent
12de073e
No related branches found
No related tags found
1 merge request
!75
Resolve "Permettre la recherche d'un jeu de données"
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
src/composables/useTranslateValue.ts
+36
-20
36 additions, 20 deletions
src/composables/useTranslateValue.ts
with
36 additions
and
20 deletions
src/composables/useTranslateValue.ts
+
36
−
20
View file @
12067371
import
type
{
LocalizedProperty
}
from
'
@/declarations
'
import
type
{
LocalizedProperty
}
from
'
@/declarations
'
import
{
useI18n
,
type
I18nScope
}
from
'
vue-i18n
'
import
{
useI18n
,
type
I18nScope
}
from
'
vue-i18n
'
const
CACHE_TRANSLATE_KEYVALUE
=
[]
export
function
useTranslateValue
(
useScope
:
I18nScope
=
'
global
'
)
{
export
function
useTranslateValue
(
useScope
:
I18nScope
=
'
global
'
)
{
const
{
t
,
mergeLocaleMessage
}
=
useI18n
({
const
{
t
,
mergeLocaleMessage
}
=
useI18n
({
useScope
useScope
...
@@ -13,26 +15,40 @@ export function useTranslateValue(useScope: I18nScope = 'global') {
...
@@ -13,26 +15,40 @@ export function useTranslateValue(useScope: I18nScope = 'global') {
return
localizedProperty
return
localizedProperty
}
}
// For each localized property of an external resource,
let
key
:
string
// we add each localized value as a message in i18n.
// We take the first found value as the message key,
/**
// this way, if we can't fallback to any locale, at least we
* First time we find this localizedProperty
// print a real message.
*
const
keyValue
=
Object
.
values
(
localizedProperty
)[
0
]
* We init it
*/
// when we have severeal values for one locale, we arbitrary
const
lpCached
=
CACHE_TRANSLATE_KEYVALUE
.
find
(
e
=>
e
.
lp
===
localizedProperty
)
// take the first one.
if
(
!
lpCached
)
{
const
key
:
string
=
typeof
keyValue
===
'
string
'
?
keyValue
:
keyValue
[
0
]
// For each localized property of an external resource,
// we add each localized value as a message in i18n.
Object
.
keys
(
localizedProperty
).
forEach
(
function
(
locale
)
{
// We take the first found value as the message key,
if
(
Array
.
isArray
(
localizedProperty
[
locale
]))
{
// this way, if we can't fallback to any locale, at least we
// when we have severeal values for one locale, we arbitrary
// print a real message.
// take the first one.
const
keyValue
=
Object
.
values
(
localizedProperty
)[
0
]
mergeLocaleMessage
(
locale
,
{
[
key
]:
localizedProperty
[
locale
][
0
]
})
}
else
{
// when we have severeal values for one locale, we arbitrary
mergeLocaleMessage
(
locale
,
{
[
key
]:
localizedProperty
[
locale
]
})
// take the first one.
}
key
=
typeof
keyValue
===
'
string
'
?
keyValue
:
keyValue
[
0
]
});
Object
.
keys
(
localizedProperty
).
forEach
(
function
(
locale
)
{
if
(
Array
.
isArray
(
localizedProperty
[
locale
]))
{
// when we have severeal values for one locale, we arbitrary
// take the first one.
mergeLocaleMessage
(
locale
,
{
[
key
]:
localizedProperty
[
locale
][
0
]
})
}
else
{
mergeLocaleMessage
(
locale
,
{
[
key
]:
localizedProperty
[
locale
]
})
}
});
CACHE_TRANSLATE_KEYVALUE
.
push
({
keyValue
,
lp
:
localizedProperty
})
}
else
{
const
keyValue
=
lpCached
.
keyValue
key
=
typeof
keyValue
===
'
string
'
?
keyValue
:
keyValue
[
0
]
}
return
t
(
key
)
return
t
(
key
)
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment