eul*_*ler 5 java xslt-1.0 dspace
如果语言被切换,我想翻译我正在维护的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;
}
Run Code Online (Sandbox Code Playgroud)
然后我就这样使用它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>
Run Code Online (Sandbox Code Playgroud)
现在,我想在每次切换语言时都不翻译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
Run Code Online (Sandbox Code Playgroud)
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::???
Run Code Online (Sandbox Code Playgroud)
如果您注意到,则::
是英语术语和翻译之间的分隔符.如果该术语没有翻译,则保留英语术语(例如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;
}
Run Code Online (Sandbox Code Playgroud)
在 dspace.cfg 中,您应该添加文件的路径:
mterms_zh.path= /put/the/file/path
mterms_fr.path=/home/dspace_instalation/config/mterms_fr
Run Code Online (Sandbox Code Playgroud)
检查语言比较和文件获取。
然后改变:
<xsl:value-of select="util:lookupBabelMeSH(node(),$current-locale)"/>
Run Code Online (Sandbox Code Playgroud)
为了
<xsl:value-of select="util:lookupMeSH(node(),$current-locale)"/>
Run Code Online (Sandbox Code Playgroud)
在 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")); }
}
Run Code Online (Sandbox Code Playgroud)