Comments (9)
Anyone here have a working repo for this combination.
from svelte-i18n.
@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,
};
});
from svelte-i18n.
Hey @9oelM, would you be able to provide a simple repro with a minimal storybook setup? It'd help a lot.
from svelte-i18n.
@kaisermann thank you for following up. I will give you an example soon.
from svelte-i18n.
Hey guys,
Any update on it ?
I am facing the same problematic.
Thanks in advance 🙌
from svelte-i18n.
Thanks @smoglica , this works for me, but I still have a problem if I reload the page while checking a component that uses translations, it shows the error [svelte-i18n] Cannot format a message without first setting the initial locale.
but it works fine if I navigate to another component and then back, can you reproduce such issue also?
from svelte-i18n.
@igarcez, No I don't. I have the problem that you mentioned only when I include translations asynchronously, in that case you should probably wrap <slot />
in a if condition block and waiting for the translation, so:
// Wrapper.svelte
<script>
import { isLoading } from 'svelte-i18n'
...
</script>
{#if $isLoading}
Please wait...
{:else}
<slot />
{/if}
from svelte-i18n.
The approach with the global wrapper didn't work for me either. Did the syntax for addDecorator
change in any way?
Works fine when using a decorator in the *.stories.svelte
file, like described here: https://storybook.js.org/docs/svelte/writing-stories/decorators
from svelte-i18n.
@bummzack I think it changed yeah. The following works for me:
Change this:
// 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,
};
});
To this:
// preview.js
import Wrapper from './Wrapper.svelte;
export const decorators = [
() => Wrapper,
// optionally_add_your_other_decorators
];
from svelte-i18n.
Related Issues (20)
- Support Svelte 4 HOT 3
- Allow unit tests with ts-node esm HOT 1
- Sveltekit: Update translation outside of component
- Variable inside html isn't replaced HOT 1
- Inform about unused language keys HOT 1
- When I hover over a <a> the site language changes to the default language. HOT 1
- `intl-getcanocicallocales` is probably not a good idea HOT 2
- CJS / ESM conflict HOT 22
- Change log version comparison links are off
- Production build using svelte-i18n ^3.7.4 throws "Named export 'IntlMessageFormat' not found." HOT 6
- Problems with LIB check in cli extractor HOT 1
- Sapper reference in Getting Started guide
- Allow to restrict some parts of the i18n file to unauthorized users in SvelteKit. HOT 3
- Is it possible when registering several json files to overwrite the previous key contents with latest file?
- Docs on how to use variables in translation strings HOT 1
- How to disable AM/PM in $time formatter
- Leading zeros for day/month when formatting date values
- Add a register store?
- Build error when deploying in vercel
- Provide a Readable<string> instead of pure string
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from svelte-i18n.