Fetch

Monster.I18n.Providers. Fetch

The fetch provider retrieves a JSON file from the given URL and returns a translation object.

You can create the object via the monster namespace new Monster.I18n.Provider.Fetch().

<script type="module">
import {Monster} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/monster.js';
new Monster.I18n.Providers.Fetch()
</script>

Alternatively, you can also integrate this function individually.

<script type="module">
import {Fetch} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/i18n/providers/fetch.js';
new Fetch()
</script>

Constructor

# new Fetch(url, options)

As options the key fetch can be passed. This config object is passed to the fetch method as init.

The url may contain placeholders (language, script, region, variants, extlang, privateUse), so you can specify one url for all translations.

new Fetch('https://www.example.com/assets/${language}.json')
Parameters:
Name Type Description
url string | URL
options Object

see Monster.I18n.Providers.Fetch#defaults

Since:
  • 1.13.0
Tutorials:
Example

das ist ein test

import {Fetch} from 'https://cdn.jsdelivr.net/npm/@schukai/monster@1.30.0/dist/modules/i18n/providers/fetch.js';

// fetch from API
const translation = new Fetch('https://example.com/${language}.json').getTranslation('en-GB');
// ↦ https://example.com/en.json

Members

# defaults

Defaults

Properties
Name Type Description
fetch Object
Properties
Name Type Default Description
method String GET
mode String cors
cache String no-cache
credentials String omit
redirect String follow
referrerPolicy String no-referrer

# url

Properties
Type Description
string

Methods

# getTranslations(locale) → {Promise}

Parameters:
Name Type Description
locale Locale | string
Returns:
Type
Promise