|
131 |
132 | ##### `no_prefix` (default)
133 |
134 | |
135 |
136 | With this strategy, routes stay as they are generated by Nuxt. No locale prefix will be added. The locale can be changed without changing the URL.
137 |
138 | |
|
139 |
140 | ##### `prefix_except_default`
141 |
142 | |
143 |
144 | Using this strategy, all of your routes will have a locale prefix added except for the default language.
145 |
146 | |
|
147 |
148 | ##### `prefix`
149 |
150 | |
151 |
152 | With this strategy, all routes will have a locale prefix.
153 |
154 | |
|
155 |
156 | ##### `prefix_and_default`
157 |
158 | |
159 |
160 | This strategy combines both previous strategies behaviours, meaning that you will get URLs with prefixes for every language, but URLs for the default language will also have a non-prefixed version. This could lead to duplicated content. You will have to handle, which URL is preferred when navigating in your app.
161 |
162 | |
163 |
164 | #### Configuration
165 |
166 | A strategy may be set using the `strategy` module option. Make sure that you have a `defaultLocale` defined in any case.
167 |
168 | ```ts
169 | export default defineNuxtConfig({
170 | i18n: {
171 | locales: ['en', 'de'],
172 | defaultLocale: 'en',
173 | strategy: 'prefix_except_default',
174 | },
175 | })
176 | ```
177 |
178 | ### Custom Route Paths
179 |
180 | In some cases, you might want to translate URLs in addition to having them prefixed with the locale code. For example, you might want to have a route like `/about` in English and `/ueber-uns` in German. You can achieve this by defining a custom path for the route in the `nuxt.config.ts` file:
181 |
182 | ```ts
183 | export default defineNuxtConfig({
184 | i18n: {
185 | locales: ['en', 'de', 'fr'],
186 | defaultLocale: 'en',
187 | pages: {
188 | about: {
189 | de: '/ueber-uns',
190 | fr: '/a-propos'
191 | }
192 | }
193 | }
194 | })
195 | ```
196 |
197 | > ℹ️ Each key within the pages object should correspond to the relative file-based path (excluding the `.vue` file extension) of the route within your `pages` directory.
198 |
199 | Customized route paths must start with a `/` and not include the locale prefix.
200 |
201 | ### Auto-Importing & Lazy-Loading Translations
202 |
203 | For apps that contain a lot of translated content, it is preferable not to bundle all the messages in the main bundle, but rather lazy-load only the language that the users selected. By defining a directory where translation files are located, locale messages can be dynamically imported when the app loads or when the user switches to another language.
204 |
205 | However, you can also benefit from the advantages of auto-import without enabling dynamic imports.
206 |
207 | How to enable file-based translations with or without lazy-loading:
208 |
209 | - Set the `langImports` option to `true`.
210 | - Enable dynamic imports by setting the `lazy` option to `true`.
211 | - Optionally, configure the `langDir` option to a directory that contains your translation files. Defaults to `locales`.
212 | - Make sure the `locales` option covers possible languages.
213 |
214 | > ℹ️ Translation files must be called the same as their locale. Currently, JSON, JSON5 and YAML are supported.
215 |
216 | Example files structure:
217 |
218 | ```
219 | ├── locales/
220 | │ ├── en.json
221 | │ ├── es.json5
222 | │ ├── fr.yaml
223 | └── nuxt.config.js
224 | ```
225 |
226 | Configuration example:
227 |
228 | ```ts
229 | export default defineNuxtConfig({
230 | i18n: {
231 | locales: ['en', 'es', 'fr'],
232 | defaultLocale: 'en',
233 | langImports: true,
234 | lazy: true
235 | }
236 | })
237 | ```
238 |
239 | > ℹ️ If you prefer to import file-based translations but don't want to dynamically import them, omit the `lazy` module option, as it defaults to `false`.
240 |
241 | > ⚠️ The global route middleware to lazy-load translations when switching locales won't run when the `no_prefix` strategy is chosen. Use the `useLazyLocaleSwitch` composable for changing the language, it will load the corresponding translations beforehand.
242 |
243 | ### Manual Translations
244 |
245 | Instead of auto-importing (with or without lazy-loading), you can manually import your translations and merge them into the global locale messages object:
246 |
247 | ```ts
248 | // Import from JSON or an ES module
249 | import en from './locales/en.json'
250 | import de from './locales/de.json'
251 |
252 | export default defineNuxtConfig({
253 | i18n: {
254 | locales: ['en', 'de'],
255 | defaultLocale: 'en',
256 | messages: {
257 | en,
258 | de,
259 | },
260 | },
261 | })
262 | ```
263 |
264 | The locale messages defined above will be passed as the `messages` option when initializing `@leanera/vue-i18n` with `createI18n()`.
265 |
266 | ## API
267 |
268 | ### Module Options
269 |
270 | ```ts
271 | interface ModuleOptions {
272 | /**
273 | * List of locales supported by your app
274 | *
275 | * @remarks
276 | * Intended to be an array of string codes, e.g. `['en', 'fr']`
277 | *
278 | * @default []
279 | */
280 | locales?: string[]
281 |
282 | /**
283 | * The app's default locale
284 | *
285 | * @remarks
286 | * It's recommended to set this to some locale regardless of the chosen strategy, as it will be used as a fallback locale
287 | *
288 | * @default 'en'
289 | */
290 | defaultLocale?: string
291 |
292 | /**
293 | * Directory where your locale files are stored
294 | *
295 | * @remarks
296 | * Expected to be a relative path from the project root
297 | *
298 | * @default 'locales'
299 | */
300 | langDir?: string
301 |
302 | /**
303 | * Whether to enable locale auto-importing
304 | *
305 | * @remarks
306 | * When enabled, the module will automatically import all locale files from the `langDir` directory
307 | *
308 | * @default false
309 | */
310 | langImports?: boolean
311 |
312 | /**
313 | * Whether to lazy-load locale messages in the client
314 | *
315 | * @remarks
316 | * If enabled, locale messages will be loaded on demand when the user navigates to a route with a different locale
317 | *
318 | * This has no effect if the `langImports` option is disabled
319 | *
320 | * Note: When `strategy` is set to `no_prefix`, use the `useLazyLocaleSwitch` composable to ensure the translation messages are loaded before switching locales
321 | *
322 | * @default false
323 | */
324 | lazy?: boolean
325 |
326 | /**
327 | * The app's default messages
328 | *
329 | * @remarks
330 | * Can be omitted if auto-importing of locales is enabled
331 | *
332 | * @default {}
333 | */
334 | messages?: LocaleMessages
335 |
336 | /**
337 | * Routes strategy
338 | *
339 | * @remarks
340 | * Can be set to one of the following:
341 | *
342 | * - `no_prefix`: routes won't have a locale prefix
343 | * - `prefix_except_default`: locale prefix added for every locale except default
344 | * - `prefix`: locale prefix added for every locale
345 | * - `prefix_and_default`: locale prefix added for every locale and default
346 | *
347 | * @default 'no_prefix'
348 | */
349 | strategy?: Strategies
350 |
351 | /**
352 | * Customize the names of the paths for a specific locale
353 | *
354 | * @remarks
355 | * In some cases, you might want to translate URLs in addition to having them prefixed with the locale code
356 | *
357 | * @example
358 | * pages: {
359 | * about: {
360 | * en: '/about-us', // Accessible at `/en/about-us`
361 | * fr: '/a-propos', // Accessible at `/fr/a-propos`
362 | * es: '/sobre' // Accessible at `/es/sobre`
363 | * }
364 | * }
365 | * @default {}
366 | */
367 | pages?: CustomRoutePages
368 |
369 | /**
370 | * Custom route overrides for the generated routes
371 | *
372 | * @example
373 | * routeOverrides: {
374 | * // Use `en` catch-all page as fallback for non-existing pages
375 | * '/en/:id(.*)*': '/:id(.*)*'
376 | * }
377 | *
378 | * @default {}
379 | */
380 | routeOverrides?: Record