Skip to content

Commit bde3d79

Browse files
committed
feat: Adds a defaultResolver.
1 parent 1ac7b2e commit bde3d79

File tree

3 files changed

+22
-11
lines changed

3 files changed

+22
-11
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"name": "Francisco Madeira",
66
"email": "[email protected]"
77
},
8-
"keywords": "laravel, vue, vue3, inertiajs",
8+
"keywords": ["laravel", "vue", "vue3", "inertiajs"],
99
"repository": "https://github.com/xico2k/laravel-vue-i18n",
1010
"license": "MIT",
1111
"description": "allows to connect your `Laravel` Framework localization files with `Vue`.",

src/index.ts

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1+
import path from 'path'
12
import { reactive, Plugin } from 'vue'
23
import { OptionsInterface } from './interfaces/options'
34
import { LanguageInterface } from './interfaces/language'
5+
import { LanguageJsonFileInterface } from './interfaces/language-json-file'
46
import { ReplacementsInterface } from './interfaces/replacements'
57
import { choose } from './pluralization'
68

9+
/**
10+
* Resolves the lang location, on a Laravel App.
11+
*/
12+
const defaultResolve = (lang: string): Promise<LanguageJsonFileInterface> => {
13+
return import(`..${path.sep}..${path.sep}..${path.sep}..${path.sep}resources${path.sep}lang/${lang}.json`)
14+
}
15+
716
/**
817
* The default options, for the plugin.
918
*/
1019
const DEFAULT_OPTIONS: OptionsInterface = {
1120
lang: document.documentElement.lang || 'en',
12-
resolve: (lang: string) => new Promise((resolve) => resolve({ default: {} }))
21+
resolve: defaultResolve
1322
}
1423

1524
/**
1625
* Stores the current options.
1726
*/
18-
let options: OptionsInterface = DEFAULT_OPTIONS;
27+
let options: OptionsInterface = DEFAULT_OPTIONS
1928

2029
/**
2130
* Stores the loaded languages.
@@ -97,7 +106,7 @@ function makeReplacements(message: string, replacements?: ReplacementsInterface)
97106
const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1)
98107

99108
Object.entries(replacements || []).forEach(([key, value]) => {
100-
value = value.toString();
109+
value = value.toString()
101110

102111
message = message
103112
.replace(`:${key}`, value)
@@ -112,19 +121,18 @@ function makeReplacements(message: string, replacements?: ReplacementsInterface)
112121
* Resets all the data stored in memory.
113122
*/
114123
export const reset = (): void => {
115-
loaded = [];
116-
options = DEFAULT_OPTIONS;
124+
loaded = []
125+
options = DEFAULT_OPTIONS
117126

118127
for (const [key] of Object.entries(activeMessages)) {
119-
activeMessages[key] = null;
128+
activeMessages[key] = null
120129
}
121130
}
122131

123-
124132
/**
125133
* Alias to `transChoice` to mimic the same function name from Laravel Framework.
126134
*/
127-
export const trans_choice = transChoice;
135+
export const trans_choice = transChoice
128136

129137
/**
130138
* The Vue Plugin. to be used on your Vue app like this: `app.use(i18nVue)`
@@ -133,7 +141,8 @@ export const i18nVue: Plugin = {
133141
install: (app, currentOptions: OptionsInterface = {}) => {
134142
options = { ...options, ...currentOptions }
135143
app.config.globalProperties.$t = (key: string, replacements: ReplacementsInterface) => trans(key, replacements)
136-
app.config.globalProperties.$tChoice = (key: string, number: number, replacements: ReplacementsInterface) => transChoice(key, number, replacements)
144+
app.config.globalProperties.$tChoice = (key: string, number: number, replacements: ReplacementsInterface) =>
145+
transChoice(key, number, replacements)
137146
loadLanguageAsync(options.lang)
138147
}
139148
}

test/translate.test.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ it('translates with "trans" helper', async () => {
1818
it('returns the same message if there is no resolve method provided', async () => {
1919
const wrapper = mount({ template: `<h1>{{ $t('Welcome!') }}</h1>` }, {
2020
global: {
21-
plugins: [i18nVue]
21+
plugins: [[i18nVue, {
22+
resolve: () => new Promise((resolve) => resolve({ default: {} }))
23+
}]]
2224
}
2325
});
2426

0 commit comments

Comments
 (0)