question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Question] A way to initialize svelte-i18n for Storybook

See original GitHub issue

Hello, thank you for making this awesome library! I just have a question related to the usage of this library with Storybook.

I am using svelte with storybook, and it turns out that I need to properly initialize svelte-i18n somehow by using something like addDecorator in Storybook, but I just don’t know how. I’ve already struggled a lot with this, but I haven’t found a workaround for this.

Both Storybook for Svelte and svelte-i18n are at their early stages, so I could not really find much information on the web. So I would be really glad if anybody would knows about this issue could help.

Thank you very much in advance!

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:1
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

4reactions
jerriclynsjohncommented, Dec 19, 2020

Anyone here have a working repo for this combination.

2reactions
smoglicacommented, Jun 25, 2020

@9oelM you’re right, using addDecorator is the correct way. I suggest to use it globally in order to initialize svelte-i18n for all stories.

Creation of global decorators can be done in .storybook/preview.js. The tricky part here is that you need to create a wrapper component which will initialize i18n for your story component.

Create your wrapper component for example in .storybook/Wrapper.svelte

// Wrapper.svelte
<script>
import { addMessages, init, getLocaleFromNavigator } from 'svelte-i18n';
import { addMessages } from 'svelte-i18n';

import en from './en.json';
import enUS from './en-US.json';

addMessages('en', en);
addMessages('en-US', enUS);

init({
  fallbackLocale: 'en',
  initialLocale: getLocaleFromNavigator(),
});
</script>

<slot /> // Here where your story component's content will be projected 

Next step it’s telling to Storybook to use the global decorator.

// preview.js
import { addDecorator } from '@storybook/svelte';
import Wrapper from './Wrapper.svelte;

addDecorator(storyFn => {
  const { Component, props, on, WrapperData } = storyFn();

  return {
    Component,
    props,
    on,
    Wrapper,
    WrapperData,
  };
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

A Step-by-Step Guide to Svelte Localization | Phrase
Still quite new but powerful, the Svelte framework has found a solid niche for itself. Here's how to implement Svelte localization!
Read more >
I18n with Storybook - JS.ORG
1. Expose i18next to Storybook ... To make your translations available in your stories, you'll first need to expose your i18next instance to ......
Read more >
Svelte-i18n with Snowpack shows blank page without errors
This way you don't have to wait for the dictionaries to load in order to retrieve messages. // index.js import { register, init, ......
Read more >
Announcing SvelteKit 1.0
How do I prepare my application for deployment? An application framework is designed to answer these questions. SvelteKit does so with a design ......
Read more >
Calling i18n.js file on app's entry-point | svelte-i18n : r/sveltejs
If you're using Sapper, remember to also call init() on your server-side code (server.js)." I don't know how to do it? Can anyone...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found