Skip to content
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

i18n: Extract concatenated strings of translate functions #2655

Merged
merged 1 commit into from
Sep 6, 2017
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
28 changes: 26 additions & 2 deletions i18n/babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,29 @@ const VALID_TRANSLATION_KEYS = [ 'msgid', 'msgid_plural', 'msgctxt' ];
*/
const REGEXP_TRANSLATOR_COMMENT = /^\s*translators:\s*([\s\S]+)/im;

/**
* Given an argument node (or recursed node), attempts to return a string
* represenation of that node's value.
*
* @param {Object} node AST node
* @return {String} String value
*/
function getNodeAsString( node ) {
switch ( node.type ) {
case 'BinaryExpression':
return (
getNodeAsString( node.left ) +
getNodeAsString( node.right )
);

case 'StringLiteral':
return node.value;

default:
return '';
}
}

/**
* Returns translator comment for a given AST traversal path if one exists.
*
Expand Down Expand Up @@ -185,7 +208,7 @@ module.exports = function() {
const translation = path.node.arguments.reduce( ( memo, arg, i ) => {
const key = functionKeys[ i ];
if ( isValidTranslationKey( key ) ) {
memo[ key ] = arg.value;
memo[ key ] = getNodeAsString( arg );
}

return memo;
Expand Down Expand Up @@ -312,6 +335,7 @@ module.exports = function() {
};
};

module.exports.getNodeAsString = getNodeAsString;
module.exports.getTranslatorComment = getTranslatorComment;
module.exports.isValidTranslationKey = isValidTranslationKey;
module.exports.isSameTranslation = isSameTranslation;
module.exports.getTranslatorComment = getTranslatorComment;
34 changes: 33 additions & 1 deletion i18n/test/babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import babelPlugin from '../babel-plugin';

describe( 'babel-plugin', () => {
const {
getNodeAsString,
getTranslatorComment,
isValidTranslationKey,
isSameTranslation,
getTranslatorComment,
} = babelPlugin;

describe( '.isValidTranslationKey()', () => {
Expand Down Expand Up @@ -90,4 +91,35 @@ describe( 'babel-plugin', () => {
expect( comment ).toBe( 'Long comment spanning multiple lines' );
} );
} );

describe( '.getNodeAsString()', () => {
function getNodeAsStringFromArgument( source ) {
let string;
traverse( transform( source ).ast, {
CallExpression( path ) {
string = getNodeAsString( path.node.arguments[ 0 ] );
},
} );

return string;
}

it( 'should returns an empty string by default', () => {
const string = getNodeAsStringFromArgument( '__( {} );' );

expect( string ).toBe( '' );
} );

it( 'should return a string value', () => {
const string = getNodeAsStringFromArgument( '__( "hello" );' );

expect( string ).toBe( 'hello' );
} );

it( 'should be a concatenated binary expression string value', () => {
const string = getNodeAsStringFromArgument( '__( "hello" + " world" );' );

expect( string ).toBe( 'hello world' );
} );
} );
} );