我想翻译item-view中显示的主题(MeSH)术语。如果切换了语言,我要维护的DSPace实例中的xsl。以前我使用下面的代码(我在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
中这样使用它:
<xsl:choose>
<xsl:when test="$active-locale!='en'">
<xsl:variable name="current-locale">
<xsl:if test="$active-locale='fr'">
<xsl:text>FRE</xsl:text>
</xsl:if>
<xsl:if test="$active-locale='zh'">
<xsl:text>CHN</xsl:text>
</xsl:if>
</xsl:variable>
<xsl:variable name="translation">
<xsl:value-of select="util:lookupBabelMeSH(node(),$current-locale)"/>
</xsl:variable>
<xsl:choose>
<xsl:when test="$translation=''">
<xsl:value-of select="node()"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$translation"/>
</xsl:otherwise>
</xsl:choose>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="node()"/>
</xsl:otherwise>
</xsl:choose>
现在,我想翻译文本没有调用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::加速度
如果你注意到,::
是英语术语和翻译之间的分隔符。如果该术语没有翻译,则保留英文术语(例如Acaricides
)。
那么是否有可能从[dspace]/config/mesh
目录中查找这些文件并进行实时翻译?
编辑
我想补充一点,如果在翻译文件中没有找到术语,它应该原样返回(例如some random text
应该返回some random text
),因为预计我无法控制用户将在主题字段中输入什么(即通过批量导入)。
提前感谢!
您可以尝试这样做(添加到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;
}
在dspace.cfg中添加文件路径:
mterms_zh.path= /put/the/file/path
mterms_fr.path=/home/dspace_instalation/config/mterms_fr
检查语言比较和文件获取。
然后改变:
<xsl:value-of select="util:lookupBabelMeSH(node(),$current-locale)"/>
<xsl:value-of select="util:lookupMeSH(node(),$current-locale)"/>
在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")); }
}