From 0e999cbe5f24be82fe1bf039f5423b9513d3ec70 Mon Sep 17 00:00:00 2001 From: Alex Kirk Date: Thu, 7 Feb 2019 17:44:23 +0100 Subject: [PATCH] Babel Makepot: Translator comments have been extracted from the file (#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 --- packages/babel-plugin-makepot/CHANGELOG.md | 1 + packages/babel-plugin-makepot/src/index.js | 14 +++++++------- packages/babel-plugin-makepot/test/index.js | 6 +++--- packages/i18n/tools/pot-to-php.js | 10 +++++----- 4 files changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/babel-plugin-makepot/CHANGELOG.md b/packages/babel-plugin-makepot/CHANGELOG.md index 8079f5197a2fd..e48eeff4bca3e 100644 --- a/packages/babel-plugin-makepot/CHANGELOG.md +++ b/packages/babel-plugin-makepot/CHANGELOG.md @@ -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) diff --git a/packages/babel-plugin-makepot/src/index.js b/packages/babel-plugin-makepot/src/index.js index cf66b8bbc80f3..ad0cde941aecb 100644 --- a/packages/babel-plugin-makepot/src/index.js +++ b/packages/babel-plugin-makepot/src/index.js @@ -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 @@ -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 ); } } @@ -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 @@ -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; diff --git a/packages/babel-plugin-makepot/test/index.js b/packages/babel-plugin-makepot/test/index.js index 17af021380b9e..52ae1dcc5b913 100644 --- a/packages/babel-plugin-makepot/test/index.js +++ b/packages/babel-plugin-makepot/test/index.js @@ -12,7 +12,7 @@ import babelPlugin from '../src'; describe( 'babel-plugin', () => { const { getNodeAsString, - getTranslatorComment, + getExtractedComment, isValidTranslationKey, isSameTranslation, } = babelPlugin; @@ -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 ); }, } ); diff --git a/packages/i18n/tools/pot-to-php.js b/packages/i18n/tools/pot-to-php.js index 0e5a4f9c68c6f..29aafe3abcf59 100755 --- a/packages/i18n/tools/pot-to-php.js +++ b/packages/i18n/tools/pot-to-php.js @@ -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 }`; } }