-
Notifications
You must be signed in to change notification settings - Fork 80
DOM overlays for fluent-react #101
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
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose there are no unexpected side-effects if the string inlcudes the
<
character outside of the context of a markup element. For exampleThe result is that 0 < 3 ...
.It looks like the
parseMarkup
that comes next will just leverage the DOM API to split out the different parts, and so this step is more of an optimization in case the translation string does not contain react components. Just thinking out loud and checking my understanding - does that sound about right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Correct. In the rare scenario of a false-positive (like in the example you gave) we'll pay a bit extra to parse HTML which is not there. It will parse as a text node and things should work just fine. I'll add a test.