-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Examples for passing MDText via Context
- Loading branch information
Showing
17 changed files
with
206 additions
and
67 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
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 |
---|---|---|
@@ -1,15 +1,41 @@ | ||
var ReactDOM = require('react-dom'); | ||
var React = require('react'); | ||
var T = require('i18n-react').default; | ||
const { default: T, MDText } = require('i18n-react'); | ||
|
||
T.setTexts({ | ||
greeting: "#Hello, World!\n My name is *{myName}*! \n {{howAreYou}}", | ||
const dictionary = { | ||
greeting: "###Hello, World!\n My name is **{myName}**! \n {{howAreYou}}", | ||
howAreYou: "_How do you do?_" | ||
}); | ||
}; | ||
|
||
T.setTexts(dictionary, { MDFlavor: 1 }) | ||
ReactDOM.render( | ||
//<T.span text="greeting" myName="i18n-react"/>, | ||
<T.span text={{ key: "greeting", myName: "i18n-react" }}/>, | ||
document.getElementById('content') | ||
<section> | ||
<h2>Singelton</h2> | ||
<T.text text={{ key: "greeting", myName: "i18n-react" }}/> | ||
</section>, | ||
document.getElementById('content1') | ||
); | ||
|
||
const Texts = new MDText(dictionary, { MDFlavor: 1 }); | ||
ReactDOM.render( | ||
<section> | ||
<h2>MDText object</h2> | ||
<Texts.text text={{ key: "greeting", myName: "i18n-react" }}/> | ||
</section>, | ||
document.getElementById('content2') | ||
); | ||
|
||
let MDTextContext = React.createContext(); | ||
|
||
ReactDOM.render( | ||
<section> | ||
<h2>MDText in React Context</h2> | ||
<MDTextContext.Provider value={Texts}> | ||
<MDTextContext.Consumer>{ (TT) => | ||
<TT.text text={{ key: "greeting", myName: "i18n-react" }}/> | ||
}</MDTextContext.Consumer> | ||
</MDTextContext.Provider> | ||
</section>, | ||
document.getElementById('content3') | ||
); | ||
|
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,16 @@ | ||
var React = require('react'); | ||
import MDTextContext from './text-context'; | ||
import { MDText } from '../../dist/i18n-react'; | ||
export default function HelloBlock(props: { name: string, days: number, style: string }) { | ||
return ( | ||
<MDTextContext.Consumer>{(T: MDText) => | ||
<div style={{ padding: "0 0 20px 30px", borderBottom: "solid 1px" }}> | ||
<T.p text={{ key: "greetings.hello", who: props.name, context: props.style }} /> | ||
<T.p text={{ key: "greetings.howdy", who: props.name, context: props.style }} style={{ fontSize: "90%" }} /> | ||
<T.text tag='section' text="longTime" context={props.days} /> | ||
<T.text tag='article' text='lorem' style={{ padding: "10px 50px" }} /> | ||
<T.a text={{ key: "greetings.bye", who: props.name, context: props.style }} onClick={() => alert("Cya")} href="#" /> | ||
</div> | ||
}</MDTextContext.Consumer> | ||
); | ||
} |
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,13 @@ | ||
<!-- index.html --> | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<title>I18N-React</title> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.3.0/umd/react.development.js"></script> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.3.0/umd/react-dom.development.js"></script> | ||
</head> | ||
<body> | ||
<div id="content"></div> | ||
<script src="build/index.js"></script> | ||
</body> | ||
</html> |
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,22 @@ | ||
var React = require('react'); | ||
var ReactDOM = require('react-dom'); | ||
import { MDText } from '../../dist/i18n-react'; | ||
import HelloBlock from './hello-block'; | ||
import MDTextContext from './text-context'; | ||
|
||
|
||
/* Initialize once - probably in entry point js somwehere near the router */ | ||
var T = new MDText(require('./texts.yml')); | ||
|
||
ReactDOM.render( | ||
<MDTextContext.Provider value={T}> | ||
<section> | ||
<HelloBlock name="World" style="formal" days={0} /> | ||
<HelloBlock name="State" style="normal" days={1} /> | ||
<HelloBlock name="City" style="informal" days={3} /> | ||
<HelloBlock name="Town" style="informal" days={5} /> | ||
</section> | ||
</MDTextContext.Provider>, | ||
document.getElementById('content') | ||
); | ||
|
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,4 @@ | ||
var React = require('react'); | ||
import T from '../../dist/i18n-react'; | ||
export default React.createContext(T); | ||
|
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,22 @@ | ||
greetings: | ||
hello: Hi, {who}! | ||
howdy: | ||
formal: How do you do? | ||
normal: How are you, {who}? | ||
informal: "What's up?" | ||
bye: Bye | ||
|
||
longTime: | ||
0: Not a day no see | ||
1: 1 day no see | ||
'2..4': A few days no see | ||
_: "[{context} days][Long time no see]" | ||
|
||
lorem: | | ||
# Lorem | ||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. | ||
## Quisque | ||
Quisque sit amet nisi quis eros cursus finibus quis sed nisl. | ||
### Integer | ||
Integer efficitur pharetra pretium. | ||
Sed semper dui vitae nibh scelerisque sodales. |
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,8 @@ | ||
{ | ||
"compilerOptions": { | ||
"jsx": "react", | ||
"module": "commonjs", | ||
"strict": true, | ||
"strictNullChecks": false | ||
} | ||
} |
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 |
---|---|---|
@@ -1,18 +1,18 @@ | ||
var React = require('react'); | ||
var T = require('i18n-react').default; | ||
var MDTextContext = require('text-context'); | ||
|
||
class HelloBlock extends React.Component { | ||
render() { | ||
return ( | ||
<div style={ { padding: "0 0 20px 30px", borderBottom: "solid 1px" } }> | ||
<T.p text={{ key: "greetings.hello", who: this.props.name, context: this.props.style}} /> | ||
<T.p text={{ key: "greetings.howdy", who: this.props.name, context: this.props.style}} style={{ fontSize:"90%" }} /> | ||
<T.text tag='section' text="longTime" context={this.props.days} /> | ||
<T.text tag='article' text='lorem' style={ { padding: "10px 50px" }}/> | ||
<T.a text={{ key: "greetings.bye", who: this.props.name, context: this.props.style}} onClick={ () => alert("Cya")} href="#"/> | ||
</div> | ||
); | ||
} | ||
function HelloBlock(props) { | ||
return ( | ||
<MDTextContext.Consumer>{ (T) => | ||
<div style={ { padding: "0 0 20px 30px", borderBottom: "solid 1px" } }> | ||
<T.p text={{ key: "greetings.hello", who: props.name, context: props.style}} /> | ||
<T.p text={{ key: "greetings.howdy", who: props.name, context: props.style}} style={{ fontSize:"90%" }} /> | ||
<T.text tag='section' text="longTime" context={props.days} /> | ||
<T.text tag='article' text='lorem' style={ { padding: "10px 50px" }}/> | ||
<T.a text={{ key: "greetings.bye", who: props.name, context: props.style}} onClick={ () => alert("Cya")} href="#"/> | ||
</div> | ||
}</MDTextContext.Consumer> | ||
); | ||
} | ||
|
||
module.exports = HelloBlock |
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 |
---|---|---|
@@ -1,18 +1,21 @@ | ||
var React = require('react'); | ||
var ReactDOM = require('react-dom'); | ||
var T = require('i18n-react').default; | ||
var MDText = require('i18n-react').MDText; | ||
var HelloBlock = require('./hello-block'); | ||
var MDTextContext = require('text-context'); | ||
|
||
/* Initialize once - probably in entry point js somwehere near the router */ | ||
T.setTexts( require('./texts.yml')); | ||
var T = new MDText(require('./texts.yml')); | ||
|
||
ReactDOM.render( | ||
<section> | ||
<HelloBlock name="World" style="formal" days={0}/> | ||
<HelloBlock name="State" style="normal" days={1}/> | ||
<HelloBlock name="City" style="informal" days={3}/> | ||
<HelloBlock name="Town" style="informal" days={5}/> | ||
</section>, | ||
<MDTextContext.Provider value={T}> | ||
<section> | ||
<HelloBlock name="World" style="formal" days={0}/> | ||
<HelloBlock name="State" style="normal" days={1}/> | ||
<HelloBlock name="City" style="informal" days={3}/> | ||
<HelloBlock name="Town" style="informal" days={5}/> | ||
</section> | ||
</MDTextContext.Provider>, | ||
document.getElementById('content') | ||
); | ||
|
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,6 @@ | ||
var React = require('react'); | ||
var T = require('i18n-react').default; | ||
|
||
var MDTextContext = React.createContext(T); | ||
|
||
module.exports = MDTextContext; |
Oops, something went wrong.