Register for your free account! | Forgot your password?

You last visited: Today at 05:10

  • Please register to post and access all features, it's quick, easy and FREE!

Advertisement



i18n - Basic multilanguage for lua

Discussion on i18n - Basic multilanguage for lua within the Metin2 PServer Guides & Strategies forum part of the Metin2 Private Server category.

Reply
 
Old   #1

 
elite*gold: 83
Join Date: Nov 2013
Posts: 2,891
Received Thanks: 2,764
i18n - Basic multilanguage for lua

i18n.lua
Code:
local translations = {}
local config = {
    default_locale = nil,
    context_locale = nil,
    locales = {},
    locales_by_id = {},
    locales_by_short_name = {},
    locales_by_name = {}
}

i18n = {}
i18n.locale = {}
i18n.locales = {}

setmetatable(i18n.locale, {
    __newindex = function(tbl, k, v) end,
    __index = function(tbl, k)
        local t = type(k)
        if t == "number" then
            return config.locales_by_id[k]
        elseif t == "string" then
            return config.locales_by_short_name[k] or config.locales_by_name[k]
        else
            return config.context_locale or config.default_locale
        end
    end
})

setmetatable(i18n.locales, {
    __newindex = function(tbl, k, v) end,
    __index = config.locales
})

function i18n.get(key, locale)
    if not locale then
        locale = config.context_locale or config.default_locale

        if not locale then
            return key
        end
    end

    return translations[locale][key] or key
end

function i18n.set_default_locale(locale)
    config.default_locale = locale
end

function i18n.with_context_locale(locale, fnc)
    config.context_locale = locale
    fnc()
    config.context_locale = nil
end

function i18n.register_locale(id, name_short, name)
    -- check if a locale by any of the given parameters is already registered
    if config.locales_by_id[id] then
        local l = config.locales_by_id[id]
        error(string.format("locale with id %d already registered (%d %s %s)", l.id, l.id, l.name_short, l.name))
    elseif config.locales_by_short_name[name_short] then
        local l = config.locales_by_short_name[name_short]
        error(string.format("locale with short name %s already registered (%d %s %s)", l.name_short, l.id, l.name_short, l.name))
    elseif config.locales_by_name[name] then
        local l = config.locales_by_name[name]
        error(string.format("locale with name %s already registered (%d %s %s)", l.name, l.id, l.name_short, l.name))
    end
    
    local locale = {
        id = id,
        name_short = name_short,
        name = name
    }

    table.insert(config.locales, locale)
    config.locales_by_id[id] = locale
    config.locales_by_short_name[name_short] = locale
    config.locales_by_name[name] = locale
end

function i18n.load_file(file, locale, append)
    if not translations[locale] or not append then
        translations[locale] = {}
    end

    for k, v in pairs(dofile(file)) do
        translations[locale][k] = v
    end
end
Example usage:
Loading the translation files:
i18n_init.lua
Code:
dofile("i18n.lua")

i18n.register_locale(1, "DE", "Deutsch")
i18n.register_locale(2, "EN", "English")

i18n.set_default_locale(i18n.locale.EN)

i18n.load_file("locale_de.lua", i18n.locale.DE)
i18n.load_file("locale_en.lua", i18n.locale.EN)
locale_de.lua
Code:
return {
    GREET = "Hallo"
}
locale_en.lua
Code:
return {
    GREET = "Hello"
}
Usage for metin2:
questlib:
Code:
function with_player_locale(fnc)
    i18n.with_context_locale(i18n.locale[pc.getf("i18n", "locale")], fnc)
end
Quest:
Code:
quest test begin
state start begin
    when login begin
        with_player_locale(function()
            say(i18n.get("GREET"))
        end)
    end
end
end
Select locale quest:
Code:
quest select_locale begin
state start begin
    when login begin
        with_player_locale(function()
            send_letter(i18n.get("SELECT_LOCALE_LETTER"))
        end)
    end

    when button or info begin
        with_player_locale(function()
            local options = {}
            local option_ids = {}

            for _, locale in ipairs(i18n.locales) do
                table.insert(options, locale.name)
                table.insert(option_ids, locale.id)
            end

            say_title(i18n.get("SELECT_LOCALE_TITLE"))
            say(i18n.get("SELECT_LOCALE_TEXT"))
            local selected_locale_id = select(options)
            pc.setf("i18n", "locale", selected_locale_id)
        end)
    end
end
end
rollback is offline  
Thanks
6 Users
Old 04/16/2019, 00:52   #2

 
elite*gold: 83
Join Date: Nov 2013
Posts: 2,891
Received Thanks: 2,764
Die neuste Version findet ihr jetzt auch unter:
rollback is offline  
Old 04/19/2019, 10:43   #3
„Ich bin Igneel's Sohn!“




 
Natsu Dragneel's Avatar
 
elite*gold: 1476
The Black Market: 666/0/0
Join Date: Jun 2016
Posts: 13,212
Received Thanks: 2,562
Quote:
Originally Posted by rollback View Post
Die neuste Version findet ihr jetzt auch unter:
Nice! Danke dir dafür.
Natsu Dragneel is offline  
Reply


Similar Threads Similar Threads
[Realase]Décripteur de fichier lua ressource 7.3 /Décripteur resource file lua 7.3
04/08/2012 - Rappelz Private Server - 5 Replies
Closed
Sehr weit geführte locale_.lua (locale_big5.lua)
08/02/2010 - Metin2 PServer Guides & Strategies - 50 Replies
Abend^^ Habe hier mal selber eine locale_.lua übersetzt! Ich habe mich von anderen localen inspirieren lassen habe sie aber bis auf 6 Zeilen selber geschrieben!! Ich werde sie, wenn ihr mir sagt welche quests die von zeile 300 - 900 sind, noch vervollständigen aber erstes Ziel... Website mit allen Quests zum Ansehen und einzelnen Download! Hoffe konnte euch weiterhelfen! Locale_.lua + skill_group.quest damit auch die Lehrer deutsch sind! Updatestand: 22.01.2010 Version 1: locale_.lua...



All times are GMT +1. The time now is 05:10.


Powered by vBulletin®
Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Support | Contact Us | FAQ | Advertising | Privacy Policy | Terms of Service | Abuse
Copyright ©2025 elitepvpers All Rights Reserved.