forked from
npmx.dev/npmx.dev
[READ-ONLY]
a fast, modern browser for the npm registry
1import type { I18nStatus, I18nLocaleStatus } from '#shared/types'
2
3/**
4 * Composable for accessing translation status data from Lunaria.
5 * Provides information about translation progress for each locale.
6 */
7export function useI18nStatus() {
8 const { locale } = useI18n()
9
10 const {
11 data: status,
12 status: fetchStatus,
13 error,
14 } = useFetch<I18nStatus>('/lunaria/status.json', {
15 responseType: 'json',
16 server: false,
17 // Cache the result to avoid refetching on navigation
18 getCachedData: (key, nuxtApp) => nuxtApp.payload.data[key] ?? nuxtApp.static.data[key],
19 })
20
21 /**
22 * Get the translation status for a specific locale
23 */
24 function getLocaleStatus(langCode: string): I18nLocaleStatus | null {
25 if (!status.value) return null
26 return status.value.locales.find(l => l.lang === langCode) ?? null
27 }
28
29 /**
30 * Translation status for the current locale
31 */
32 const currentLocaleStatus = computed<I18nLocaleStatus | null>(() => {
33 return getLocaleStatus(locale.value)
34 })
35
36 /**
37 * Whether the current locale's translation is 100% complete
38 */
39 const isComplete = computed(() => {
40 const localeStatus = currentLocaleStatus.value
41 if (!localeStatus) return true // Assume complete if no data
42 return localeStatus.percentComplete === 100
43 })
44
45 /**
46 * Whether the current locale is the source locale (English)
47 */
48 const isSourceLocale = computed(() => {
49 return locale.value === (status.value?.sourceLocale.lang ?? 'en-US')
50 })
51
52 /**
53 * GitHub URL to edit the current locale's translation file
54 */
55 const githubEditUrl = computed(() => {
56 return currentLocaleStatus.value?.githubEditUrl ?? null
57 })
58
59 return {
60 /** Full translation status data */
61 status,
62 /** Fetch status ('idle' | 'pending' | 'success' | 'error') */
63 fetchStatus,
64 /** Fetch error if any */
65 error,
66 /** Get status for a specific locale */
67 getLocaleStatus,
68 /** Status for the current locale */
69 currentLocaleStatus,
70 /** Whether current locale is 100% complete */
71 isComplete,
72 /** Whether current locale is the source (English) */
73 isSourceLocale,
74 /** GitHub edit URL for current locale */
75 githubEditUrl,
76 }
77}