-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathSpokenDialect.java
More file actions
175 lines (156 loc) · 6.07 KB
/
SpokenDialect.java
File metadata and controls
175 lines (156 loc) · 6.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
* microsoft-translator-java-api
*
* Copyright 2012 Jonathan Griggs <jonathan.griggs at gmail.com>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.memetix.mst.language;
import com.memetix.mst.MicrosoftTranslatorAPI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* SpokenLanguage - an enum of all spoken language codes supported by the Microsoft Translator API for the Speak Service
*
* Uses the AJAX Interface V2 - see: http://msdn.microsoft.com/en-us/library/ff512399.aspx
*
* @author Jonathan Griggs <jonathan.griggs at gmail.com>
*/
public enum SpokenDialect {
CATALAN_SPAIN("ca-es"),
DANISH_DENMARK("da-dk"),
GERMAN_GERMANY("de-de"),
ENGLISH_AUSTRALIA("en-au"),
ENGLISH_CANADA("en-ca"),
ENGLISH_UNITED_KINGDOM("en-gb"),
ENGLISH_INDIA("en-in"),
ENGLISH_UNITED_STATES("en-us"),
SPANISH_SPAIN("es-es"),
SPANISH_MEXICO("es-mx"),
FINNISH_FINLAND("fi-fi"),
FRENCH_CANADA("fr-ca"),
FRENCH_FRANCE("fr-fr"),
ITALIAN_ITALY("it-it"),
JAPANESE_JAPAN("ja-jp"),
KOREAN_KOREA("ko-kr"),
NORWEGIAN_NORWAY("nb-no"),
DUTCH_NETHERLANDS("nl-nl"),
POLISH_POLAND("pl-pl"),
PORTUGUESE_BRAZIL("pt-br"),
PORTUGUESE_PORTUGAL("pt-pt"),
RUSSIAN_RUSSIA("ru-ru"),
SWEDISH_SWEDEN("sv-se"),
CHINESE_SIMPLIFIED_PEOPLES_REPUBLIC_OF_CHINA("zh-cn"),
CHINESE_TRADITIONAL_HONG_KONG_SAR("zh-hk"),
CHINESE_TRADITIONAL_TAIWAN("zh-tw");
/**
* Microsoft's String representation of this language.
*/
private final String language;
/**
* Internal Localized Name Cache
*/
private Map<Language,String> localizedCache = new ConcurrentHashMap<Language,String>();
/**
* Enum constructor.
* @param pLanguage The language identifier.
*/
private SpokenDialect(final String pLanguage) {
language = pLanguage;
}
public static SpokenDialect fromString(final String pLanguage) {
for (SpokenDialect l : values()) {
if (l.toString().equals(pLanguage)) {
return l;
}
}
return null;
}
/**
* Returns the String representation of this language.
* @return The String representation of this language.
*/
@Override
public String toString() {
return language;
}
public static void setKey(String pKey) {
SpokenDialectService.setKey(pKey);
}
/**
* getName()
*
* Returns the name for this language in the tongue of the specified locale
*
* If the name is not cached, then it retrieves the name of ALL languages in this locale.
* This is not bad behavior for 2 reasons:
*
* 1) We can make a reasonable assumption that the client will request the
* name of another language in the same locale
* 2) The GetLanguageNames service call expects an array and therefore we can
* retrieve ALL names in the same, single call anyway.
*
* @return The String representation of this language's localized Name.
*/
public String getName(Language locale) throws Exception {
String localizedName = null;
if(this.localizedCache.containsKey(locale)) {
localizedName = this.localizedCache.get(locale);
} else {
//If not in the cache, pre-load all the Language names for this locale
String[] names = SpokenDialectService.execute(SpokenDialect.values(), locale);
int i = 0;
for(SpokenDialect lang : SpokenDialect.values()) {
lang.localizedCache.put(locale,names[i]);
i++;
}
localizedName = this.localizedCache.get(locale);
}
return localizedName;
}
// Flushes the localized name cache for this language
private void flushCache() {
this.localizedCache.clear();
}
// Flushes the localized name cache for all languages
public static void flushNameCache() {
for(SpokenDialect lang : SpokenDialect.values())
lang.flushCache();
}
private final static class SpokenDialectService extends MicrosoftTranslatorAPI {
private static final String SERVICE_URL = "http://api.microsofttranslator.com/V2/Ajax.svc/GetLanguageNames?";
/**
* Detects the language of a supplied String.
*
* @param text The String to detect the language of.
* @return A DetectResult object containing the language, confidence and reliability.
* @throws Exception on error.
*/
public static String[] execute(final SpokenDialect[] targets, final Language locale) throws Exception {
//Run the basic service validations first
validateServiceState();
String[] localizedNames = new String[0];
if(locale==Language.AUTO_DETECT) {
return localizedNames;
}
final String targetString = buildStringArrayParam(SpokenDialect.values());
final URL url = new URL(SERVICE_URL
+PARAM_LOCALE+URLEncoder.encode(locale.toString(), ENCODING)
+PARAM_LANGUAGE_CODES + URLEncoder.encode(targetString, ENCODING));
localizedNames = retrieveStringArr(url);
return localizedNames;
}
}
}