| /** |
| * Copyright (c) 2011, 2016 - Loetz GmbH&Co.KG (69115 Heidelberg, Germany) |
| * |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Christophe Loetz (Loetz GmbH&Co.KG) - initial implementation |
| * |
| * |
| * This copyright notice shows up in the generated Java code |
| */ |
| package org.eclipse.osbp.xtext.i18n; |
| |
| import com.google.common.base.Objects; |
| import java.io.InputStream; |
| import java.util.ArrayList; |
| import java.util.HashMap; |
| import java.util.Iterator; |
| import java.util.List; |
| import java.util.Locale; |
| import java.util.Map; |
| import java.util.Properties; |
| import java.util.Set; |
| import java.util.TreeMap; |
| import org.apache.commons.lang3.StringEscapeUtils; |
| import org.eclipse.emf.ecore.EObject; |
| import org.eclipse.emf.ecore.resource.Resource; |
| import org.eclipse.osbp.preferences.ProductConfiguration; |
| import org.eclipse.osbp.xtext.addons.AdvancedJvmModelGenerator; |
| import org.eclipse.osbp.xtext.addons.EObjectHelper; |
| import org.eclipse.osbp.xtext.i18n.DSLOutputConfigurationProvider; |
| import org.eclipse.osbp.xtext.i18n.I18NKeyGenerator; |
| import org.eclipse.osbp.xtext.i18n.I18nUtil; |
| import org.eclipse.osbp.xtext.i18n.StringComparator; |
| import org.eclipse.xtext.AbstractRule; |
| import org.eclipse.xtext.RuleCall; |
| import org.eclipse.xtext.generator.AbstractFileSystemAccess2; |
| import org.eclipse.xtext.generator.IFileSystemAccess; |
| import org.eclipse.xtext.nodemodel.BidiTreeIterable; |
| import org.eclipse.xtext.nodemodel.BidiTreeIterator; |
| import org.eclipse.xtext.nodemodel.ICompositeNode; |
| import org.eclipse.xtext.nodemodel.ILeafNode; |
| import org.eclipse.xtext.nodemodel.INode; |
| import org.eclipse.xtext.nodemodel.util.NodeModelUtils; |
| import org.eclipse.xtext.xbase.lib.CollectionLiterals; |
| import org.eclipse.xtext.xbase.lib.Conversions; |
| import org.eclipse.xtext.xbase.lib.Exceptions; |
| import org.eclipse.xtext.xbase.lib.IterableExtensions; |
| import org.eclipse.xtext.xbase.lib.StringExtensions; |
| |
| @SuppressWarnings("all") |
| public class I18NModelGenerator extends AdvancedJvmModelGenerator { |
| protected TreeMap<String, String> properties = CollectionLiterals.<String, String>newTreeMap(new StringComparator()); |
| |
| protected HashMap<String, String> addedTranslatables = CollectionLiterals.<String, String>newHashMap(); |
| |
| public void addTranslatables(final String additionals) { |
| this.addedTranslatables.clear(); |
| String[] pairs = additionals.split(","); |
| for (final String pair : pairs) { |
| boolean _contains = pair.contains("->"); |
| if (_contains) { |
| String[] parts = pair.split("->"); |
| final String[] _converted_parts = (String[])parts; |
| Iterator<String> iter = ((List<String>)Conversions.doWrapArray(_converted_parts)).iterator(); |
| String _next = iter.next(); |
| String _next_1 = iter.next(); |
| this.addedTranslatables.put(_next, _next_1); |
| } else { |
| this.addedTranslatables.put(pair, null); |
| } |
| } |
| } |
| |
| public void getTranslatables(final Resource resource, final TreeMap<String, String> translatables) { |
| translatables.clear(); |
| Set<String> _keySet = this.addedTranslatables.keySet(); |
| for (final String additional : _keySet) { |
| boolean _contains = additional.contains(":"); |
| if (_contains) { |
| String[] complex = additional.split(":"); |
| String _get = complex[1]; |
| String _key = I18NKeyGenerator.key(_get); |
| String _get_1 = complex[1]; |
| String _value = I18NKeyGenerator.value(_get_1); |
| translatables.put(_key, _value); |
| } else { |
| String _key_1 = I18NKeyGenerator.key(additional); |
| String _value_1 = I18NKeyGenerator.value(additional); |
| translatables.put(_key_1, _value_1); |
| } |
| } |
| EObject eobject = EObjectHelper.getSemanticElement(resource); |
| ICompositeNode node = NodeModelUtils.getNode(eobject); |
| BidiTreeIterable<INode> _asTreeIterable = node.getAsTreeIterable(); |
| BidiTreeIterator<INode> contentsIterator = _asTreeIterable.iterator(); |
| while (contentsIterator.hasNext()) { |
| { |
| INode abstractNode = contentsIterator.next(); |
| EObject _grammarElement = abstractNode.getGrammarElement(); |
| if ((_grammarElement instanceof RuleCall)) { |
| EObject _grammarElement_1 = abstractNode.getGrammarElement(); |
| RuleCall rule = ((RuleCall) _grammarElement_1); |
| AbstractRule _rule = rule.getRule(); |
| String _name = _rule.getName(); |
| boolean _startsWith = _name.startsWith(I18nUtil.TRANSLATABLE); |
| if (_startsWith) { |
| boolean found = false; |
| while ((contentsIterator.hasNext() && (!found))) { |
| { |
| INode _next = contentsIterator.next(); |
| abstractNode = _next; |
| EObject _grammarElement_2 = abstractNode.getGrammarElement(); |
| if ((_grammarElement_2 instanceof RuleCall)) { |
| StringBuffer text = new StringBuffer(); |
| Iterable<ILeafNode> _leafNodes = abstractNode.getLeafNodes(); |
| for (final ILeafNode leaf : _leafNodes) { |
| String _text = leaf.getText(); |
| String _trim = _text.trim(); |
| text.append(_trim); |
| } |
| boolean _or = false; |
| String _string = text.toString(); |
| boolean _contains_1 = _string.contains("extends"); |
| if (_contains_1) { |
| _or = true; |
| } else { |
| String _string_1 = text.toString(); |
| boolean _contains_2 = _string_1.contains("mapped superclass"); |
| _or = _contains_2; |
| } |
| if (_or) { |
| found = true; |
| } else { |
| boolean _and = false; |
| String _string_2 = text.toString(); |
| int _length = _string_2.length(); |
| boolean _greaterThan = (_length > 0); |
| if (!_greaterThan) { |
| _and = false; |
| } else { |
| String _string_3 = text.toString(); |
| boolean _equals = _string_3.equals("\"\""); |
| boolean _not = (!_equals); |
| _and = _not; |
| } |
| if (_and) { |
| String _string_4 = text.toString(); |
| String _key_2 = I18NKeyGenerator.key(_string_4); |
| String _string_5 = text.toString(); |
| String _value_2 = I18NKeyGenerator.value(_string_5); |
| translatables.put(_key_2, _value_2); |
| } |
| } |
| found = true; |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| public void generateI18n(final IFileSystemAccess fsa, final Resource input) { |
| boolean _willLanguagesAutocreate = ProductConfiguration.willLanguagesAutocreate(); |
| if (_willLanguagesAutocreate) { |
| if ((fsa instanceof AbstractFileSystemAccess2)) { |
| AbstractFileSystemAccess2 erfsa = ((AbstractFileSystemAccess2) fsa); |
| HashMap<String, TreeMap<String, String>> translations = CollectionLiterals.<String, TreeMap<String, String>>newHashMap(); |
| Map<String, Locale> supportedLanguages = ProductConfiguration.getLanguages(); |
| this.properties.clear(); |
| this.getTranslatables(input, this.properties); |
| Set<String> _keySet = supportedLanguages.keySet(); |
| for (final String localeId : _keySet) { |
| { |
| final Properties prop = new Properties(); |
| try { |
| boolean _equals = localeId.equals("default"); |
| if (_equals) { |
| InputStream _readBinaryFile = erfsa.readBinaryFile("I18N.properties", |
| DSLOutputConfigurationProvider.DEFAULT_OUTPUT_I18N); |
| prop.load(_readBinaryFile); |
| } else { |
| InputStream _readBinaryFile_1 = erfsa.readBinaryFile((("I18N_" + localeId) + ".properties"), |
| DSLOutputConfigurationProvider.DEFAULT_OUTPUT_I18N); |
| prop.load(_readBinaryFile_1); |
| } |
| } catch (final Throwable _t) { |
| if (_t instanceof Exception) { |
| final Exception e = (Exception)_t; |
| e.getStackTrace(); |
| } else { |
| throw Exceptions.sneakyThrow(_t); |
| } |
| } |
| StringComparator _stringComparator = new StringComparator(); |
| TreeMap<String, String> p = CollectionLiterals.<String, String>newTreeMap(_stringComparator); |
| translations.put(localeId, p); |
| Set<String> _stringPropertyNames = prop.stringPropertyNames(); |
| for (final String props : _stringPropertyNames) { |
| String _property = prop.getProperty(props); |
| int _length = _property.length(); |
| boolean _greaterThan = (_length > 0); |
| if (_greaterThan) { |
| String _property_1 = prop.getProperty(props); |
| p.put(props, _property_1); |
| } |
| } |
| boolean _equals_1 = localeId.equals("default"); |
| if (_equals_1) { |
| Set<String> _keySet_1 = this.properties.keySet(); |
| for (final String props_1 : _keySet_1) { |
| TreeMap<String, String> _get = translations.get("default"); |
| String _get_1 = this.properties.get(props_1); |
| _get.put(props_1, _get_1); |
| } |
| } |
| } |
| } |
| Set<String> _keySet_1 = supportedLanguages.keySet(); |
| for (final String localeId_1 : _keySet_1) { |
| boolean _equals = localeId_1.equals("default"); |
| boolean _not = (!_equals); |
| if (_not) { |
| TreeMap<String, String> _get = translations.get("default"); |
| Set<String> _keySet_2 = _get.keySet(); |
| for (final String key : _keySet_2) { |
| boolean _findTranslation = this.findTranslation(localeId_1, key, translations); |
| boolean _not_1 = (!_findTranslation); |
| if (_not_1) { |
| TreeMap<String, String> _get_1 = translations.get("default"); |
| String value = _get_1.get(key); |
| ArrayList<String> payLoads = this.improveForTranslation(value); |
| boolean _equals_1 = localeId_1.equals("en"); |
| if (_equals_1) { |
| TreeMap<String, String> _get_2 = translations.get(localeId_1); |
| String _join = IterableExtensions.join(payLoads); |
| _get_2.put(key, _join); |
| } else { |
| boolean _isLanguagesAutotranslate = ProductConfiguration.isLanguagesAutotranslate(); |
| if (_isLanguagesAutotranslate) { |
| String property = ""; |
| for (final String payLoad : payLoads) { |
| char _charAt = payLoad.charAt(0); |
| boolean _isLetter = Character.isLetter(_charAt); |
| if (_isLetter) { |
| String _escapeJava = StringEscapeUtils.escapeJava(payLoad); |
| String _plus = (property + _escapeJava); |
| property = _plus; |
| } else { |
| property = (property + payLoad); |
| } |
| } |
| TreeMap<String, String> _get_3 = translations.get(localeId_1); |
| _get_3.put(key, property); |
| } else { |
| boolean _isLanguagesAutotranslate_1 = ProductConfiguration.isLanguagesAutotranslate(); |
| boolean _not_2 = (!_isLanguagesAutotranslate_1); |
| if (_not_2) { |
| boolean _equals_2 = localeId_1.equals("de"); |
| if (_equals_2) { |
| TreeMap<String, String> _get_4 = translations.get(localeId_1); |
| String _join_1 = IterableExtensions.join(payLoads); |
| _get_4.put(key, _join_1); |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| } |
| Set<String> _keySet_3 = supportedLanguages.keySet(); |
| for (final String localeId_2 : _keySet_3) { |
| { |
| StringBuilder output = new StringBuilder(); |
| output.append((("#" + localeId_2) + "\n")); |
| TreeMap<String, String> _get_5 = translations.get(localeId_2); |
| Set<String> _keySet_4 = _get_5.keySet(); |
| for (final String key_1 : _keySet_4) { |
| { |
| TreeMap<String, String> _get_6 = translations.get(localeId_2); |
| String value_1 = _get_6.get(key_1); |
| String _escapeJava_1 = StringEscapeUtils.escapeJava(value_1); |
| value_1 = _escapeJava_1; |
| StringBuilder _append = output.append(((key_1 + "=") + value_1)); |
| _append.append("\n"); |
| } |
| } |
| boolean _equals_3 = localeId_2.equals("default"); |
| if (_equals_3) { |
| erfsa.generateFile("I18N.properties", DSLOutputConfigurationProvider.DEFAULT_OUTPUT_I18N, output); |
| } else { |
| erfsa.generateFile((("I18N_" + localeId_2) + ".properties"), |
| DSLOutputConfigurationProvider.DEFAULT_OUTPUT_I18N, output); |
| } |
| } |
| } |
| } |
| } |
| } |
| |
| public boolean findTranslation(final String localeId, final String key, final HashMap<String, TreeMap<String, String>> translations) { |
| String[] splitter = localeId.split("_"); |
| int _length = splitter.length; |
| boolean _greaterThan = (_length > 1); |
| if (_greaterThan) { |
| ArrayList<String> part = CollectionLiterals.<String>newArrayList(splitter); |
| int _size = part.size(); |
| int _minus = (_size - 1); |
| part.remove(_minus); |
| String _join = IterableExtensions.join(part, "_"); |
| boolean _findTranslation = this.findTranslation(_join, key, translations); |
| if (_findTranslation) { |
| return true; |
| } |
| } |
| boolean _and = false; |
| boolean _containsKey = translations.containsKey(localeId); |
| if (!_containsKey) { |
| _and = false; |
| } else { |
| TreeMap<String, String> _get = translations.get(localeId); |
| boolean _containsKey_1 = _get.containsKey(key); |
| _and = _containsKey_1; |
| } |
| if (_and) { |
| return true; |
| } |
| return false; |
| } |
| |
| public ArrayList<String> splitTextAndNumbers(final String text) { |
| ArrayList<String> array = CollectionLiterals.<String>newArrayList(); |
| boolean _or = false; |
| boolean _equals = Objects.equal(text, null); |
| if (_equals) { |
| _or = true; |
| } else { |
| int _length = text.length(); |
| boolean _equals_1 = (_length == 0); |
| _or = _equals_1; |
| } |
| if (_or) { |
| return array; |
| } |
| String current = ""; |
| int len = text.length(); |
| int i = 1; |
| char lastChar = text.charAt(0); |
| current = (current + Character.valueOf(lastChar)); |
| while ((i < len)) { |
| { |
| boolean _or_1 = false; |
| boolean _and = false; |
| char _charAt = text.charAt(i); |
| boolean _isLetter = Character.isLetter(_charAt); |
| if (!_isLetter) { |
| _and = false; |
| } else { |
| boolean _isDigit = Character.isDigit(lastChar); |
| _and = _isDigit; |
| } |
| if (_and) { |
| _or_1 = true; |
| } else { |
| boolean _and_1 = false; |
| char _charAt_1 = text.charAt(i); |
| boolean _isDigit_1 = Character.isDigit(_charAt_1); |
| if (!_isDigit_1) { |
| _and_1 = false; |
| } else { |
| boolean _isLetter_1 = Character.isLetter(lastChar); |
| _and_1 = _isLetter_1; |
| } |
| _or_1 = _and_1; |
| } |
| if (_or_1) { |
| array.add(current); |
| current = ""; |
| } |
| char _charAt_2 = text.charAt(i); |
| lastChar = _charAt_2; |
| current = (current + Character.valueOf(lastChar)); |
| i = (i + 1); |
| } |
| } |
| int _length_1 = current.length(); |
| boolean _greaterThan = (_length_1 > 0); |
| if (_greaterThan) { |
| array.add(current); |
| } |
| return array; |
| } |
| |
| public ArrayList<String> improveForTranslation(final String key) { |
| String payLoad = key.replace("_", " "); |
| ArrayList<String> newArray = CollectionLiterals.<String>newArrayList(); |
| ArrayList<String> array = this.splitTextAndNumbers(payLoad); |
| for (final String element : array) { |
| char _charAt = element.charAt(0); |
| boolean _isLetter = Character.isLetter(_charAt); |
| if (_isLetter) { |
| String _deAbbreviateTranslation = this.deAbbreviateTranslation(element); |
| newArray.add(_deAbbreviateTranslation); |
| } else { |
| newArray.add(element); |
| } |
| } |
| int _size = newArray.size(); |
| boolean _greaterThan = (_size > 0); |
| if (_greaterThan) { |
| String _get = newArray.get(0); |
| String _firstUpper = StringExtensions.toFirstUpper(_get); |
| newArray.set(0, _firstUpper); |
| } |
| return newArray; |
| } |
| |
| public String deAbbreviateTranslation(final String text) { |
| String payLoad = text; |
| String _replaceAll = payLoad.replaceAll("(^|\\s)abv(\\s|$)", " alcohol by volume "); |
| payLoad = _replaceAll; |
| String _replaceAll_1 = payLoad.replaceAll("(^|\\s)abw(\\s|$)", " alcohol by weight "); |
| payLoad = _replaceAll_1; |
| String _replaceAll_2 = payLoad.replaceAll("(^|\\s)accnt(\\s|$)", " account "); |
| payLoad = _replaceAll_2; |
| String _replaceAll_3 = payLoad.replaceAll("(^|\\s)addr(\\s|$)", " address "); |
| payLoad = _replaceAll_3; |
| String _replaceAll_4 = payLoad.replaceAll("(^|\\s)base id(\\s|$)", " identifier "); |
| payLoad = _replaceAll_4; |
| String _replaceAll_5 = payLoad.replaceAll("(^|\\s)base uuid(\\s|$)", " identifier "); |
| payLoad = _replaceAll_5; |
| String _replaceAll_6 = payLoad.replaceAll("(^|\\s)bsin(\\s|$)", " brand single identification number "); |
| payLoad = _replaceAll_6; |
| String _replaceAll_7 = payLoad.replaceAll("(^|\\s)cal(\\s|$)", " calories "); |
| payLoad = _replaceAll_7; |
| String _replaceAll_8 = payLoad.replaceAll("(^|\\s)carb(\\s|$)", " carbohydrate "); |
| payLoad = _replaceAll_8; |
| String _replaceAll_9 = payLoad.replaceAll("(^|\\s)chol(\\s|$)", " cholesterol "); |
| payLoad = _replaceAll_9; |
| String _replaceAll_10 = payLoad.replaceAll("(^|\\s)cd(\\s|$)", " code "); |
| payLoad = _replaceAll_10; |
| String _replaceAll_11 = payLoad.replaceAll("(^|\\s)desc(\\s|$)", " description "); |
| payLoad = _replaceAll_11; |
| String _replaceAll_12 = payLoad.replaceAll("(^|\\s)diet(\\s|$)", " dietary "); |
| payLoad = _replaceAll_12; |
| String _replaceAll_13 = payLoad.replaceAll("(^|\\s)dv(\\s|$)", " daily volume "); |
| payLoad = _replaceAll_13; |
| String _replaceAll_14 = payLoad.replaceAll("(^|\\s)exp(\\s|$)", " expiring "); |
| payLoad = _replaceAll_14; |
| String _replaceAll_15 = payLoad.replaceAll("(^|\\s)hdr(\\s|$)", " header "); |
| payLoad = _replaceAll_15; |
| String _replaceAll_16 = payLoad.replaceAll("(^|\\s)hier(\\s|$)", " hierarchy "); |
| payLoad = _replaceAll_16; |
| String _replaceAll_17 = payLoad.replaceAll("(^|\\s)id(\\s|$)", " identifier "); |
| payLoad = _replaceAll_17; |
| String _replaceAll_18 = payLoad.replaceAll("(^|\\s)img(\\s|$)", " image "); |
| payLoad = _replaceAll_18; |
| String _replaceAll_19 = payLoad.replaceAll("(^|\\s)lang(\\s|$)", " language "); |
| payLoad = _replaceAll_19; |
| String _replaceAll_20 = payLoad.replaceAll("(^|\\s)nm(\\s|$)", " name "); |
| payLoad = _replaceAll_20; |
| String _replaceAll_21 = payLoad.replaceAll("(^|\\s)num(\\s|$)", " number "); |
| payLoad = _replaceAll_21; |
| String _replaceAll_22 = payLoad.replaceAll("(^|\\s)pkg(\\s|$)", " package "); |
| payLoad = _replaceAll_22; |
| String _replaceAll_23 = payLoad.replaceAll("(^|\\s)ref(\\s|$)", " reference "); |
| payLoad = _replaceAll_23; |
| String _replaceAll_24 = payLoad.replaceAll("(^|\\s)sat(\\s|$)", " saturated "); |
| payLoad = _replaceAll_24; |
| String _replaceAll_25 = payLoad.replaceAll("(^|\\s)serv(\\s|$)", " serving "); |
| payLoad = _replaceAll_25; |
| String _replaceAll_26 = payLoad.replaceAll("(^|\\s)sod(\\s|$)", " sodium "); |
| payLoad = _replaceAll_26; |
| String _replaceAll_27 = payLoad.replaceAll("(^|\\s)sku(\\s|$)", " stock-keeping unit "); |
| payLoad = _replaceAll_27; |
| String _replaceAll_28 = payLoad.replaceAll("(^|\\s)srp(\\s|$)", " suggested retail price "); |
| payLoad = _replaceAll_28; |
| String _replaceAll_29 = payLoad.replaceAll("(^|\\s)tel(\\s|$)", " telephone "); |
| payLoad = _replaceAll_29; |
| String _replaceAll_30 = payLoad.replaceAll("(^|\\s)tot(\\s|$)", " total "); |
| payLoad = _replaceAll_30; |
| String _trim = payLoad.trim(); |
| payLoad = _trim; |
| return payLoad; |
| } |
| |
| public void doGenerate(final Resource input, final IFileSystemAccess fsa) { |
| this.generateI18n(fsa, input); |
| super.doGenerate(input, fsa); |
| } |
| } |