next-multilingual
Advanced tools
Comparing version 0.6.0 to 0.7.0
@@ -34,9 +34,7 @@ import type { Rewrite, Redirect } from 'next/dist/lib/load-custom-routes'; | ||
/** The default location of the Next.js `pages` directory. */ | ||
static defaultPagesDirectoryPath: string; | ||
static readonly defaultPagesDirectoryPath = "pages"; | ||
/** The default file extensions of Next.js' pages to include when building localized routes. */ | ||
static defaultPagesExtensions: string[]; | ||
static readonly defaultPagesExtensions: string[]; | ||
/** The default files, under the `pages` directory, to exclude when building localized routes. */ | ||
static defaultExcludedPages: string[]; | ||
/** The default unique key identifier used to create localized page slugs. */ | ||
static defaultSlugKeyId: string; | ||
static readonly defaultExcludedPages: string[]; | ||
/** The actual desired locales of the multilingual application. */ | ||
@@ -54,4 +52,2 @@ private readonly actualLocales; | ||
private readonly excludedPages; | ||
/** The unique key identifier used to create localized page slugs. */ | ||
private readonly slugKeyId; | ||
/** The Next.js application's multilingual routes. */ | ||
@@ -67,7 +63,6 @@ private routes; | ||
* @param excludedPages - Specify pages to excluded if different than the ones used by Next.js (e.g. _app.tsx). | ||
* @param slugKeyId - The unique key identifier used to create localized page slugs. | ||
* | ||
* @throws Error when one of the arguments is invalid. | ||
*/ | ||
constructor(applicationIdentifier: string, locales: string[], pagesDirectoryPath?: string, pagesExtensions?: string[], excludedPages?: string[], slugKeyId?: string); | ||
constructor(applicationIdentifier: string, locales: string[], pagesDirectoryPath?: string, pagesExtensions?: string[], excludedPages?: string[]); | ||
/** | ||
@@ -74,0 +69,0 @@ * Get the URL locale prefixes. |
@@ -77,7 +77,6 @@ "use strict"; | ||
* @param excludedPages - Specify pages to excluded if different than the ones used by Next.js (e.g. _app.tsx). | ||
* @param slugKeyId - The unique key identifier used to create localized page slugs. | ||
* | ||
* @throws Error when one of the arguments is invalid. | ||
*/ | ||
constructor(applicationIdentifier, locales, pagesDirectoryPath = MulConfig.defaultPagesDirectoryPath, pagesExtensions = MulConfig.defaultPagesExtensions, excludedPages = MulConfig.defaultExcludedPages, slugKeyId = MulConfig.defaultSlugKeyId) { | ||
constructor(applicationIdentifier, locales, pagesDirectoryPath = MulConfig.defaultPagesDirectoryPath, pagesExtensions = MulConfig.defaultPagesExtensions, excludedPages = MulConfig.defaultExcludedPages) { | ||
// Set the application identifier if valid. | ||
@@ -108,3 +107,2 @@ if (!messages_1.keySegmentRegExp.test(applicationIdentifier)) { | ||
this.excludedPages = excludedPages.map((excludedPage) => (0, path_1.resolve)(this.pagesDirectoryPath, excludedPage)); | ||
this.slugKeyId = slugKeyId; | ||
this.routes = this.getRoutes(); | ||
@@ -293,5 +291,5 @@ // During development, add an extra watcher to trigger recompile when a `.properties` file changes. | ||
const keyValueObject = (0, properties_1.parsePropertiesFile)(messagesFilePath); | ||
const slugKey = Object.keys(keyValueObject).find((key) => key.endsWith(`.${this.slugKeyId}`)); | ||
const slugKey = Object.keys(keyValueObject).find((key) => key.endsWith(`.${messages_1.SLUG_KEY_ID}`)); | ||
if (!slugKey) { | ||
__2.log.warn(`unable to create the \`${(0, __1.normalizeLocale)(locale)}\` slug for \`${sourceFilePath}\`. The message file \`${messagesFilePath}\` must include a key with the \`${this.slugKeyId}\` identifier.`); | ||
__2.log.warn(`unable to create the \`${(0, __1.normalizeLocale)(locale)}\` slug for \`${sourceFilePath}\`. The message file \`${messagesFilePath}\` must include a key with the \`${messages_1.SLUG_KEY_ID}\` identifier.`); | ||
return ''; | ||
@@ -391,4 +389,2 @@ } | ||
MulConfig.defaultExcludedPages = ['_app', '_document', '_error', '404']; | ||
/** The default unique key identifier used to create localized page slugs. */ | ||
MulConfig.defaultSlugKeyId = 'slug'; | ||
/** | ||
@@ -395,0 +391,0 @@ * Returns the Next.js multilingual config. |
@@ -8,2 +8,10 @@ import { BabelifiedMessages } from './babel-plugin'; | ||
/** | ||
* The message key identifier used for slugs. | ||
*/ | ||
export declare const SLUG_KEY_ID = "slug"; | ||
/** | ||
* The message key identifier used for titles. | ||
*/ | ||
export declare const TITLE_KEY_ID = "title"; | ||
/** | ||
* Get a page's title from the locale scope messages. | ||
@@ -10,0 +18,0 @@ * |
@@ -6,3 +6,3 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.handleMessages = exports.getMessages = exports.useMessages = exports.Messages = exports.Message = exports.getSourceFilePath = exports.getMessagesFilePath = exports.getTitle = exports.keySegmentRegExpDescription = exports.keySegmentRegExp = void 0; | ||
exports.handleMessages = exports.getMessages = exports.useMessages = exports.Messages = exports.Message = exports.getSourceFilePath = exports.getMessagesFilePath = exports.getTitle = exports.TITLE_KEY_ID = exports.SLUG_KEY_ID = exports.keySegmentRegExpDescription = exports.keySegmentRegExp = void 0; | ||
const router_1 = require("next/router"); | ||
@@ -18,2 +18,10 @@ const path_1 = require("path"); | ||
/** | ||
* The message key identifier used for slugs. | ||
*/ | ||
exports.SLUG_KEY_ID = 'slug'; | ||
/** | ||
* The message key identifier used for titles. | ||
*/ | ||
exports.TITLE_KEY_ID = 'title'; | ||
/** | ||
* Get a page's title from the locale scope messages. | ||
@@ -31,5 +39,9 @@ * | ||
function getTitle(messages, values) { | ||
const titleMessage = messages.format('title', values, true); | ||
const slugMessage = messages.format('slug', values, true); | ||
return titleMessage ? titleMessage : slugMessage; | ||
const titleMessage = messages.format(exports.TITLE_KEY_ID, values, true); | ||
const slugMessage = messages.format(exports.SLUG_KEY_ID, values, true); | ||
const applicableTitle = titleMessage !== '' ? titleMessage : slugMessage; | ||
if (applicableTitle === '') { | ||
__2.log.warn(`unable to use \`getTitle\` in \`${messages.sourceFilePath}\` because keys with identifiers \`${exports.TITLE_KEY_ID}\` and \`${exports.SLUG_KEY_ID}\` were not found in messages file \`${messages.messagesFilePath}\``); | ||
} | ||
return applicableTitle; | ||
} | ||
@@ -36,0 +48,0 @@ exports.getTitle = getTitle; |
{ | ||
"name": "next-multilingual", | ||
"description": "An opinionated end-to-end solution for Next.js applications that requires multiple languages.", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"license": "MIT", | ||
@@ -6,0 +6,0 @@ "main": "lib/index.js", |
@@ -327,3 +327,3 @@ # ![next-multilingual](./assets/next-multilingual-banner.svg) | ||
- For components, files are only required if you use the `useMessages` hook. | ||
- For messages shared across multiple components (shared messages), you need to create a "shared message component". More details on how to do this below. | ||
- For messages shared across multiple components (shared messages), you need to create a "shared message hook". More details on how to do this below. | ||
@@ -471,3 +471,3 @@ Also, make sure to check your console log for warnings about potential issues with your messages. It can be tricky to get used to how it works first, but we tried to make it easy to detect and fix problems. Note that those logs will only show in non-production environments. | ||
To illustrate this we created [one example using fruits](./example/messages/Fruits.ts). All you need to do, is create a component that calls `useMessages` like this: | ||
To illustrate this we created [one example using fruits](./example/messages/useFruits.ts). All you need to do, is create a hook that calls `useMessages` like this: | ||
@@ -493,7 +493,7 @@ ```ts | ||
And to use it, simple import this component from anywhere you might need these values: | ||
And to use it, simple import this hook from anywhere you might need these values: | ||
```tsx | ||
import type { ReactElement } from 'react'; | ||
import { useFruitsMessages } from '../messages/Fruits'; | ||
import { useFruitsMessages } from '../messages/useFruits'; | ||
@@ -625,3 +625,3 @@ export default function FruitList(): ReactElement { | ||
One of Next.js' core feature is its [builtin API support](https://nextjs.org/docs/api-routes/introduction). It's not uncommon for APIs to return content in different languages. `next-multilingual` has an equivalent API just for this use case: `getMessages`. Unlike the hook `useMessages`, `getMessages` can be used in API Routes. Here is an "Hello API" example on how to use it: | ||
One of Next.js' core feature is its [builtin API support](https://nextjs.org/docs/api-routes/introduction). It's not uncommon for APIs to return content in different languages. `next-multilingual` has an equivalent API just for this use case: `getMessages`. Unlike the `useMessages` hook, `getMessages` can be used in API Routes. Here is an "Hello API" example on how to use it: | ||
@@ -648,5 +648,5 @@ ```ts | ||
This is very similar to the API implemented in the [example application](./example/pages/api). We are using the `Accept-Language` HTTP header to tell the API in which locale we want its response to be. Unlike `useMessages` that has the context of the current locale, we need to tell `getMessages` in which locale to return messages. | ||
This is very similar to the API implemented in the [example application](./example/pages/api). We are using the `Accept-Language` HTTP header to tell the API in which locale we want its response to be. Unlike the `useMessages` hook that has the context of the current locale, we need to tell `getMessages` in which locale to return messages. | ||
Message files behave exactly the same as with `useMessages`, you simply need to create one next to the API Route's file, in our case `hello.en-US.properties`: | ||
Message files behave exactly the same as with `useMessages` You simply need to create one next to the API Route's file, in our case `hello.en-US.properties`: | ||
@@ -653,0 +653,0 @@ ```properties |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
143742
2256