Skip to content

Commit

Permalink
docs: add multiple-languages example
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore committed May 25, 2021
1 parent 62004a0 commit 9d9592d
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 0 deletions.
35 changes: 35 additions & 0 deletions examples/multiple-languages/README.md
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
```
29 changes: 29 additions & 0 deletions examples/multiple-languages/index.js
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)
60 changes: 60 additions & 0 deletions examples/multiple-languages/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions examples/multiple-languages/package.json
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"
}
}

0 comments on commit 9d9592d

Please sign in to comment.