Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for left/right strings #6

Merged
merged 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .storybook/preview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export const parameters = {
locale: 'en',
locales: {
en: {title: 'English', right: '🇺🇸'},
gb: {title: 'English', right: 'GB'},
fr: {title: 'Français', left: '🇫🇷'},
ja: {left: '🇯🇵'},
de: {right: '🇩🇪'},
es: 'Spain',
it: '🇮🇹 Italiano',
},
}
31 changes: 30 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const myDecorator = (story, context) => {

End users configure the `locales` and `locale` parameters in `.storybook/preview.js`.

`Locales` is an object where the keys are the "ids" of the locale/language and the values are the plain text name of that locale you want to use. This is what will appear in the dropdown in the toolbar.
`locales` is an object where the keys are the "ids" of the locale/language and the values are the plain text name of that locale you want to use. This is what will appear in the dropdown in the toolbar.

```javascript
export const parameters = {
Expand All @@ -74,4 +74,33 @@ export const parameters = {
};
```

The `locales` object can also have values as an object with keys of `title`, `left`, or `right`.

This is useful if you want to include an emoji flag or some other string to the left or right side.

For example:
```javascript
export const parameters = {
locale: "en",
locales: {
en: {title: "English", left: '🇺🇸'},
fr: {title: "Français", left: '🇫🇷'},
ja: {title: "日本語", left: '🇯🇵'},
},
};
```

Or something like this:
```javascript
export const parameters = {
locale: "en_US",
locales: {
en_US: {title: "English", right: 'US'},
en_GB: {title: "English", right: 'GB'},
fr_FR: {title: "Français", right: 'FR'},
ja_JP: {title: "日本語", right: 'JP'},
},
};
```

Addons should instruct them to use whichever format your i18n implementation expects.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@
"dependencies": {},
"devDependencies": {
"@babel/cli": "^7.12.1",
"@babel/core": "^7.12.3",
"@babel/preset-env": "^7.12.1",
"@babel/core": "^7.15.0",
"@babel/preset-env": "^7.15.0",
"@babel/preset-react": "^7.12.5",
"@babel/preset-typescript": "^7.13.0",
"@babel/preset-typescript": "^7.15.0",
"@storybook/addon-essentials": "^6.2.9",
"@storybook/react": "^6.2.9",
"auto": "^10.3.0",
Expand Down
27 changes: 21 additions & 6 deletions src/Tool.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,34 @@ export interface Link {
title: string;
active: boolean;
onClick: () => void;
left?: string;
right?: string;
}

export type LocaleValue = string | {title: string, left?: string, right?: string};

const getValue = (value: LocaleValue) => {
if (typeof value === 'string') {
return {title: value};
}
return {
title: value.title || '',
left: value.left,
right: value.right
};
}

const getLocales = (
locales: string[],
locales: Record<string, LocaleValue>,
locale: string,
onSelect: (selected: string) => void
): Link[] =>
locales
? Object.entries(locales).map(([key, name]) => ({
id: key,
title: name,
active: key === locale,
onClick: () => onSelect(key),
? Object.entries(locales).map(([key, value]) => ({
...getValue(value),
id: key,
active: key === locale,
onClick: () => onSelect(key),
}))
: [
{
Expand Down
6 changes: 6 additions & 0 deletions stories/Test.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import React from 'react';

const Test = () => <div>Test</div>;

export default Test;

8 changes: 8 additions & 0 deletions stories/test.stories.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import Test from './Test';

export default {
component: Test,
title: 'Test',
};

export const Default = () => <Test/>;