Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
23 changes: 19 additions & 4 deletions src/scripts/babel-gettext-extractor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ function isStringLiteral(node: AstNodeT) {
return node.type === 'StringLiteral'
}

function isTemplateLiteral(node: AstNodeT) {
return node.type === 'TemplateLiteral';
}
function isLiteral(node: AstNodeT) {
return isStringLiteral(node) || isTemplateLiteral(node)
}

function isObjectLiteral(node: AstNodeT) {
return node.type === 'ObjectExpression'
}
Expand All @@ -43,8 +50,8 @@ function isStringConcatExpr(node: AstNodeT) {
return (
node.type === 'BinaryExpression' &&
node.operator === '+' &&
((isStringLiteral(left) || isStringConcatExpr(left)) &&
(isStringLiteral(right) || isStringConcatExpr(right)))
((isLiteral(left) || isStringConcatExpr(left)) &&
(isLiteral(right) || isStringConcatExpr(right)))
)
}

Expand All @@ -53,6 +60,15 @@ function getStringValue(node: AstNodeT) {
return node.value
}

if (isTemplateLiteral(node)) {
// Support only simple template literals without expressions
if (node.expressions.length > 0 || node.quasis.length !== 1) {
return null
}
return node.quasis[0].value.cooked

}

if (isStringConcatExpr(node)) {
return getStringValue(node.left) + getStringValue(node.right)
}
Expand Down Expand Up @@ -208,8 +224,7 @@ export default function plugin() {
const ctxtProp = getContextProperty(options)

if (ctxtProp) {
const messageContext = ctxtProp.value.extra.rawValue

const messageContext = getStringValue(ctxtProp.value)
if (messageContext) {
translate.msgctxt = messageContext
}
Expand Down
4 changes: 4 additions & 0 deletions test/fixtures/templateLiterals/.i18nrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"fileName": "test/fixtures/templateLiterals/messages.pot",
"baseDirectory": "test"
}
9 changes: 9 additions & 0 deletions test/fixtures/templateLiterals/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
i18n(`Hello World`)

i18n(`Hello World`, {context: `someContext`})

i18n(`Hello` + ` World` + ` ` + `concat`)

i18n(`Hello` + " World" + ` ` + 'concat' + ' ' + 'different quotes')

i18n(`Not in ${'the'} result`)
26 changes: 26 additions & 0 deletions test/specs/scripts/extract.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,32 @@ describe('extract', () => {
})
})

describe('template literals ', () => {
const templateLiteralsDir = `${fixtureDir}/templateLiterals`

afterEach(() => {
removeIfExists(`${templateLiteralsDir}/messages.pot`)
})

it('should be possible to pass template literals as arguments', () => {
expect(existsSync(`${templateLiteralsDir}/messages.pot`)).toBeFalsy()

callForDir(templateLiteralsDir)

expect(existsSync(`${templateLiteralsDir}/messages.pot`)).toBeDefined()

const messages = readFileSync(`${templateLiteralsDir}/messages.pot`).toString('utf-8')

expect(messages).toContain('msgid "Hello World"')
expect(messages).toContain('msgid "Hello World concat"')
expect(messages).toContain('msgid "Hello World concat different quotes"')
expect(messages).toContain([`#: fixtures/templateLiterals/index.js:1`, `msgid "Hello World"`].join('\n'))
expect(messages).toContain([`#: fixtures/templateLiterals/index.js:3`, `msgctxt "someContext"`, `msgid "Hello World"`].join('\n'))
expect(messages).toContain([`#: fixtures/templateLiterals/index.js:5`, `msgid "Hello World concat"`].join('\n'))
expect(messages).not.toContain('msgid "Hello World Not in the result"')
})
})

describe('replacements', () => {
const replacementsDir = `${fixtureDir}/replacements`

Expand Down