Skip to content

Use React's new Context API #406

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 14 commits into from
Jan 10, 2020
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
14 changes: 0 additions & 14 deletions common.mk
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,6 @@ lint:
@eslint --config $(ROOT)/eslint_test.json --max-warnings 0 test/
@echo -e " $(OK) $@"

test:
ifneq (,$(wildcard ./test/index.js))
@nyc --reporter=text --reporter=html mocha \
--recursive --ui tdd \
--require $(ROOT)/mocha_setup \
--require ./test/index \
test/**/*_test.js
else
@nyc --reporter=text --reporter=html mocha \
--recursive --ui tdd \
--require $(ROOT)/mocha_setup \
test/**/*_test.js
endif

html:
ifneq (,$(wildcard ./.esdoc.json))
@esdoc
Expand Down
7 changes: 7 additions & 0 deletions fluent-bundle/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ include ../common.mk

build: index.js compat.js

test:
@nyc --reporter=text --reporter=html mocha \
--recursive --ui tdd \
--require $(ROOT)/mocha_setup \
--require ./test/index \
test/**/*_test.js

index.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(ROOT)/bundle_config.js \
Expand Down
6 changes: 6 additions & 0 deletions fluent-dedent/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ include ../common.mk

build: index.js compat.js

test:
@nyc --reporter=text --reporter=html mocha \
--recursive --ui tdd \
--require $(ROOT)/mocha_setup \
test/**/*_test.js

index.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(ROOT)/bundle_config.js \
Expand Down
7 changes: 7 additions & 0 deletions fluent-dom/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ DEPS := cached-iterable:CachedIterable

include ../common.mk

test:
@nyc --reporter=text --reporter=html mocha \
--recursive --ui tdd \
--require $(ROOT)/mocha_setup \
--require ./test/index \
test/**/*_test.js

build: index.js compat.js

index.js: $(SOURCES)
Expand Down
6 changes: 6 additions & 0 deletions fluent-gecko/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ include ../common.mk
version = $(1)@$(shell node -e "\
console.log(require('../$(1)/package.json').version)")

test:
@nyc --reporter=text --reporter=html mocha \
--recursive --ui tdd \
--require $(ROOT)/mocha_setup \
test/**/*_test.js

build: Fluent.jsm FluentSyntax.jsm Localization.jsm DOMLocalization.jsm l10n.js fluent-react.js

Fluent.jsm: $(SOURCES)
Expand Down
6 changes: 6 additions & 0 deletions fluent-langneg/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@ GLOBAL := FluentLangNeg

include ../common.mk

test:
@nyc --reporter=text --reporter=html mocha \
--recursive --ui tdd \
--require $(ROOT)/mocha_setup \
test/**/*_test.js

build: index.js compat.js

index.js: $(SOURCES)
Expand Down
16 changes: 16 additions & 0 deletions fluent-react/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// Jest requires us to specify the transforms it needs to run the tests
module.exports = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this file required for running tests with Jest? Please add a one-line comment explaining why we need it :)

"presets": [
"@babel/preset-react",
["@babel/preset-env", {
"targets": "node >= 8.9.0"
}]
],
"plugins": [
["babel-plugin-transform-rename-import", {
"original": "fluent",
"replacement": "fluent/compat"
}],
"@babel/plugin-proposal-async-generator-functions"
],
};
3 changes: 3 additions & 0 deletions fluent-react/makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ include ../common.mk

build: index.js compat.js

test:
jest --collect-coverage

index.js: $(SOURCES)
@rollup $(CURDIR)/src/index.js \
--config $(ROOT)/bundle_config.js \
Expand Down
17 changes: 12 additions & 5 deletions fluent-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,20 @@
"react": "^0.14.9 || ^15.0.0 || ^16.0.0"
},
"devDependencies": {
"@babel/preset-react": "^7.0.0-beta.47",
"@babel/plugin-proposal-async-generator-functions": "^7.2.0",
"@babel/preset-env": "^7.5.5",
"@babel/preset-react": "7.0.0",
"babel-jest": "^24.8.0",
"babel-plugin-transform-rename-import": "^2.2.0",
"enzyme": "^3.3.0",
"enzyme-adapter-react-16": "^1.1.1",
"jsdom": "^11.12.0",
"jest": "^24.8.0",
"prettier": "^1.18.2",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"sinon": "^4.2.2"
"react-test-renderer": "^16.8.6"
},
"jest": {
"transformIgnorePatterns": [
"node_modules/(?!(@fluent/sequence)/)"
]
}
}
7 changes: 7 additions & 0 deletions fluent-react/src/context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createContext } from "react";
import ReactLocalization from "./localization";

export default createContext({
l10n: new ReactLocalization([]),
parseMarkup: null
});
2 changes: 0 additions & 2 deletions fluent-react/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,3 @@
export { default as LocalizationProvider } from "./provider";
export { default as withLocalization } from "./with_localization";
export { default as Localized } from "./localized";
export { default as ReactLocalization, isReactLocalization }
from "./localization";
47 changes: 2 additions & 45 deletions fluent-react/src/localization.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,55 +6,24 @@ import { CachedSyncIterable } from "cached-iterable";
*
* The current negotiated fallback chain of languages is stored in the
* `ReactLocalization` instance in form of an iterable of `FluentBundle`
* instances. This iterable is used to find the best existing translation for
* instances. This iterable is used to find the best existing translation for
* a given identifier.
*
* `Localized` components must subscribe to the changes of the
* `ReactLocalization`'s fallback chain. When the fallback chain changes (the
* `bundles` iterable is set anew), all subscribed compontent must relocalize.
*
* The `ReactLocalization` class instances are exposed to `Localized` elements
* via the `LocalizationProvider` component.
*/
export default class ReactLocalization {
constructor(bundles) {
this.bundles = CachedSyncIterable.from(bundles);
this.subs = new Set();
}

/*
* Subscribe a `Localized` component to changes of `bundles`.
*/
subscribe(comp) {
this.subs.add(comp);
}

/*
* Unsubscribe a `Localized` component from `bundles` changes.
*/
unsubscribe(comp) {
this.subs.delete(comp);
}

/*
* Set a new `bundles` iterable and trigger the retranslation.
*/
setBundles(bundles) {
this.bundles = CachedSyncIterable.from(bundles);

// Update all subscribed Localized components.
this.subs.forEach(comp => comp.relocalize());
}

getBundle(id) {
return mapBundleSync(this.bundles, id);
}

/*
* Find a translation by `id` and format it to a string using `args`.
*/
getString(id, args, fallback) {
const bundle = this.getBundle(id);

if (bundle) {
const msg = bundle.getMessage(id);
if (msg && msg.value) {
Expand All @@ -78,15 +47,3 @@ export default class ReactLocalization {
console.warn(`[@fluent/react] ${error.name}: ${error.message}`);
}
}

export function isReactLocalization(props, propName) {
const prop = props[propName];

if (prop instanceof ReactLocalization) {
return null;
}

return new Error(
`The ${propName} context field must be an instance of ReactLocalization.`
);
}
Loading