Skip to content

Commit

Permalink
Babel Makepot: Translator comments have been extracted from the file (#…
Browse files Browse the repository at this point in the history
…9440)

* Comments have been extracted from the file

* Update POT comment behavior in PHP file generation

* Convert function names

* Babel Plugin Makepot: Add CHANGELOG entry for translator -> extracted
  • Loading branch information
akirk authored and youknowriad committed Mar 6, 2019
1 parent 9a25f00 commit 6d6ceee
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 15 deletions.
1 change: 1 addition & 0 deletions packages/babel-plugin-makepot/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Bug Fix

- Fixed Babel plugin for POT file generation to properly handle plural numbers specified in the passed header. ([#13577](https://github.com/WordPress/gutenberg/pull/13577))
- Fix extracted translator comments to be written as prefixed by `#.` ([#9440](https://github.com/WordPress/gutenberg/pull/9440))

## 2.1.0 (2018-09-05)

Expand Down
14 changes: 7 additions & 7 deletions packages/babel-plugin-makepot/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ function getNodeAsString( node ) {
}

/**
* Returns translator comment for a given AST traversal path if one exists.
* Returns the extracted comment for a given AST traversal path if one exists.
*
* @param {Object} path Traversal path.
* @param {number} _originalNodeLine Private: In recursion, line number of
* the original node passed.
*
* @return {?string} Translator comment.
* @return {?string} Extracted comment.
*/
function getTranslatorComment( path, _originalNodeLine ) {
function getExtractedComment( path, _originalNodeLine ) {
const { node, parent, parentPath } = path;

// Assign original node line so we can keep track in recursion whether a
Expand Down Expand Up @@ -152,7 +152,7 @@ function getTranslatorComment( path, _originalNodeLine ) {
// Only recurse as long as parent node is on the same or previous line
const { line } = parent.loc.start;
if ( line >= _originalNodeLine - 1 && line <= _originalNodeLine ) {
return getTranslatorComment( parentPath, _originalNodeLine );
return getExtractedComment( parentPath, _originalNodeLine );
}
}

Expand Down Expand Up @@ -266,9 +266,9 @@ module.exports = function() {
};

// If exists, also assign translator comment
const translator = getTranslatorComment( path );
const translator = getExtractedComment( path );
if ( translator ) {
translation.comments.translator = translator;
translation.comments.extracted = translator;
}

// Create context grouping for translation if not yet exists
Expand Down Expand Up @@ -340,6 +340,6 @@ module.exports = function() {
};

module.exports.getNodeAsString = getNodeAsString;
module.exports.getTranslatorComment = getTranslatorComment;
module.exports.getExtractedComment = getExtractedComment;
module.exports.isValidTranslationKey = isValidTranslationKey;
module.exports.isSameTranslation = isSameTranslation;
6 changes: 3 additions & 3 deletions packages/babel-plugin-makepot/test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import babelPlugin from '../src';
describe( 'babel-plugin', () => {
const {
getNodeAsString,
getTranslatorComment,
getExtractedComment,
isValidTranslationKey,
isSameTranslation,
} = babelPlugin;
Expand Down Expand Up @@ -43,12 +43,12 @@ describe( 'babel-plugin', () => {
} );
} );

describe( '.getTranslatorComment()', () => {
describe( '.getExtractedComment()', () => {
function getCommentFromString( string ) {
let comment;
traverse( transformSync( string, { ast: true } ).ast, {
CallExpression( path ) {
comment = getTranslatorComment( path );
comment = getExtractedComment( path );
},
} );

Expand Down
10 changes: 5 additions & 5 deletions packages/i18n/tools/pot-to-php.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ function convertTranslationToPHP( translation, textdomain, context = '' ) {
NEWLINE;
}

if ( ! isEmpty( comments.extracted ) ) {
if ( ! isEmpty( comments.translator ) ) {
// All extracted comments are split by newlines, add a tab to line them up nicely.
const extracted = comments.extracted
const translator = comments.translator
.split( NEWLINE )
.join( NEWLINE + TAB + ' ' );

php += TAB + `/* ${ extracted } */${ NEWLINE }`;
php += TAB + `/* ${ translator } */${ NEWLINE }`;
}

if ( ! isEmpty( comments.translator ) ) {
php += TAB + `/* translators: ${ comments.translator } */${ NEWLINE }`;
if ( ! isEmpty( comments.extracted ) ) {
php += TAB + `/* translators: ${ comments.extracted } */${ NEWLINE }`;
}
}

Expand Down

0 comments on commit 6d6ceee

Please sign in to comment.