luacomputercraft

(ComputerCraft) (minecraft version 1.7.10) (question) Make a language file reader


I'd like to make a language reader for my operating system, but I cant find anything that helps me.
I want to put the list into my other script.
Heres the de-de language file (location: /os/bin/):

    de = {
    Menu = "Menü",
    Shutdown = "Ausschalten",
    MenuLength = 4,
    ShutdownLength = 11
    }

Can anyone help me please?


Solution

  • The gsub() string function/method can do that with your translation table.
    You only have to use your language table for that.
    Example...

    # /usr/bin/lua
    Lua 5.3.5  Copyright (C) 1994-2018 Lua.org, PUC-Rio
    > de = {
    >>     Menu = "Menü",
    >>     Shutdown = "Ausschalten",
    >>     MenuLength = 4,
    >>     ShutdownLength = 11
    >>     }
    > language = de
    > print(('Menu Shutdown'):gsub('(%g+)', language))
    Menü Ausschalten    2
    

    If you have to use Lua 5.1 then use %w...

    # /bin/lua
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > de = {
    >>     Menu = "Menü",
    >>     Shutdown = "Ausschalten",
    >>     MenuLength = 4,
    >>     ShutdownLength = 11
    >>     }
    > language = de
    > print(('Menu Shutdown'):gsub('(%w+)', language))
    Menü Ausschalten    2
    

    The length can be measured with the len() function/method...

    > print(('Shutdown'):gsub('(%w+)', language):len())
    11
    > print(('Menu'):gsub('(%w+)', language):len())
    5
    

    As you can see in Lua the Umlaut ü is measured different.

    To include your de-de.lua i suggest dofile() that loads it with your specific path...

    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > dofile('/os/bin/de-de.lua') -- But read comment about security
    > print(de['Menu'])
    Menü
    

    Try to convert the special words with german Umlaut into its byte representation.
    And put the bytes into your translation table...

    $ lua
    Lua 5.4.3  Copyright (C) 1994-2021 Lua.org, PUC-Rio
    > print(('Menü'):byte(1,-1))
    77  101 110 195 188
    > print('\77\101\110\195\188')
    Menü
    > de = {Menu = '\77\101\110\195\188'}
    > print(('Menu'):gsub('%g+', de))
    Menü    1
    

    ...or for Lua 5.1...

    $ /bin/lua
    Lua 5.1.5  Copyright (C) 1994-2012 Lua.org, PUC-Rio
    > print(('Menü'):byte(1, -1))
    77  101 110 195 188
    > print('\77\101\110\195\188')
    Menü
    > de = {Menu = '\77\101\110\195\188'}
    > print(('Menu'):gsub('%w+', de))
    Menü    1
    

    You also can mix it.
    And here are the bytes for: üäößÜÄÖ

    > print(('üäößÜÄÖ'):byte(1, -1))
    195 188 195 164 195 182 195 159 195 156 195 132 195 150
    > de = {Menu = 'Men\195\188', Umlaute = '\195\188\195\164\195\182\195\159\195\156\195\132\195\150'}
    > print(('Menu Umlaute'):gsub('%w+', de))
    Menü üäößÜÄÖ    2