如果语言被切换,我想翻译我正在维护的DSPace实例中item-view.xsl中显示的主题(MeSH)术语.以前我使用下面的代码(我在XSLUtils.java类中添加了这个代码)来查找Babelmesh站点并动态翻译它.
public static String lookupBabelMeSH(String term,String lang) {
try {
URLConnection babelMeshConn = (new URL("https://babelmesh.nlm.nih.gov/mesh_trans.PHP?oterm=" + URLEncoder.encode(term,"UTF-8") + "&in=ENG&out=" + lang)).openConnection();
babelMeshConn.setConnectTimeout(5000);
babelMeshConn.setReadTimeout(5000);
BufferedReader in = new BufferedReader(new InputStreamReader(babelMeshConn.getInputStream(),"UTF-8"));
String value = in.readLine();
in.close();
if (!StringUtils.isEmpty(value)) {
return value;
}
} catch (MalformedURLException mue) {
} catch (IOException ioe) {
}
return null;
}
然后我在item-view.xsl中使用它,如下所示:
现在,我想在每次切换语言时都不翻译BabelMesh网站来翻译文本,因为请求数量有限制.此外,由于BabelMesh的url是硬编码的,因此对BabelMesh服务的任何更改都将破坏翻译的呈现.我只需要将这些术语翻译成中文和法文.我的翻译位于[dspace] / config / mesh目录中.这些文件分别命名为mterms_fr和mterms_zh,用于法语和中文翻译.
mterms_fr
Acanthocheilonemiasis::acanthocheilonemiase
Acanthocytes::ACANTHOCYTE
Acantholysis::ACANTHOLYSE
Acanthoma::ACANTHOME
Acanthopodina::ACANTHOPODINA
Acanthosis Nigricans::ACANTHOSIS NIGRICANS
Acarbose::ACARBOSE
Acari::ACARIEN
Acaricides::Acaricides
Acaridae::ACARIDAE
Acatalasia::ACATALASIE
Accelerated Idioventricular Rhythm::RYTHME IdioVENTRICULAIRE ACCELERE
Acceleration::ACCELERATION
mterms_zh
Acanthocheilonemiasis::棘唇虫病
Acanthocytes::棘形红细胞
Acantholysis::皮肤棘层松解
Acanthoma::棘皮瘤
Acanthopodina::Acanthopodina
Acanthosis Nigricans::Acanthosis Nigricans
Acarbose::Acarbose
Acari::Acari
Acaricides::Acaricides
Acaridae::Acaridae
Acatalasia::Acatalasia
Accelerated Idioventricular Rhythm::Accelerated Idioventricular Rhythm
Acceleration::加速度
如果您注意到,::是英语术语和翻译之间的分隔符.如果该术语没有翻译,则保留英语术语(例如杀螨剂).
那么可以从[dspace] / config / mesh目录中查找这些文件并即时进行翻译吗?
编辑
我想补充一点,如果在翻译文件中找不到该术语,则应该按原样返回(例如,一些随机文本应该返回一些随机文本),因为预计我无法控制用户将输入的内容主题字段(即通过批量导入).
提前致谢!
最佳答案
你可以尝试这样的东西(添加到XSLUtils.java):
private static Properties chinnese = null;
private static Properties french = null;
static{
try {
chinnese = new Properties();
String mterms_zhPath=ConfigurationManager.getProperty("mterms_zh.path");
chinnese.load(new InputStreamReader(new FileInputStream(new File(mterms_zhPath)),"UTF8"));
french = new Properties();
String mterms_frPath=ConfigurationManager.getProperty("mterms_fr.path");
french.load(new InputStreamReader(new FileInputStream(new File(mterms_frPath)),"UTF8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String lookupMeSH(String term,String lang) {
String translated=null;
if("zh".equals(lang)){
translated=chinnese.getProperty(term);
}else if("fr".equals(lang)){
translated=french.getProperty(term);
}
return translated;
}
mterms_zh.path= /put/the/file/path
mterms_fr.path=/home/dspace_instalation/config/mterms_fr
检查langs比较和文件征询.
然后改变:
对于
在xsl
并将文件分隔符从“::”替换为“=”
添加完整运行类:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;
public class Test3 {
private static Properties chinnese = null;
private static Properties french = null;
static{
chinnese = new Properties();
try {
String mterms_zhPath="D:/mterms_fr";
chinnese.load(new InputStreamReader(new FileInputStream(new File(mterms_zhPath)),"UTF8"));
french = new Properties();
String mterms_frPath="D:/mterms_fr";
french.load(new InputStreamReader(new FileInputStream(new File(mterms_frPath)),"UTF8"));
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static String lookupMeSH(String term,String lang) {
String translated=null;
if("zh".equals(lang)){
translated=chinnese.getProperty(term);
}else if("fr".equals(lang)){
translated=french.getProperty(term);
}
return translated;
}
public static void main (String [] args) {
// Test3 test3=new Test3();
//XSLUtils s = new XSLUtils();
System.out.println(lookupMeSH("Acari","fr")); }
}