npm

svelte-django-gettext

1.0.3 • Public • Published

Use django's gettext in Svelte

Wrapper around django's JavaScript i18n features in Svelte.

Django docs: Using the JavaScript translation catalog

In your rollup configuration:

import svelte from 'rollup-plugin-svelte';
import { writeGettextExtracts, gettextStringExtractor } from 'svelte-django-gettext/build';

export default {
    input: 'app.js',
    output: {...},
    plugins: [
        svelte({
            preprocessors: {
                markup: gettextStringExtractor
            }
        }),
        writeGettextExtracts('js/_svelte_django_extracted_messages.js'),
    ]
}

Or with vite:

import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';

import { gettextStringExtractor, writeGettextExtracts } from 'svelte-django-gettext/build';

export default defineConfig({
    base: '',
    plugins: [
        svelte({
            preprocess: {
                markup: gettextStringExtractor
            },
        }),
        writeGettextExtracts('js/_svelte_django_extracted_messages.js'),
    ],
});

In your application:

<script>
import gettext from 'svelte-django-gettext';

</script>

<h1>{gettext('hello')}</h1>

Make sure the page loads the generated catalog, from a django template: <script src="{% url 'javascript-catalog' %}"></script>. If this is not the case, all functions will just return their message id.

How it works:

Django's ./manage.py makemessages cannot parse .svelte files to extract gettext() calls. A preprocessor extracts gettext() calls and outputs _svelte_django_extracted_messages.js, which then contains gettext calls extracted from your .svelte files.

This file should be added to .gitignore, but Django's makemessages management command can read translations strings from it.

Package Sidebar

Install

npm i svelte-django-gettext

Weekly Downloads

107

Version

1.0.3

License

ISC

Unpacked Size

6.31 kB

Total Files

4

Last publish

Collaborators

  • jieter