@zachhandley/ez-i18n 0.2.2 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,138 +0,0 @@
1
- import type { App, Plugin, ComputedRef } from 'vue';
2
- import { computed } from 'vue';
3
- import { useStore } from '@nanostores/vue';
4
- // Import from package path (not relative) to ensure shared store instance
5
- import { effectiveLocale, translations, setLocale } from '@zachhandley/ez-i18n/runtime';
6
- import type { TranslateFunction } from '../types';
7
-
8
- /**
9
- * Get nested value from object using dot notation
10
- */
11
- function getNestedValue(obj: Record<string, unknown>, path: string): unknown {
12
- const keys = path.split('.');
13
- let value: unknown = obj;
14
-
15
- for (const key of keys) {
16
- if (value == null || typeof value !== 'object') {
17
- return undefined;
18
- }
19
- value = (value as Record<string, unknown>)[key];
20
- }
21
-
22
- return value;
23
- }
24
-
25
- /**
26
- * Interpolate params into string
27
- */
28
- function interpolate(
29
- str: string,
30
- params?: Record<string, string | number>
31
- ): string {
32
- if (!params) return str;
33
- return str.replace(/\{(\w+)\}/g, (match, key) => {
34
- return key in params ? String(params[key]) : match;
35
- });
36
- }
37
-
38
- /**
39
- * Create a translation function bound to a translations object
40
- */
41
- function createTranslateFunction(
42
- translationsRef: ComputedRef<Record<string, unknown>>
43
- ): TranslateFunction {
44
- return (key: string, params?: Record<string, string | number>): string => {
45
- const trans = translationsRef.value;
46
- const value = getNestedValue(trans, key);
47
-
48
- if (typeof value !== 'string') {
49
- if (import.meta.env?.DEV) {
50
- console.warn('[ez-i18n] Missing translation:', key);
51
- }
52
- return key;
53
- }
54
-
55
- return interpolate(value, params);
56
- };
57
- }
58
-
59
- /**
60
- * Vue plugin that provides global $t(), $locale, and $setLocale
61
- *
62
- * @example
63
- * // In _vueEntrypoint.ts or main.ts
64
- * import { ezI18nVue } from '@zachhandley/ez-i18n/vue';
65
- *
66
- * export default (app) => {
67
- * app.use(ezI18nVue);
68
- * };
69
- *
70
- * @example
71
- * // In Vue components
72
- * <template>
73
- * <h1>{{ $t('welcome.title') }}</h1>
74
- * <p>{{ $t('welcome.message', { name: userName }) }}</p>
75
- * <button @click="$setLocale('es')">Español</button>
76
- * </template>
77
- */
78
- export const ezI18nVue: Plugin = {
79
- install(app: App) {
80
- // Get reactive store values
81
- const locale = useStore(effectiveLocale);
82
- const trans = useStore(translations);
83
-
84
- // Create reactive computed for translations
85
- const transComputed = computed(() => trans.value);
86
-
87
- // Create translate function
88
- const t = createTranslateFunction(transComputed);
89
-
90
- // Add global properties
91
- app.config.globalProperties.$t = t;
92
- app.config.globalProperties.$locale = locale;
93
- app.config.globalProperties.$setLocale = setLocale;
94
-
95
- // Also provide for composition API usage
96
- app.provide('ez-i18n', {
97
- t,
98
- locale,
99
- setLocale,
100
- });
101
- },
102
- };
103
-
104
- /**
105
- * Composable for using i18n in Vue components with Composition API
106
- *
107
- * @example
108
- * <script setup>
109
- * import { useI18n } from '@zachhandley/ez-i18n/vue';
110
- *
111
- * const { t, locale, setLocale } = useI18n();
112
- * const greeting = t('welcome.greeting');
113
- * </script>
114
- */
115
- export function useI18n() {
116
- const locale = useStore(effectiveLocale);
117
- const trans = useStore(translations);
118
- const transComputed = computed(() => trans.value);
119
- const t = createTranslateFunction(transComputed);
120
-
121
- return {
122
- t,
123
- locale,
124
- setLocale,
125
- };
126
- }
127
-
128
- // Type augmentation for Vue global properties
129
- declare module 'vue' {
130
- interface ComponentCustomProperties {
131
- $t: TranslateFunction;
132
- /** Current locale (reactive ref from nanostore) */
133
- $locale: Readonly<import('vue').Ref<string>>;
134
- $setLocale: typeof setLocale;
135
- }
136
- }
137
-
138
- export default ezI18nVue;