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
80 lines (72 loc) · 2.44 KB
/
useI18nStatus.ts
File metadata and controls
80 lines (72 loc) · 2.44 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
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],
})
/**
* Get the translation status for a specific locale
*/
function getLocaleStatus(langCode: string): I18nLocaleStatus | null {
if (!status.value) return null
return status.value.locales.find(l => l.lang === 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) or a variant of it.
* The source locale is 'en' (base), but app-facing locale codes are 'en-US', 'en-GB', etc.
* We check if the current locale starts with the source locale code to handle variants.
*/
const isSourceLocale = computed(() => {
const sourceLang = status.value?.sourceLocale.lang ?? 'en'
return locale.value === sourceLang || locale.value.startsWith(`${sourceLang}-`)
})
/**
* 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,
}
}