-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #101 from stasm/fluent-react-overlays
DOM overlays for fluent-react
- Loading branch information
Showing
21 changed files
with
496 additions
and
413 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
{ | ||
"name": "fluent-react-example-html-markup", | ||
"version": "0.1.0", | ||
"private": true, | ||
"devDependencies": { | ||
"react-scripts": "1.0.17" | ||
}, | ||
"dependencies": { | ||
"fluent": "file:../../../fluent", | ||
"fluent-intl-polyfill": "file:../../../fluent-intl-polyfill", | ||
"fluent-langneg": "file:../../../fluent-langneg", | ||
"fluent-react": "file:../../../fluent-react", | ||
"react": "^16.1.1", | ||
"react-dom": "^16.1.1" | ||
}, | ||
"scripts": { | ||
"start": "react-scripts start", | ||
"build": "react-scripts build" | ||
} | ||
} |
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 @@ | ||
<!doctype html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="utf-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1"> | ||
<title>HTML Markup in translations - a fluent-react example</title> | ||
<style> | ||
button.text { | ||
background: none; | ||
border: none; | ||
padding: 0; | ||
color: blue; | ||
} | ||
</style> | ||
</head> | ||
<body> | ||
<div id="root"></div> | ||
</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,29 @@ | ||
import React from 'react'; | ||
import { Localized, withLocalization } from 'fluent-react'; | ||
|
||
function App(props) { | ||
function showAlert(id) { | ||
const { getString } = props; | ||
alert(getString(id)); | ||
} | ||
|
||
return ( | ||
<div> | ||
<Localized id="sign-in-or-cancel" | ||
signin={<button onClick={() => showAlert('clicked-sign-in')}></button>} | ||
cancel={<button className="text" onClick={() => showAlert('clicked-cancel')}></button>} | ||
> | ||
<p>{'<signin>Sign in</signin> or <cancel>cancel</cancel>.'}</p> | ||
</Localized> | ||
|
||
<Localized id="agree-prompt" | ||
input={<input type="text" />} | ||
button={<button onClick={() => showAlert('agree-alert')}></button>} | ||
> | ||
<p>{'My name is <input/> and <button>I agree</button>.'}</p> | ||
</Localized> | ||
</div> | ||
); | ||
} | ||
|
||
export default withLocalization(App); |
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 @@ | ||
import React from 'react'; | ||
import ReactDOM from 'react-dom'; | ||
import { LocalizationProvider } from 'fluent-react'; | ||
|
||
import { generateMessages } from './l10n'; | ||
import App from './App'; | ||
|
||
ReactDOM.render( | ||
<LocalizationProvider messages={generateMessages(navigator.languages)}> | ||
<App /> | ||
</LocalizationProvider>, | ||
document.getElementById('root') | ||
); |
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,37 @@ | ||
import 'fluent-intl-polyfill'; | ||
import { MessageContext } from 'fluent'; | ||
import { negotiateLanguages } from 'fluent-langneg'; | ||
|
||
const MESSAGES_ALL = { | ||
'pl': ` | ||
sign-in-or-cancel = <signin>Zaloguj</signin> albo <cancel>anuluj</cancel>. | ||
clicked-sign-in = Brawo! | ||
clicked-cancel = OK, nieważne. | ||
agree-prompt = Nazywam się <input/> i <button>zgadzam się</button>. | ||
agree-alert = Fajnie, zgadzamy się. | ||
`, | ||
'en-US': ` | ||
sign-in-or-cancel = <signin>Sign in</signin> or <cancel>cancel</cancel>. | ||
clicked-sign-in = You are now signed in. | ||
clicked-cancel = OK, nevermind. | ||
agree-prompt = My name is <input/> and <button>I agree</button>. | ||
agree-alert = Cool, agreed. | ||
`, | ||
}; | ||
|
||
export function* generateMessages(userLocales) { | ||
// Choose locales that are best for the user. | ||
const currentLocales = negotiateLanguages( | ||
userLocales, | ||
['en-US', 'pl'], | ||
{ defaultLocale: 'en-US' } | ||
); | ||
|
||
for (const locale of currentLocales) { | ||
const cx = new MessageContext(locale); | ||
cx.addMessages(MESSAGES_ALL[locale]); | ||
yield cx; | ||
} | ||
} |
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,8 @@ | ||
/* eslint-env browser */ | ||
|
||
const TEMPLATE = document.createElement('template'); | ||
|
||
export function parseMarkup(str) { | ||
TEMPLATE.innerHTML = str; | ||
return TEMPLATE.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
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.