forked from npmx-dev/npmx.dev
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseI18nStatus.ts
More file actions
85 lines (76 loc) · 2.37 KB
/
useI18nStatus.ts
File metadata and controls
85 lines (76 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import type { I18nStatus, I18nLocaleStatus } from '#shared/types'
/**
* Composable for accessing translation status data from Lunaria.
* Provides information about translation progress for each locale.
*/
export function useI18nStatus() {
const { locale } = useI18n()
const {
data: status,
status: fetchStatus,
error,
} = useFetch<I18nStatus>('/lunaria/status.json', {
responseType: 'json',
server: false,
// Cache the result to avoid refetching on navigation
getCachedData: (key, nuxtApp) => nuxtApp.payload.data[key] ?? nuxtApp.static.data[key],
})
const localesMap = computed<Map<string, I18nLocaleStatus> | undefined>(() => {
return status.value?.locales.reduce((acc, locale) => {
acc.set(locale.lang, locale)
return acc
}, new Map())
})
/**
* Get the translation status for a specific locale
*/
function getLocaleStatus(langCode: string): I18nLocaleStatus | null {
return localesMap.value?.get(langCode) ?? null
}
/**
* Translation status for the current locale
*/
const currentLocaleStatus = computed<I18nLocaleStatus | null>(() => {
return getLocaleStatus(locale.value)
})
/**
* Whether the current locale's translation is 100% complete
*/
const isComplete = computed(() => {
const localeStatus = currentLocaleStatus.value
if (!localeStatus) return true // Assume complete if no data
return localeStatus.percentComplete === 100
})
/**
* Whether the current locale is the source locale (English)
*/
const isSourceLocale = computed(() => {
return locale.value === (status.value?.sourceLocale.lang ?? 'en-US')
})
/**
* GitHub URL to edit the current locale's translation file
*/
const githubEditUrl = computed(() => {
return currentLocaleStatus.value?.githubEditUrl ?? null
})
return {
/** Full translation status data */
status,
/** Fetch status ('idle' | 'pending' | 'success' | 'error') */
fetchStatus,
/** Fetch error if any */
error,
/** Get status for a specific locale */
getLocaleStatus,
/** Status for the current locale */
currentLocaleStatus,
/** Whether current locale is 100% complete */
isComplete,
/** Whether current locale is the source (English) */
isSourceLocale,
/** GitHub edit URL for current locale */
githubEditUrl,
/** locale info map by lang */
localesMap,
}
}