传统的Web应用程序需要国际化。错误消息当前以这种方式写入源代码中:
addErrorMessage("some text here");
使用正则表达式可以轻松找到并提取这些符号。它们应该被替换为这样的东西:
addErrorMessage(ResourceBundle.getBundle("/Bundle", lcale).getString("key for text here"));
此处文字的密钥与此处的某些文字之间的对应关系将位于.property文件中。
根据一些linux大师的说法,可以使用awk实现,但我对此一无所知。我可以写一个小应用程序来完成这项任务,但它可能有点过分。是否有针对此目标的ide插件或现有应用程序?
答案 0 :(得分:0)
awk -v TextOrg='some text here' -v key='key for text here' ' "addErrorMessage(\"" TextOrg "\")" {
gsub( "addErrorMessage(\"" TextOrg "\")" \
, "addErrorMessage(ResourceBundle.getBundle(\"/Bundle\", lcale).getString(\"" key "\"))")
}
7
' YourFile
这是特定组合的一种方式。请小心:
-v ...
)"this f***ing text"
- > "this f\*\*\*ing text"
)如果是几个同伴,你当然想做。
她有一个同伴的文件
假设Trad.txt是一个包含一系列2行第1个原始文本的文件,第二个是密钥(以避免某些chara作为需要复杂转义序列解释的分隔符,如果使用的话)
ex:Trad.txt
some text
key text
other text
other key
示例代码(简单,没有详尽的安全性,......)未经测试,但对于使用awk的概念
awk '
# for first file only
FNR == NR {
# keep in memory first line as text to change
if ( NR % 2 ) TextOrg = $0
else {
# load in array the key corresponding (index is the text to change)
Key[ TextOrg] = $0
Len[ TextOrg] = length( addErrorMessage(\"" $0 "\")"
}
# don't go further in script for this line
next
}
# this point and further is reach only by second file
# if addError is found
/addErrorMessage(".*")/{
# try with each peer if there is a change (a more complex loop is more perfomant checking just necessary replacement but this one do the job)
for( TextOrg in Key) {
# to avoid regex interpretation
# Assuming for this sample code that there is 1 replace (loop is needed normaly)
# try a find of text (return the place where)
Here = index( addErrorMessage(\"" TextOrg "\")", $0)
if( Here > 0) {
# got a match, replace the substring be recreating a full one
$0 = substr( $0, 1, Here) \
"addErrorMessage(ResourceBundle.getBundle(\"/Bundle\", lcale).getString(\"" Key[ TextOrg] "\"))") \
substr( $0, Here + Len[ TextOrg])
}
}
}
# print the line in his current state (modified or not)
7
' Trad.txt YourFile
最后,这是一种解决方案解决方案,因为很多特殊情况可能会发生"ref: function addErrorMessage(\" ...\") bla bla"
会出现问题,或者()
内的空间未在此处理,或者是cutd line insdie ()
, ...