/** * Gets all of the i18n key/value pairs from the given template root. In other words, returns * everything that needs to be translated. * * @param templateRoot * @param i18nPrefix */ private static Map<String, String> getTemplateI18nValues(final Element templateRoot, final String i18nPrefix) { final TemplateVisitor visitor = new TemplateVisitor(i18nPrefix); DomVisit.visit(templateRoot, visitor); return visitor.getI18nValues(); }
/** * Records the translation key/value for an element. * * @param i18nKeyPrefix * @param element */ protected void visitElement(String i18nKeyPrefix, Element element) { String translationKey = i18nKeyPrefix + getOrGenerateTranslationKey(element); String translationValue = getTextContent(element); i18nValues.put(translationKey, translationValue); }
@Override public boolean visit(Element element) { // Developers can mark entire sections of the template as "do not translate" if ("dummy".equals(element.getAttribute("data-role"))) { return false; } // If the element either explicitly enables i18n (via an i18n key) or is a text-only // node, record it. if (hasAttribute(element, "data-i18n-key") || isTextOnly(element)) { visitElement(i18nPrefix, element); return false; } if (hasAttribute(element, "title")) { visitAttribute(i18nPrefix, element, "title"); } if (hasAttribute(element, "placeholder")) { visitAttribute(i18nPrefix, element, "placeholder"); } return true; }
protected String getElementKey(Element element) { String elementKey; if (hasAttribute(element, "data-field")) { elementKey = element.getAttribute("data-field"); } else if (hasAttribute(element, "id")) { elementKey = element.getAttribute("id"); } else if (hasAttribute(element, "name")) { elementKey = element.getAttribute("name"); } else { elementKey = getOrGenerateTranslationKey(element); } return elementKey; }
/** * Gets a translation key associated with the given element. If no key attribute exists in this * element, generate and assign one. * * @param element */ protected String getOrGenerateTranslationKey(Element element) { String translationKey = null; String currentText = getTextContent(element); if (hasAttribute(element, "data-i18n-key")) { translationKey = element.getAttribute("data-i18n-key"); } else { translationKey = currentText.replaceAll("[:\\s'\"]+", "_"); if (translationKey.length() > 128) { translationKey = translationKey.substring(0, 128) + translationKey.hashCode(); } element.setAttribute("data-i18n-key", translationKey); } return translationKey; }
/** * Returns true if the given element has some text and no element children. * * @param element */ public boolean isTextOnly(Element element) { NodeList childNodes = element.getChildNodes(); for (int idx = 0; idx < childNodes.getLength(); idx++) { Node item = childNodes.item(idx); // As soon as we hit an element, we can return false if (item.getNodeType() == Node.ELEMENT_NODE) { return false; } } String textContent = getTextContent(element); return (textContent != null) && (textContent.trim().length() > 0); }
/** * Records the translation key/value for an attribute. * * @param i18nKeyPrefix * @param element * @param attributeName */ protected void visitAttribute(String i18nKeyPrefix, Element element, String attributeName) { String elementKey = getElementKey(element); // If we couldn't figure out a key for this thing, then just bail. if (elementKey == null || elementKey.trim().length() == 0) { return; } String translationKey = i18nKeyPrefix + elementKey; translationKey += "-" + attributeName; String translationValue = element.getAttribute(attributeName); i18nValues.put(translationKey, translationValue); }
protected String getElementKey(Element element) { String elementKey; if (hasAttribute(element, "data-field")) { elementKey = element.getAttribute("data-field"); } else if (hasAttribute(element, "id")) { elementKey = element.getAttribute("id"); } else if (hasAttribute(element, "name")) { elementKey = element.getAttribute("name"); } else { elementKey = getOrGenerateTranslationKey(element); } return elementKey; }
/** * Gets a translation key associated with the given element. If no key attribute exists in this * element, generate and assign one. * * @param element */ protected String getOrGenerateTranslationKey(Element element) { String translationKey = null; String currentText = getTextContent(element); if (hasAttribute(element, "data-i18n-key")) { translationKey = element.getAttribute("data-i18n-key"); } else { translationKey = currentText.replaceAll("[:\\s'\"]+", "_"); if (translationKey.length() > 128) { translationKey = translationKey.substring(0, 128) + translationKey.hashCode(); } element.setAttribute("data-i18n-key", translationKey); } return translationKey; }
/** * Returns true if the given element has some text and no element children. * * @param element */ public boolean isTextOnly(Element element) { NodeList childNodes = element.getChildNodes(); for (int idx = 0; idx < childNodes.getLength(); idx++) { Node item = childNodes.item(idx); // As soon as we hit an element, we can return false if (item.getNodeType() == Node.ELEMENT_NODE) { return false; } } String textContent = getTextContent(element); return (textContent != null) && (textContent.trim().length() > 0); }
/** * Records the translation key/value for an attribute. * * @param i18nKeyPrefix * @param element * @param attributeName */ protected void visitAttribute(String i18nKeyPrefix, Element element, String attributeName) { String elementKey = getElementKey(element); // If we couldn't figure out a key for this thing, then just bail. if (elementKey == null || elementKey.trim().length() == 0) { return; } String translationKey = i18nKeyPrefix + elementKey; translationKey += "-" + attributeName; String translationValue = element.getAttribute(attributeName); i18nValues.put(translationKey, translationValue); }
@Override public boolean visit(Element element) { // Developers can mark entire sections of the template as "do not translate" if ("dummy".equals(element.getAttribute("data-role"))) { return false; } // If the element either explicitly enables i18n (via an i18n key) or is a text-only // node, record it. if (hasAttribute(element, "data-i18n-key") || isTextOnly(element)) { visitElement(i18nPrefix, element); return false; } if (hasAttribute(element, "title")) { visitAttribute(i18nPrefix, element, "title"); } if (hasAttribute(element, "placeholder")) { visitAttribute(i18nPrefix, element, "placeholder"); } return true; }
/** * Records the translation key/value for an element. * * @param i18nKeyPrefix * @param element */ protected void visitElement(String i18nKeyPrefix, Element element) { String translationKey = i18nKeyPrefix + getOrGenerateTranslationKey(element); String translationValue = getTextContent(element); i18nValues.put(translationKey, translationValue); }
/** * Gets all of the i18n key/value pairs from the given template root. In other words, returns * everything that needs to be translated. * * @param templateRoot * @param i18nPrefix */ private static Map<String, String> getTemplateI18nValues(final Element templateRoot, final String i18nPrefix) { final TemplateVisitor visitor = new TemplateVisitor(i18nPrefix); DomVisit.visit(templateRoot, visitor); return visitor.getI18nValues(); }