-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add multiple-languages example
- Loading branch information
1 parent
62004a0
commit 9d9592d
Showing
4 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Multiple Languages | ||
|
||
This example shows how to query for documents of different languages. By | ||
default, the client will return documents from your repository's default | ||
language. | ||
|
||
The example demonstrates how the `lang` parameter can be used to override the | ||
default behavior. If you want to query for a document with a language, pass the | ||
language identifier in any client `get` method. | ||
|
||
The example also shows how to set default parameters on the client. In this | ||
example, we set `en-us` as the default language to query. | ||
|
||
**Note**: This example is written for Node.js. If you plan to use this code for | ||
the browser, you can remove `node-fetch` from the example. | ||
|
||
```diff | ||
import * as prismic from '@prismicio/client' | ||
- import fetch from 'node-fetch' | ||
import QuickLRU from 'quick-lru' | ||
``` | ||
|
||
## How to run the example | ||
|
||
```sh | ||
# Clone the repository to your computer | ||
git clone https://github.com/prismicio/prismic-javascript.git | ||
cd prismic-javascript/examples/multiple-languages | ||
|
||
# Install the dependencies | ||
npm install | ||
|
||
# Run the example | ||
node index.js | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import * as prismic from '@prismicio/client' | ||
import fetch from 'node-fetch' | ||
|
||
const endpoint = prismic.getEndpoint('qwerty') | ||
|
||
const client = prismic.createClient(endpoint, { | ||
fetch, | ||
// A default language can be set here. If a `lang` option is not set, your | ||
// repository's default language will be used by default. | ||
defaultParams: { | ||
lang: 'en-us', | ||
}, | ||
}) | ||
|
||
// This will fetch Article documents with the `en-us` lang. | ||
const articlesDefaultLanguage = await client.getAllByType('article') | ||
console.log('articlesDefaultLanguage: ', articlesDefaultLanguage) | ||
|
||
// This will also fetch Article documents with the `en-us` lang. | ||
const articlesEnglish = await client.getAllByType('article', { lang: 'en-us' }) | ||
console.log('articlesEnglish: ', articlesEnglish) | ||
|
||
// This will fetch Article documents with the `fr-fr` lang. | ||
const articlesFrench = await client.getAllByType('article', { lang: 'fr-fr' }) | ||
console.log('articlesFrench: ', articlesFrench) | ||
|
||
// This will fetch Article documents with any language. | ||
const articlesAllLanguages = await client.getAllByType('article', { lang: '*' }) | ||
console.log('articlesAllLanguages: ', articlesAllLanguages) |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"type": "module", | ||
"dependencies": { | ||
"@prismicio/client": "^5.0.0", | ||
"node-fetch": "^2.6.1" | ||
} | ||
} |