Skip to content
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
3 changes: 2 additions & 1 deletion .babelrc
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
}
}
],
"@babel/preset-flow"
"@babel/preset-flow",
"@babel/preset-react",
]
}
11 changes: 10 additions & 1 deletion example/src/messages.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,13 @@
import { defineMessages } from 'react-intl'
// eslint-disable-next-line import/no-extraneous-dependencies
import { defineMessages, injectIntl } from 'react-intl'

export const SubmitButton = injectIntl(({ intl }) => {
const label = intl.formatMessage({
id: 'a.submit',
defaultMessage: 'Submit Button'
})
return <button aria-label={label}>{label}</button>
})

export default defineMessages({
hello: {
Expand Down
26 changes: 13 additions & 13 deletions extract-react-intl/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const getBabelrc = cwd => {
const getBabelrcDir = cwd => path.dirname(readBabelrcUp.sync({ cwd }).path)

// eslint-disable-next-line max-lines-per-function
module.exports = async (locales, pattern, opts) => {
module.exports = async (locales, pattern, opts = {}) => {
if (!Array.isArray(locales)) {
throw new TypeError(`Expected a Array, got ${typeof locales}`)
}
Expand All @@ -53,23 +53,22 @@ module.exports = async (locales, pattern, opts) => {
throw new TypeError(`Expected a string, got ${typeof pattern}`)
}

opts = {
cwd: process.cwd(),
defaultLocale: 'en',
...opts
}
const defaultLocale = opts.defaultLocale || 'en'
const cwd = opts.cwd || process.cwd()

const babelrc = getBabelrc(cwd) || {}
const babelrcDir = getBabelrcDir(cwd)

const babelrc = getBabelrc(opts.cwd) || {}
const babelrcDir = getBabelrcDir(opts.cwd)
delete opts.cwd
delete opts.defaultLocale

const { moduleSourceName } = opts
const pluginOptions = moduleSourceName ? { moduleSourceName } : {}
const pluginOptions = opts

const { presets = [], plugins = [] } = babelrc

presets.unshift({
// eslint-disable-next-line global-require
plugins: [[require('babel-plugin-react-intl').default, pluginOptions]]
plugins: [[require('babel-plugin-react-intl'), pluginOptions]]
})

const extractFromFile = async file => {
Expand All @@ -79,10 +78,11 @@ module.exports = async (locales, pattern, opts) => {
}
const { metadata: result } = await pify(transformFile)(file, babelOpts)
const localeObj = localeMap(locales)
// eslint-disable-next-line no-unused-vars
for (const { id, defaultMessage } of result['react-intl'].messages) {
// eslint-disable-next-line no-unused-vars
for (const locale of locales) {
localeObj[locale][id] =
opts.defaultLocale === locale ? defaultMessage : ''
localeObj[locale][id] = defaultLocale === locale ? defaultMessage : ''
}
}
return localeObj
Expand Down
2 changes: 2 additions & 0 deletions extract-react-intl/test/__snapshots__/test.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,13 @@ Object {
exports[`extract from file 1`] = `
Object {
"en": Object {
"components.App.1248161314": "Submit button",
"components.App.hello": "hello",
"components.App.world": "world",
"components/Greeting/welcome": "Welcome {name}, you have received {unreadCount, plural, =0 {no new messages} one {{formattedUnreadCount} new message} other {{formattedUnreadCount} new messages}} since {formattedLastLoginTime}.",
},
"ja": Object {
"components.App.1248161314": "",
"components.App.hello": "",
"components.App.world": "",
"components/Greeting/welcome": "",
Expand Down
9 changes: 8 additions & 1 deletion extract-react-intl/test/fixtures/components/App/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
// @flow
import React, { Component } from 'react'
import { FormattedMessage } from 'react-intl'
import { FormattedMessage, injectIntl } from 'react-intl'
import Greeting from '../Greeting'
import messages from './messages'

injectIntl(({ intl }) => {
const label = intl.formatMessage({ defaultMessage: "Submit button" })

return <button aria-label={label}>{label}</button>
});


export default class App extends Component {
render() {
const user = {
Expand Down
8 changes: 6 additions & 2 deletions extract-react-intl/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ const locales = ['en', 'ja']

test('extract from file', async () => {
process.env.BABEL_ENV = 'react-intl'
const x = await m(locales, pattern, { cwd: `${__dirname}/fixtures` })
const x = await m(locales, pattern, {
cwd: `${__dirname}/fixtures`,
extractFromFormatMessageCall: true
})
expect(x).toMatchSnapshot()
})

test.only('babelrc path resolution', async () => {
// TODO: fix
test.skip('babelrc path resolution', async () => {
const x = await m(['en'], './extract-react-intl/test/resolution/**/*.js', {
cwd: `${__dirname}/resolution`
})
Expand Down
5 changes: 4 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-unused-vars */
'use strict'
const path = require('path')
const fs = require('fs')
Expand Down Expand Up @@ -87,7 +88,9 @@ module.exports = async (locales, pattern, buildDir, opts) => {

const oldLocaleMaps = loadLocaleFiles(locales, buildDir, ext, delimiter)

const extractorOptions = { defaultLocale }
delete opts.defaultLocale

const extractorOptions = { defaultLocale, ...opts }

if (moduleName) {
extractorOptions.moduleSourceName = moduleName
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"add-contributor": "all-contributors add",
"fmt": "prettier --write '**/*.{json,js,md}'",
"example": "./cli.js -l=en,ja -o example/i18n -d en 'example/**/*.js'",
"lint": "eslint index.js cli.js",
"lint": "eslint index.js cli.js ./extract-react-intl/index.js",
"test": "npm run lint && jest"
},
"bin": {
Expand All @@ -39,7 +39,7 @@
],
"dependencies": {
"@babel/core": "^7.5.5",
"babel-plugin-react-intl": "3.0.1",
"babel-plugin-react-intl": "3.5.1",
"extract-react-intl": "^0.9.0",
"flat": "^4.1.0",
"glob": "^7.1.4",
Expand All @@ -63,15 +63,15 @@
"all-contributors-cli": "^6.8.1",
"babel-core": "7.0.0-bridge.0",
"babel-jest": "^24.8.0",
"babel-plugin-react-intl-auto": "^2.0.0",
"babel-plugin-react-intl-auto": "^2.1.0",
"eslint": "^6.1.0",
"eslint-config-precure": "^4.18.1",
"husky": "^3.0.3",
"jest": "^24.8.0",
"lint-staged": "^9.2.1",
"prettier": "^1.18.2",
"react": "^16.8.6",
"react-intl": "^2.9.0",
"react-intl": "^3.1.10",
"temp-write": "^4.0.0",
"tempy": "^0.3.0"
},
Expand Down
5 changes: 5 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ $ extract-messages --help
$ extract-messages --locales=ja,en --output app/translations 'app/**/*.js'
$ extract-messages -l=ja,en -o i18n 'src/**/*.js'
$ extract-messages -l=ja,en -o app/translations -f yaml 'app/**/messages.js'
$ extract-messages -l=ja,en -o i18n 'src/**/*.js' --extractFromFormatMessageCall
```

### create-react-app user
Expand Down Expand Up @@ -178,6 +179,10 @@ Default: `react-intl`

Set from where _defineMessages_, `<FormatterMessage />` and `<FormattedHTML />` are imported.

##### babel-plugin-react-intl's Options

See https://github.com/formatjs/formatjs/tree/master/packages/babel-plugin-react-intl#options

## Contributors

Thanks goes to these wonderful people ([emoji key](https://github.com/kentcdodds/all-contributors#emoji-key)):
Expand Down
Loading