| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package com.google.javascript.jscomp; |
| 18 |
|
| 19 |
import com.google.common.collect.Lists; |
| 20 |
import com.google.javascript.jscomp.JsMessage.PlaceholderReference; |
| 21 |
import com.google.javascript.rhino.IR; |
| 22 |
import com.google.javascript.rhino.Node; |
| 23 |
import com.google.javascript.rhino.Token; |
| 24 |
|
| 25 |
import java.util.Collections; |
| 26 |
import java.util.List; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class ReplaceMessagesForChrome extends JsMessageVisitor { |
| 38 |
|
| 39 |
ReplaceMessagesForChrome(AbstractCompiler compiler, |
| 40 |
JsMessage.IdGenerator idGenerator, |
| 41 |
boolean checkDuplicatedMessages, JsMessage.Style style) { |
| 42 |
|
| 43 |
super(compiler, checkDuplicatedMessages, style, idGenerator); |
| 44 |
} |
| 45 |
|
| 46 |
private static Node getChromeI18nGetMessageNode(String messageId) { |
| 47 |
Node chromeI18n = IR.getprop(IR.name("chrome"), IR.string("i18n")); |
| 48 |
Node getMessage = IR.getprop(chromeI18n, IR.string("getMessage")); |
| 49 |
return IR.call(getMessage, IR.string(messageId)); |
| 50 |
} |
| 51 |
|
| 52 |
@Override |
| 53 |
protected void processJsMessage( |
| 54 |
JsMessage message, JsMessageDefinition definition) { |
| 55 |
try { |
| 56 |
Node msgNode = definition.getMessageNode(); |
| 57 |
Node newValue = getNewValueNode(msgNode, message); |
| 58 |
newValue.copyInformationFromForTree(msgNode); |
| 59 |
|
| 60 |
definition.getMessageParentNode().replaceChild(msgNode, newValue); |
| 61 |
compiler.reportCodeChange(); |
| 62 |
} catch (MalformedException e) { |
| 63 |
compiler.report(JSError.make(message.getSourceName(), e.getNode(), |
| 64 |
MESSAGE_TREE_MALFORMED, e.getMessage())); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
private Node getNewValueNode(Node origNode, JsMessage message) |
| 69 |
throws MalformedException { |
| 70 |
Node newValueNode = getChromeI18nGetMessageNode(message.getId()); |
| 71 |
|
| 72 |
if (!message.placeholders().isEmpty()) { |
| 73 |
Node placeholderValues = origNode.getLastChild(); |
| 74 |
checkNode(placeholderValues, Token.OBJECTLIT); |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
List<String> placeholderNames = Lists.newArrayList(); |
| 79 |
for (CharSequence cs : message.parts()) { |
| 80 |
if (cs instanceof PlaceholderReference) { |
| 81 |
String placeholderName = ((PlaceholderReference) cs).getName(); |
| 82 |
placeholderNames.add(placeholderName); |
| 83 |
} |
| 84 |
} |
| 85 |
Collections.sort(placeholderNames); |
| 86 |
|
| 87 |
Node placeholderValueArray = IR.arraylit(); |
| 88 |
for (String name : placeholderNames) { |
| 89 |
Node value = getPlaceholderValue(placeholderValues, name); |
| 90 |
if (value == null) { |
| 91 |
throw new MalformedException( |
| 92 |
"No value was provided for placeholder " + name, |
| 93 |
origNode); |
| 94 |
} |
| 95 |
placeholderValueArray.addChildToBack(value); |
| 96 |
} |
| 97 |
newValueNode.addChildToBack(placeholderValueArray); |
| 98 |
} |
| 99 |
|
| 100 |
newValueNode.copyInformationFromForTree(origNode); |
| 101 |
return newValueNode; |
| 102 |
} |
| 103 |
|
| 104 |
private Node getPlaceholderValue( |
| 105 |
Node placeholderValues, String placeholderName) { |
| 106 |
for (Node key : placeholderValues.children()) { |
| 107 |
if (key.getString().equals(placeholderName)) { |
| 108 |
return key.getFirstChild().cloneTree(); |
| 109 |
} |
| 110 |
} |
| 111 |
return null; |
| 112 |
} |
| 113 |
} |