Commit d32ef2ed authored by Alexander Basov's avatar Alexander Basov Committed by Matija Obreza
Browse files

implement i18n inGeoRegion

parent 7cb1fdd2
......@@ -98,6 +98,10 @@ public class GeoRegion extends VersionedModel {
}
}
public String getName(Locale locale) {
return getNameLocal(locale);
}
private synchronized String getNameLocal(Locale locale) {
if (this.nameJ == null && this.nameL != null) {
final ObjectMapper mapper = new ObjectMapper();
......
package org.genesys2.server.service.worker;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.io.IOUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
......@@ -26,147 +28,173 @@ import java.util.zip.ZipFile;
@Component
public class GeoRegionDataCLDR {
private static final String CLDR_URL = "http://unicode.org/Public/cldr/26.0.1/core.zip";
public static final Log LOG = LogFactory.getLog(GeoRegionDataCLDR.class);
@Resource
private Set<String> supportedLocales;
public List<GeoRegion> getGeoRigionDataCLDR() throws IOException, ParserConfigurationException, SAXException {
ZipFile zipFile = getCoreZip();
LOG.info("Obtained CLDR zip file: " + zipFile);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
SAXParser parser = saxParserFactory.newSAXParser();
ParserForCLDR parserForCLDR = new ParserForCLDR();
ZipEntry zipEntry;
List<GeoRegion> geoRegionList = new ArrayList<>();
InputStream inputStream;
for (String language : supportedLocales) {
String fileName = "common/main/" + language + ".xml";
LOG.info("Examining " + fileName);
zipEntry = zipFile.getEntry(fileName);
inputStream = zipFile.getInputStream(zipEntry);
parser.parse(inputStream, parserForCLDR);
List<GeoRegion> geoRegionsWithAssigndParents = assignParents(parserForCLDR.getRegionList());
geoRegionList.addAll(geoRegionsWithAssigndParents);
inputStream.close();
}
return geoRegionList;
}
private List<GeoRegion> assignParents(Map<String, GeoRegion> geoRegions) {
String world = "001";
String continents = "002,019,142,150,009";
String forAfrica = "015,014,017,018,011";
String forAsia = "143,030,034,035,145";
String forEurope = "151,154,039,155";
String forOceania = "053,054,057,061";
String forAmerica = "419,003,021";
String forLatinAmerica = "029,013,005";
for (String isoCode : continents.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(world));
}
for (String isoCode : forAfrica.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[0]));
}
for (String isoCode : forAmerica.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[1]));
}
for (String isoCode : forAsia.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[2]));
}
for (String isoCode : forEurope.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[3]));
}
for (String isoCode : forOceania.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[4]));
}
for (String isoCode : forLatinAmerica.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(forAmerica.split(",")[0]));
}
return new ArrayList<>(geoRegions.values());
}
private ZipFile getCoreZip() throws IOException {
URL url = new URL(CLDR_URL);
LOG.info("Downloading " + url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
File tempFileForXml = File.createTempFile("core", ".zip");
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(tempFileForXml);
IOUtils.copy(in, out);
out.close();
LOG.info("Done copying CLDR data to file");
ZipFile zipFile = new ZipFile(tempFileForXml);
return zipFile;
}
public class ParserForCLDR extends DefaultHandler {
private GeoRegion geoRegion;
private Map<String, GeoRegion> regionMap;
// needs for avoid duplicate geoRegions in regionList
private List<String> isoCodeList;
private String xmlTag = "";
private Attributes attributes;
public Map<String, GeoRegion> getRegionList() {
return regionMap;
}
@Override
public void startDocument() throws SAXException {
regionMap = new HashMap<>();
isoCodeList = new ArrayList<>();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
geoRegion = new GeoRegion();
this.xmlTag = qName;
this.attributes = attributes;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String attribute = attributes.getValue("type");
if (this.xmlTag.equalsIgnoreCase("territory") && attribute.matches("[0-9]+") && !isoCodeList.contains(attribute)) {
geoRegion.setIsoCode(attribute);
geoRegion.setName(new String(ch, start, length));
isoCodeList.add(attribute);
regionMap.put(geoRegion.getIsoCode(), geoRegion);
}
}
}
private static final String CLDR_URL = "http://unicode.org/Public/cldr/26.0.1/core.zip";
public static final Log LOG = LogFactory.getLog(GeoRegionDataCLDR.class);
@Resource
private Set<String> supportedLocales;
public List<GeoRegion> getGeoRigionDataCLDR() throws IOException, ParserConfigurationException, SAXException {
ZipFile zipFile = getCoreZip();
LOG.info("Obtained CLDR zip file: " + zipFile);
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
SAXParser parser = saxParserFactory.newSAXParser();
ParserForCLDR parserForCLDR = new ParserForCLDR();
ZipEntry zipEntry;
List<GeoRegion> geoRegionList = null;
Map<String, Map<String, GeoRegion>> forNameJGeoRegions = new HashMap<>();
InputStream inputStream;
for (String language : supportedLocales) {
String fileName = "common/main/" + language + ".xml";
LOG.info("Examining " + fileName);
zipEntry = zipFile.getEntry(fileName);
inputStream = zipFile.getInputStream(zipEntry);
parser.parse(inputStream, parserForCLDR);
if (language.equals("en")) {
geoRegionList = assignParents(parserForCLDR.getRegionMap());
} else {
forNameJGeoRegions.put(language, parserForCLDR.getRegionMap());
}
inputStream.close();
}
assignNameJ(geoRegionList, forNameJGeoRegions);
return geoRegionList;
}
private void assignNameJ(List<GeoRegion> geoRegionList, Map<String, Map<String, GeoRegion>> forNameJGeoRegions) throws JsonProcessingException {
for(GeoRegion geoRegion : geoRegionList){
ObjectMapper objectMapper = new ObjectMapper();
Map<String, String> nameL = new HashMap<>();
nameL.put("en", geoRegion.getName());
for(Map.Entry<String, Map<String, GeoRegion>> entry : forNameJGeoRegions.entrySet()){
nameL.put(entry.getKey(), entry.getValue().get(geoRegion.getIsoCode()).getName());
}
String nameLJson = objectMapper.writeValueAsString(nameL);
geoRegion.setNameL(nameLJson);
}
}
private List<GeoRegion> assignParents(Map<String, GeoRegion> geoRegions) {
String world = "001";
String continents = "002,019,142,150,009";
String forAfrica = "015,014,017,018,011";
String forAsia = "143,030,034,035,145";
String forEurope = "151,154,039,155";
String forOceania = "053,054,057,061";
String forAmerica = "419,003,021";
String forLatinAmerica = "029,013,005";
for (String isoCode : continents.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(world));
}
for (String isoCode : forAfrica.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[0]));
}
for (String isoCode : forAmerica.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[1]));
}
for (String isoCode : forAsia.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[2]));
}
for (String isoCode : forEurope.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[3]));
}
for (String isoCode : forOceania.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(continents.split(",")[4]));
}
for (String isoCode : forLatinAmerica.split(",")) {
geoRegions.get(isoCode).setParentRegion(geoRegions.get(forAmerica.split(",")[0]));
}
return new ArrayList<>(geoRegions.values());
}
private ZipFile getCoreZip() throws IOException {
URL url = new URL(CLDR_URL);
LOG.info("Downloading " + url);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
File tempFileForXml = File.createTempFile("core", ".zip");
connection.setRequestMethod("GET");
InputStream in = connection.getInputStream();
FileOutputStream out = new FileOutputStream(tempFileForXml);
IOUtils.copy(in, out);
out.close();
LOG.info("Done copying CLDR data to file");
ZipFile zipFile = new ZipFile(tempFileForXml);
return zipFile;
}
public class ParserForCLDR extends DefaultHandler {
private GeoRegion geoRegion;
private Map<String, GeoRegion> regionMap;
// needs for avoid duplicate geoRegions in regionList
private List<String> isoCodeList;
private String xmlTag = "";
private Attributes attributes;
public Map<String, GeoRegion> getRegionMap() {
return regionMap;
}
@Override
public void startDocument() throws SAXException {
regionMap = new HashMap<>();
isoCodeList = new ArrayList<>();
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
geoRegion = new GeoRegion();
this.xmlTag = qName;
this.attributes = attributes;
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
super.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
String attribute = attributes.getValue("type");
if (this.xmlTag.equalsIgnoreCase("territory") && attribute.matches("[0-9]+") && !isoCodeList.contains(attribute)) {
geoRegion.setIsoCode(attribute);
geoRegion.setName(new String(ch, start, length));
isoCodeList.add(attribute);
regionMap.put(geoRegion.getIsoCode(), geoRegion);
}
}
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment