You can even write your program in Groovy using Chinese:
(1)In the ASCII file "File1.groovy" which you run:
groovy.lang.MetaClass.setUseReflection(true)
cc= new org.codehaus.groovy.control.CompilerConfiguration()
cc.setSourceEncoding('unicode')
new GroovyShell( cc ).run( new File('File2.groovy') )
(2)In the unicode file "File2.groovy":
def b= new Binding()
b.setVariable('中文',中文) //Chinese for 'Chinese'
def conf=new org.codehaus.groovy.control.CompilerConfiguration()
conf.setSourceEncoding('unicode')
new GroovyShell(b,conf).evaluate( new File('File3.groovy') )
class 中文{
public static Map 组(Collection self, Closure closure) { self.groupBy(closure) }
public static void 打句(Object self, Object value) {self.println(value)}
public static Object 找都(Object self, Closure closure) {self.findAll(closure)}
public static List 分类(Collection self, Closure closure) {self.sort(closure)}
public static void 每(Object self, Closure closure) {self.each(closure)} //每个
public static int 夵(Object self) {self.size()} //大小
public static List 割(String self) {self.tokenize()} //分割
public static List 向后(List self) {self.reverse()}
public static List 集(Object self, Closure closure) {self.collect(closure)}
public static String 满左(String self, Number numberOfChars) {self.padLeft(numberOfChars)}
}
(3)In the unicode file "File3.groovy":
use(中文){
def 物= '''\
The Java Collections API is the basis for all the nice support that Groovy gives you
through lists and maps. In fact, Groovy not only uses the same abstractions, it
even works on the very same classes that make up the Java Collections API.
'''
物.割().组{it}.集{ ['k':it.key, 'v':it.value.夵()] }.
找都{it.v>1}.分类{it.v}.向后().每{打句"${it.k.满左(12)}: $it.v"}
//or:
物.割().组{ it }.集{ ['k':it.key, 'v':it.value.夵()] }.
分类{it.v}[-1..-6].每{打句"${it.k.满左(12)} : $it.v"}
}
Although I'm not a native Chinese speaker, I often use Chinese characters just to make my code shorter.
This example is simple, using Groovy Categories. If you use Groovy Interceptors, you can do more like disable the English names, or dynamically parse the Chinese name, eg, ['满':'pad', '左':'left']
Cheers, Gavin Grover
回复 更多评论