2012-01-16 143 views
8

我建立了一個Lua的網絡應用程序,很明顯我需要開始國際化(「i18n」)它爲我的海外客戶。Lua - 如何做國際化?

在Lua中,我的應用程序國際化的最佳方式是什麼?

我意識到這是一個重要的任務,尤其是因爲我的一些顯示器是用HTML模板進行硬編碼的,而且有些數據字段位於我目前面向美國英語的數據庫中。

任何指導將不勝感激。

+0

這是方式廣泛回答的問題。將事情縮小一點。你的申請是什麼?什麼需要翻譯?你用什麼來精確地生成網頁?等等。 – 2012-01-16 22:35:33

回答

5

編輯:加元表的東西

會有人頭腦作出更加清楚一點

當然,上面的示例代碼。

在一個文件中定義了一個i18n變量使用以下方法:

local i18n = { locales = {} } 

local currentLocale = 'en' -- the default language 

function i18n.setLocale(newLocale) 
    currentLocale = newLocale 
    assert(i18n.locales[currentLocale], ("The locale %q was unknown"):format(newLocale)) 
end 

local function translate(id) 
    local result = i18n.locales[currentLocale][id] 
    assert(result, ("The id %q was not found in the current locale (%q)"):format(id, currentLocale) 
    return result 
end 

i18n.translate = translate 

setmetatable(i18n, {__call = function(_,...) return translate(id) end}) 

return i18n 

在同一個文件中,或者在其它(一個或多個),則包括需要內部i18n.locales的區域設置。

local i18n = require 'i18n' -- remove this line if on the same file as before 

i18n.locales.en = { 
    helloWorld = "Hello world", 
    loginWarning = "You need to be logged in to do that" 
} 

i18n.locales.es = { 
    helloWorld = "Hola mundo", 
    loginWarning = "Debes haber iniciado una sesión para hacer eso" 
} 
... 

用法:

local i18n = require 'i18n' 
require 'locales' -- if using a separate file for the locales, require it too 

print(i18n.translate('helloWorld')) -- Hello world 
i18n.setLocale('es') 
-- using i18n() instead of i18n.translate() 
print(i18n('helloWorld')) -- Hola mundo 
+0

kikito,非常感謝詳細的職位。就像一個想法一樣,如果實現使用了metatable,那麼是否可以根本不使用Translate表?這樣,現在的用法就像調用i18n('helloWorld')一樣簡單,而不是i18n.translate('helloWorld') – nickb 2012-01-18 04:21:24

+0

@nickb,當然,我編輯了我的答案來包含它。我也刪除了本地T. – kikito 2012-01-18 08:10:44

+0

謝謝你這是非常有用的,但我發現了一個錯誤,我編輯你的代碼,翻譯應該被稱爲翻譯(...)傳遞字符串的id來翻譯,乾杯 – 2016-04-28 08:58:38

2

在Lua中,使用翻譯表非常簡單。首先編寫代碼,以便通過函數調用來使用每個需要轉換的字符串,如i18n"Hello World"。然後爲您想要支持的語言編寫翻譯表。例如,English={["Hello World"]="Hello World"}(或添加一個索引metamethod,它只是返回密鑰,以避免重複字符串)和French={["Hello World"]="Salut le monde"}。最後在你需要時寫function i18n(s) return Language[s] end,然後設置Language=French

+0

那麼這樣的事情?
字符串= {}
字符串[ 「德國」] = { 「Anwendung」, 「打印查看」}
字符串[ 「英語」] = { 「應用程序」, 「窗口」}
語言= 「英語」
print(strings [language] [1]) – nickb 2012-01-16 23:38:13

+2

如果你這樣做,你將會在數字鍵中失去自己。有一天你會看到'language [1824]',並且不知道哪個字符串是1824,並且你的翻譯文件不會很好讀。最好做Luiz建議的 - 用「默認」英文字符串作爲鍵 - 這樣,你將永遠知道你在翻譯什麼。 – 2012-01-17 00:08:09

+0

對於語言條目使用枚舉樣式名稱是很常見的。例如:'English = {ACCOUNT_CREATION_ERROR_PASSWORD_CAPITALS =「您至少需要3個大寫字母。」}'。儘管眼睛不太好,但它具有邏輯意義,並可防止諸如「Hello world」之類的拼寫錯誤與「Hello World」不匹配。 – Deco 2012-01-17 02:34:14

0

我的解決方法與lhf的bib.lua(我的一個項目)的答案大致相同,使用一個定義字符串表的模塊。我只是想分享它,所以你可以看到一些代碼實際上使用了類似的東西。

它在圍兜/ trans.lua。

可能在我的做法可以補充一些事情是:在 參考語言定義的所有字符串中使用的語言定義

  1. 檢查是否正在使用的翻譯是完全的,例如。或者更好,即使 檢查要定義的程序中的所有字符串。

  2. 在不同非基準語言一元表機制打印 警告,並返回參考語言標籤,如果它不是可用在使用 語言。

把那些東西放在一邊,我很喜歡它。由於語言結構的差異,只有大量字符串不匹配纔會遇到困難。我想知道如何解決這些問題。

+0

嗨jpjacobs,你如何使用下面的文件? https://github.com/jpjacobs/bib/blob/master/bib-0.1/bib/trans.lua – nickb 2012-01-18 04:21:56

+0

這是'require''d [here](https://github.com/jpjacobs/bib/blob /master/bib-0.1/bib/config.lua#L20)。之後,您只需使用'strings = bib.trans.strings [en]''選擇一種語言。然後,無論何時你需要一個字符串,在'trans.lua'中創建它,並使用'strings.the_key_you_chose_for_your_string'代替。 – jpjacobs 2012-01-18 07:51:04