-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Framework: Add localization helpers to global wpcom instance #3348
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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 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,10 @@ | ||
REPORTER ?= spec | ||
NODE_BIN := ../../../../node_modules/.bin | ||
MOCHA ?= $(NODE_BIN)/mocha | ||
BASE_DIR := $(NODE_BIN)/../.. | ||
NODE_PATH := $(BASE_DIR)/client | ||
|
||
test: | ||
@NODE_ENV=test NODE_PATH=$(NODE_PATH) $(MOCHA) --compilers jsx:babel/register,js:babel/register --reporter $(REPORTER) | ||
|
||
.PHONY: test |
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 @@ | ||
wpcom.js Localization | ||
===================== | ||
|
||
This module enables the extension of a `wpcom.js` instance to include localization helper functions. Specifically, the modified instance will include a new `withLocale` function, which will result in the subsequent chained request being localized according to the current user's preferred locale. | ||
|
||
## Usage | ||
|
||
The helper is already bound for the global instance of `wpcom.js` used in Calypso. To take advantage of the localization helpers, call the `withLocale` function at the start of your request chain. | ||
|
||
```js | ||
import wpcom from 'lib/wp'; | ||
|
||
wpcom.withLocale().site( siteId ).postTypesList().then( ( data ) => { | ||
// `data` is a localized response | ||
} ); | ||
``` |
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,74 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import qs from 'querystring'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { getCurrentUserLocale } from 'state/current-user/selectors'; | ||
|
||
/** | ||
* Module variables | ||
*/ | ||
let locale; | ||
|
||
/** | ||
* Given a WPCOM parameter set, modifies the query such that a non-default | ||
* locale is added to the query parameter. | ||
* | ||
* @param {Object} params Original parameters | ||
* @return {Object} Revised parameters, if non-default locale | ||
*/ | ||
export function addLocaleQueryParam( params ) { | ||
if ( ! locale || 'en' === locale ) { | ||
return params; | ||
} | ||
|
||
let query = qs.parse( params.query ); | ||
return Object.assign( params, { | ||
query: qs.stringify( Object.assign( query, { locale } ) ) | ||
} ); | ||
}; | ||
|
||
/** | ||
* Modifies a WPCOM instance, returning an updated instance with included | ||
* localization helpers. Specifically, this adds a new `withLocale` method to | ||
* the base instance for indicating the request should be localized. | ||
* | ||
* @param {Object} wpcom Original WPCOM instance | ||
* @return {Object} Modified WPCOM instance with localization helpers | ||
*/ | ||
export function injectLocalization( wpcom ) { | ||
const request = wpcom.request.bind( wpcom ); | ||
return Object.assign( wpcom, { | ||
withLocale: function() { | ||
this.localize = true; | ||
return this; | ||
}, | ||
|
||
request: function( params, callback ) { | ||
if ( this.localize ) { | ||
this.localize = false; | ||
return request( addLocaleQueryParam( params ), callback ); | ||
} | ||
|
||
return request( params, callback ); | ||
} | ||
} ); | ||
} | ||
|
||
/** | ||
* Subscribes to the provided Redux store instance, updating the known locale | ||
* value to the latest value when state changes. | ||
* | ||
* @param {Object} store Redux store instance | ||
*/ | ||
export function bindState( store ) { | ||
function setLocale() { | ||
locale = getCurrentUserLocale( store.getState() ); | ||
} | ||
|
||
store.subscribe( setLocale ); | ||
setLocale(); | ||
} |
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,146 @@ | ||
/** | ||
* External dependencies | ||
*/ | ||
import { expect } from 'chai'; | ||
import rewire from 'rewire'; | ||
import mockery from 'mockery'; | ||
import sinon from 'sinon'; | ||
|
||
describe( 'localization', () => { | ||
let localization, addLocaleQueryParam, injectLocalization, bindState; | ||
let getCurrentUserLocaleMock = sinon.stub(); | ||
|
||
before( () => { | ||
// Mock user locale state selector | ||
mockery.enable( { | ||
warnOnReplace: false, | ||
warnOnUnregistered: false | ||
} ); | ||
mockery.registerMock( 'state/current-user/selectors', { | ||
getCurrentUserLocale: () => getCurrentUserLocaleMock() | ||
} ); | ||
|
||
// Prepare module for rewiring | ||
localization = rewire( '../' ); | ||
addLocaleQueryParam = localization.addLocaleQueryParam; | ||
injectLocalization = localization.injectLocalization; | ||
bindState = localization.bindState; | ||
} ); | ||
|
||
beforeEach( () => { | ||
localization.__set__( 'locale', undefined ); | ||
} ); | ||
|
||
after( function() { | ||
mockery.disable(); | ||
} ); | ||
|
||
describe( '#addLocaleQueryParam()', () => { | ||
it( 'should not modify params if locale unknown', () => { | ||
const params = addLocaleQueryParam( { query: 'search=foo' } ); | ||
|
||
expect( params ).to.eql( { query: 'search=foo' } ); | ||
} ); | ||
|
||
it( 'should not modify params if locale is default', () => { | ||
localization.__set__( 'locale', 'en' ); | ||
const params = addLocaleQueryParam( { query: 'search=foo' } ); | ||
|
||
expect( params ).to.eql( { query: 'search=foo' } ); | ||
} ); | ||
|
||
it( 'should include the locale query parameter for a non-default locale', () => { | ||
localization.__set__( 'locale', 'fr' ); | ||
const params = addLocaleQueryParam( { query: 'search=foo' } ); | ||
|
||
expect( params ).to.eql( { | ||
query: 'search=foo&locale=fr' | ||
} ); | ||
} ); | ||
} ); | ||
|
||
describe( '#injectLocalization()', () => { | ||
it( 'should return a modified object', () => { | ||
let wpcom = { request() {} }; | ||
injectLocalization( wpcom ); | ||
|
||
expect( wpcom.withLocale ).to.be.a( 'function' ); | ||
} ); | ||
|
||
it( 'should override the default request method', () => { | ||
const request = () => {}; | ||
let wpcom = { request }; | ||
injectLocalization( wpcom ); | ||
|
||
expect( wpcom.request ).to.not.equal( request ); | ||
} ); | ||
|
||
it( 'should not modify params if `withLocale` not used', ( done ) => { | ||
localization.__set__( 'locale', 'fr' ); | ||
let wpcom = { | ||
request( params ) { | ||
expect( params.query ).to.equal( 'search=foo' ); | ||
done(); | ||
} | ||
}; | ||
|
||
injectLocalization( wpcom ); | ||
wpcom.request( { query: 'search=foo' } ); | ||
} ); | ||
|
||
it( 'should modify params if `withLocale` is used', ( done ) => { | ||
localization.__set__( 'locale', 'fr' ); | ||
let wpcom = { | ||
request( params ) { | ||
expect( params.query ).to.equal( 'search=foo&locale=fr' ); | ||
done(); | ||
} | ||
}; | ||
|
||
injectLocalization( wpcom ); | ||
wpcom.withLocale().request( { query: 'search=foo' } ); | ||
} ); | ||
|
||
it( 'should not modify the request after `withLocale` is used', ( done ) => { | ||
localization.__set__( 'locale', 'fr' ); | ||
let assert = false; | ||
let wpcom = { | ||
request( params ) { | ||
if ( ! assert ) { | ||
return; | ||
} | ||
|
||
expect( params.query ).to.equal( 'search=foo' ); | ||
done(); | ||
} | ||
}; | ||
|
||
injectLocalization( wpcom ); | ||
wpcom.withLocale().request( { query: 'search=foo' } ); | ||
assert = true; | ||
wpcom.request( { query: 'search=foo' } ); | ||
} ); | ||
} ); | ||
|
||
describe( '#bindState()', () => { | ||
it( 'should set initial locale from state', () => { | ||
getCurrentUserLocaleMock = sinon.stub().returns( 'fr' ); | ||
bindState( { subscribe() {}, getState() {} } ); | ||
expect( localization.__get__( 'locale' ) ).to.equal( 'fr' ); | ||
} ); | ||
|
||
it( 'should subscribe to the store, setting locale on change', () => { | ||
let listener; | ||
bindState( { | ||
subscribe( _listener ) { | ||
listener = _listener; | ||
}, | ||
getState() {} | ||
} ); | ||
getCurrentUserLocaleMock = sinon.stub().returns( 'de' ); | ||
listener(); | ||
|
||
expect( localization.__get__( 'locale' ) ).to.equal( 'de' ); | ||
} ); | ||
} ); | ||
} ); |
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,13 +1,17 @@ | ||
/** | ||
* Internal dependencies | ||
*/ | ||
var wpcom = require( 'lib/wpcom-undocumented' ); | ||
var config = require( 'config' ); | ||
import wpcomUndocumented from 'lib/wpcom-undocumented'; | ||
import config from 'config'; | ||
import { injectLocalization } from './localization'; | ||
|
||
wpcom = wpcom( require( 'wpcom-xhr-request' ) ); | ||
let wpcom = wpcomUndocumented( require( 'wpcom-xhr-request' ) ); | ||
|
||
if ( config.isEnabled( 'support-user' ) ) { | ||
wpcom = require( 'lib/wp/support' )( wpcom ); | ||
} | ||
|
||
// Inject localization helpers to `wpcom` instance | ||
wpcom = injectLocalization( wpcom ); | ||
|
||
module.exports = wpcom; |
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
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.
add
-
?. I've gotten this suggestion before from @rralianThere 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.
The hyphen itself is optional, and is available to help readability (reference), though a similar effect is achieved via aligning spaces on the start of the type, name, and description.
Usage seems a bit mixed throughout Calypso. In my own case, I'm using the Sublime Text 3 DocBlockr package, which does not add the hyphens and (as far as I can tell) does not offer an option to do so, so it's a bit more effort for me to revise. TL;DR: I'm lazy 😆
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.
:-D