Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
48 changed files
with
369 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# Dictionary | ||
We added support to our own dictionary. It handles common translations in components like `Close` | ||
|
||
You have available our dictionary in `@kiwicom/orbit-components/lib/data/dictionary/...` | ||
There are files which contains our own translations. | ||
|
||
**Example:** | ||
```jsx | ||
import en_GB from "@kiwicom/orbit-components/lib/data/dictionary/en-GB.json"; | ||
import ThemeProvider from "@kiwicom/orbit-components/lib/ThemeProvider"; | ||
import Tooltip from "@kiwicom/orbit-components/lib/Tooltip"; | ||
import Heading from "@kiwicom/orbit-components/lib/Heading"; | ||
|
||
const App = () => | ||
<ThemeProvider dictionary={en_GB}> | ||
<Tooltip content="Write your text here. If it’s longer than three li…"> | ||
<Heading> | ||
Orbit design system | ||
</Heading> | ||
</Tooltip> | ||
</ThemeProvider>; | ||
``` | ||
|
||
**Fallbacks** | ||
|
||
* If translation key not exists in your language the fallback is `en_GB` which is our default lang | ||
* If translation key not exists in both files (your language, default language), the translation key will be rendered e.g. `button_close` | ||
|
||
**Your own dictionary** | ||
|
||
There is option to add your own dictionary, just pass object containing keys and values. | ||
|
||
```jsx | ||
import ThemeProvider from "@kiwicom/orbit-components/lib/ThemeProvider"; | ||
|
||
const App = () => | ||
<ThemeProvider dictionary={{ | ||
"button_close": "My own translation" | ||
}}> | ||
<Button type="secondary" size="large" /> | ||
</ThemeProvider>; | ||
``` | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
// @flow | ||
import fetch from "isomorphic-unfetch"; | ||
import path from "path"; | ||
import dotenv from "dotenv"; | ||
import fs from "fs-extra"; | ||
|
||
dotenv.config(); | ||
const env = name => process.env[name] || ""; | ||
|
||
const PHRASE_APP_BASE_URL = "https://api.phraseapp.com/api/v2"; | ||
const PHRASE_APP_PROJECT_ID = env("PHRASE_APP_PROJECT_ID"); | ||
const PHRASE_APP_ACCESS_TOKEN = env("PHRASE_APP_ACCESS_TOKEN"); | ||
|
||
const LOCALES_URL = `${PHRASE_APP_BASE_URL}/projects/${PHRASE_APP_PROJECT_ID}/locales`; | ||
const FILE_FORMAT = "nested_json"; | ||
|
||
const fetchJSON = async url => { | ||
const options = { | ||
method: "GET", | ||
headers: { | ||
"Content-Type": "application/json", | ||
Authorization: `token ${PHRASE_APP_ACCESS_TOKEN}`, | ||
}, | ||
}; | ||
return (await fetch(url, options)).json(); | ||
}; | ||
|
||
const writeJSON = (filename, obj) => | ||
new Promise((resolve, reject) => { | ||
fs.outputFile(filename, JSON.stringify(obj, null, 2), "utf8", err => { | ||
if (err) { | ||
reject(err); | ||
} | ||
|
||
resolve(); | ||
}); | ||
}); | ||
|
||
const flatten = (obj = {}, keyPrefix = "") => | ||
Object.entries(obj).reduce((result, [key, value]) => { | ||
if (value && typeof value === "object") { | ||
return { | ||
...result, | ||
...flatten(value, `${keyPrefix}${key}.`), | ||
}; | ||
} | ||
return { | ||
...result, | ||
[keyPrefix + key]: value, | ||
}; | ||
}, {}); | ||
|
||
(async () => { | ||
try { | ||
const allLocales = await fetchJSON(LOCALES_URL); | ||
const LOCALES_DATA = path.join(__dirname, "..", "src", "data", "dictionary"); | ||
|
||
// PhraseApp has limits on parallel requests | ||
// that's why we process requests in sequence | ||
// eslint-disable-next-line no-restricted-syntax | ||
for (const locale of allLocales) { | ||
const translation = await fetchJSON( | ||
`${LOCALES_URL}/${locale.id}/download?file_format=${FILE_FORMAT}&tags=orbit&encoding=UTF-8`, | ||
); | ||
|
||
await writeJSON(path.join(LOCALES_DATA, `${locale.code}.json`), flatten(translation.orbit)); | ||
} | ||
} catch (error) { | ||
console.error(error); // eslint-disable-line no-console | ||
process.exit(1); | ||
} | ||
})(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// @flow | ||
import * as React from "react"; | ||
|
||
import type { DictionaryContextType, Props } from "./index"; | ||
|
||
export const DictionaryContext: DictionaryContextType = React.createContext({}); | ||
|
||
const Dictionary = ({ values, children }: Props) => ( | ||
<DictionaryContext.Provider value={values}>{children}</DictionaryContext.Provider> | ||
); | ||
|
||
export default Dictionary; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// @flow | ||
import * as React from "react"; | ||
|
||
export type Translations = { | ||
[key: string]: string, | ||
}; | ||
|
||
export type DictionaryContextType = React.Context<Translations>; | ||
|
||
export type Props = { | ||
+values: Translations, | ||
+children: React$Node, | ||
}; | ||
|
||
declare export default React$ComponentType<Props>; | ||
|
||
declare export var DictionaryContext: DictionaryContextType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# ThemeProvider | ||
orbit-components has theming support via our own `<ThemeProvider>` which adds you possibilities to add your own theme and dictionary. | ||
```jsx | ||
import ThemeProvider from "@kiwicom/orbit-components/lib/ThemeProvider"; | ||
``` | ||
After adding import please wrap your application into `ThemeProvider` and you can provide your own [`theme`](https://github.com/kiwicom/orbit-components/blob/master/.github/theming.md) and [`dictionary`](https://github.com/kiwicom/orbit-components/blob/master/.github/dictionary.md) | ||
|
||
```jsx | ||
<ThemeProvider theme={...} dictionary={...}> | ||
<App /> | ||
</ThemeProvider> | ||
``` | ||
## Props | ||
Table below contains all types of the props available in the ThemeProvider component. | ||
|
||
| Name | Type | Default | Description | | ||
| :------------ | :------------------------ | :-------------- | :------------------------------- | | ||
| **children** | `React.Node` | | Your app | ||
| theme | `[Object]` | | See [`theming`](https://github.com/kiwicom/orbit-components/blob/master/.github/theming.md) | ||
| dictionary | `[Object]` | | See [`dictionary`](https://github.com/kiwicom/orbit-components/blob/master/.github/dictionary.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// @flow | ||
import React from "react"; | ||
import { ThemeProvider as StyledThemeProvider } from "styled-components"; | ||
|
||
import Dictionary from "../Dictionary"; | ||
|
||
import type { Props } from "./index"; | ||
|
||
const ThemeProvider = ({ theme, dictionary, children }: Props) => ( | ||
<StyledThemeProvider theme={theme}> | ||
{dictionary ? ( | ||
<Dictionary values={dictionary}>{React.Children.only(children)}</Dictionary> | ||
) : ( | ||
React.Children.only(children) | ||
)} | ||
</StyledThemeProvider> | ||
); | ||
|
||
export default ThemeProvider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// @flow | ||
/* | ||
DOCUMENTATION: https://orbit.kiwi/components/themeprovider/ | ||
*/ | ||
|
||
import type { Translations } from "../Dictionary"; | ||
|
||
export type Props = {| | ||
+theme: any, | ||
+dictionary?: Translations, | ||
+children: React$Node, | ||
|}; | ||
|
||
declare export default React$ComponentType<Props>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.