1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 | import {isElementMarker, isFunctionMarker} from './ast';
|
9 | import {
|
10 | extractFunctionMessage,
|
11 | extractElementMessageWithoutSideEffects,
|
12 | } from './extract';
|
13 | import io from './io';
|
14 | import parsing from './parsing';
|
15 | import translatedRendererFor from './translation';
|
16 |
|
17 |
|
18 |
|
19 | export default function translateMessagesToBundle(src, translationsSrc, {inputFormat = 'po', ...options} = {}) {
|
20 | const bundle = {};
|
21 | const missing = {};
|
22 | const translations = io[inputFormat].in(translationsSrc, options);
|
23 |
|
24 | function attemptToCreateRenderer(node, message) {
|
25 | if (translations[message]) {
|
26 | bundle[message] = translatedRendererFor(
|
27 | node,
|
28 | translations[message],
|
29 | message
|
30 | );
|
31 | } else {
|
32 | missing[message] = message;
|
33 | }
|
34 | }
|
35 |
|
36 | const plugin = function() {
|
37 | return {
|
38 | visitor: {
|
39 | CallExpression({node}) {
|
40 | if (isFunctionMarker(node)) {
|
41 | const message = extractFunctionMessage(node);
|
42 | attemptToCreateRenderer(node, message);
|
43 | }
|
44 | },
|
45 |
|
46 | JSXElement({node}) {
|
47 | if (isElementMarker(node)) {
|
48 | const message = extractElementMessageWithoutSideEffects(node);
|
49 | attemptToCreateRenderer(node, message);
|
50 | }
|
51 | }
|
52 | }
|
53 | };
|
54 | };
|
55 |
|
56 | parsing.transform(src, [plugin]);
|
57 |
|
58 | return {bundle, missing};
|
59 | };
|