1
|
/**
|
2
|
* Copyright (C) 2020 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.api.service.l10n;
|
10
|
|
11
|
import java.util.Enumeration;
|
12
|
import java.util.Hashtable;
|
13
|
import java.util.List;
|
14
|
import java.util.Locale;
|
15
|
import java.util.Vector;
|
16
|
|
17
|
import org.hibernate.HibernateException;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.stereotype.Component;
|
20
|
|
21
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
22
|
import eu.etaxonomy.cdm.model.common.Language;
|
23
|
|
24
|
@Component
|
25
|
public class LocaleContext {
|
26
|
|
27
|
protected static ThreadLocal<Vector<Locale>> localesHolder = new ThreadLocal<Vector<Locale>>(){
|
28
|
|
29
|
@Override
|
30
|
protected Vector<Locale> initialValue() {
|
31
|
return new Vector<>();
|
32
|
}
|
33
|
};
|
34
|
|
35
|
protected static Hashtable<String, List<Language>> languageMap = new Hashtable<>();
|
36
|
|
37
|
private ITermService termService;
|
38
|
|
39
|
@Autowired
|
40
|
public void setTermService(ITermService termService) {
|
41
|
this.termService = termService;
|
42
|
}
|
43
|
|
44
|
public void setLocales(Enumeration<Locale> locales) {
|
45
|
Vector<Locale> l = new Vector<>();
|
46
|
while(locales.hasMoreElements()){
|
47
|
l.add(locales.nextElement());
|
48
|
}
|
49
|
LocaleContext.localesHolder.set(l);
|
50
|
mapToLanguages(l);
|
51
|
}
|
52
|
|
53
|
public static Enumeration<Locale> getLocales(){
|
54
|
return localesHolder.get().elements();
|
55
|
}
|
56
|
|
57
|
public static List<Language> getLanguages(){
|
58
|
String localesKey = composeLocalesKey(getLocales());
|
59
|
return languageMap.get(localesKey);
|
60
|
}
|
61
|
|
62
|
private void mapToLanguages(Vector<Locale> locales) {
|
63
|
|
64
|
String localesKey = composeLocalesKey(locales.elements());
|
65
|
|
66
|
try {
|
67
|
if (!languageMap.containsKey(localesKey)) {
|
68
|
List<Language> languages = termService.getLanguagesByLocale(locales.elements());
|
69
|
languageMap.put(localesKey, languages);
|
70
|
}
|
71
|
} catch (HibernateException e) {
|
72
|
|
73
|
System.err.println("DEBUG: " + Thread.currentThread());
|
74
|
e.printStackTrace(System.err);
|
75
|
}
|
76
|
}
|
77
|
|
78
|
private static String composeLocalesKey(Enumeration<Locale> locales) {
|
79
|
String localesKey = "";
|
80
|
while(locales.hasMoreElements()){
|
81
|
localesKey += locales.nextElement() + ",";
|
82
|
}
|
83
|
return localesKey;
|
84
|
}
|
85
|
}
|