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

Babel Makepot: Translator comments have been extracted from the file #9440

Merged
merged 4 commits into from
Feb 7, 2019
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
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