संपादित करें: जोड़ा metatable सामान
चाहेंगे कोई मन में थोड़ा और स्पष्ट
निश्चित रूप से ऊपर के उदाहरण कोड बना रही है।
एक फाइल में आप निम्न विधियों के साथ एक 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
यह करने के लिए व्यापक एक सवाल का जवाब देने का तरीका है। थोड़ी सी चीजें नीचे संकीर्ण करें। आपका आवेदन क्या है? अनुवाद करने की क्या ज़रूरत है? वेब पेजों को बिल्कुल उत्पन्न करने के लिए आप क्या उपयोग कर रहे हैं? इत्यादि। –