Article:
Giao diện đa ngôn ngữ với ActionScript3Gettext
948
ngocdaothanh.myopenid.com 172Over 3 years ago |
Vài năm trước, các project CNTT chủ yếu thuộc loại out sourcing dành cho xuất khẩu. Việt Nam vừa gia nhập WTO, giao lưu quốc tế tăng nhiều, kinh tế phát triển nhiều... Điều kiện khá giả hơn, người Việt Nam cũng muốn dùng những sản phẩm trước đây chỉ dành cho xuất khẩu kia.
Vậy sao chúng ta không bắn một phát chết vài con chim, chỉ cần viết chương trình một lần, nhưng cả Tây lẫn ta đều dùng được? Nói chung, vấn đề chỉ là chuyển ngữ. Tiếp theo bài viết Giao diện đa ngôn ngữ với Ruby-GetText dùng cho chương trình Ruby, xin tiếp tục chủ đề giao diện đa ngôn ngữ trong Flash.
Dưới đây xin trình bày các bước tôi đang dùng trong project sắp ra mắt đại chúng.
Bước 1, tìm hiểu cách dùng thư viện actionscript3gettext (từ trang web đó có nhiều link, xin bạn đọc tự tìm hiểu kĩ thêm).
Bước 2, viết chương trình ActionScript. Trong source code cần kẹp chuỗi cần hiển thị vào ngoặc (thực chất là gọi hàm) như sau _("String resource").
Bước 3, cài Ruby và RubyGettext như hướng dẫn ở bài viết Giao diện đa ngôn ngữ với Ruby-GetText.
Bước 4, copy chương trình dưới đây vào thư mục chứa chương trình ActionScript.
- Khi chạy với tham số updatepo, nó sẽ trích tất cả các chuỗi kí tự ở bước 2 thành tập tin .po.
- Khi chạy với tham số makemo, nó sẽ dịch tập tin .po thành .mo.
Có trong tay tập tin .mo, thì có thể áp dụng kiến thức học được ở bước 1.
require 'gettext/utils'
module GetText
module_function
def msgmerge_all(textdomain, app_version, po_root = 'po', refpot = 'tmp.pot')
FileUtils.mkdir_p(po_root) unless FileTest.exist? po_root
msgmerge("#{po_root}/#{textdomain}.pot", refpot, app_version)
Dir.glob("#{po_root}/*.po"){ |f|
lang = /#{po_root}\/(.*)\.po/.match(f).to_a[1]
msgmerge("#{po_root}/#{lang}.po", refpot, app_version)
}
end
def update_pofiles(textdomain, files, app_version, po_root = 'po', refpot = 'tmp.pot')
files2 = '"' + files.join('" "') + '"'
system("xgettext #{files2} -L Python --from-code=UTF-8 --output=#{refpot} --no-wrap --sort-output")
if !FileTest.exist?(refpot)
puts 'No GetText string extracted'
return
end
msgmerge_all(textdomain, app_version, po_root, refpot)
File.delete(refpot)
end
def create_mofiles(verbose = false, podir = './po', targetdir = './data/locale', targetpath_rule = '%s/LC_MESSAGES')
modir = File.join(targetdir, targetpath_rule)
Dir.glob(File.join(podir, "*.po")) do |file|
lang = /\/([^\/]+?)\.po/.match(file[podir.size..-1]).to_a[1]
FileUtils.mkdir_p(targetdir) unless File.directory?(targetdir)
rmsgfmt(file, File.join(targetdir, "#{lang}.mo"))
if verbose
$stderr.puts %Q[#{file} -> #{File.join(targetdir, "#{lang}.mo")}]
end
end
end
end
def updatepo
puts 'Update pot/po files'
GetText.update_pofiles('locale', Dir.glob('**/*.as'), '')
end
def makemo
puts 'Create mo-files'
GetText.create_mofiles(true, 'po', 'mo')
end
def main
if ARGV.size != 1
puts 'Usage: gettext.rb '
elsif ARGV[0] == 'updatepo'
updatepo
elsif ARGV[0] == 'makemo'
makemo
else
puts 'gettext.rb '
end
end
main
172