Download
(6 Kb)
Download
Updated: 10/30/23 11:17 AM
Compatibility:
Endless Archive (9.2.5)
base-game patch (9.1.5)
Necrom (9.0.0)
Scribes of Fate (8.3.5)
Firesong (8.2.5)
Lost Depths (8.1.5)
High Isle (8.0.0)
Updated:10/30/23 11:17 AM
Created:11/11/20 02:08 PM
Monthly downloads:2,458
Total downloads:115,427
Favorites:16
MD5:
LibLanguage  Popular! (More than 5000 hits)
Version: 37
by: Shadowfen [More]
A library of localization string functionality extracted from my LibSFUtils library for general accessibility.
The function here is designed to load strings for the appropriate client language (if available) and load the strings from your default language if the client language is unavailable.

This library simplifies and standardizes the content of the language tables to make it easier for people to contribute translations for your addon.


Language Table(s)

In order to support multiple languages with your addon, you need to create a table that lists all of the strings that you display to the user from your addon, assigning them a key that you will use to retrieve the string value with GetString(). This language table itself is assigned to the two character language key for the language it supports.

For example: You could have the following string table defined:
Code:
    
    local AC_localization_strings = {
        de = {
            AC_IAKONI_TAG= "Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_1= "Set#1",
            AC_IAKONI_CATEGORY_SET_1_DESC= "#1 Set aus dem AddOn Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_2= "Set#2",
            AC_IAKONI_CATEGORY_SET_2_DESC= "#2 Set aus dem AddOn Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_3= "Set#3",
            AC_IAKONI_CATEGORY_SET_3_DESC= "#3 Set aus dem AddOn Iakoni's Gear Changer",
            },
        
        en = {
            AC_IAKONI_TAG= "Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_1= "Set#1",
            AC_IAKONI_CATEGORY_SET_1_DESC= "#1 Set from Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_2= "Set#2",
            AC_IAKONI_CATEGORY_SET_2_DESC= "#2 Set from Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_3= "Set#3",
            AC_IAKONI_CATEGORY_SET_3_DESC= "#3 Set from Iakoni's Gear Changer",
        },
           
        zh = {
            AC_IAKONI_TAG= "Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_1= "装备配置#1",
            AC_IAKONI_CATEGORY_SET_1_DESC= "#1 号装备配置 Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_2= "装备配置#2",
            AC_IAKONI_CATEGORY_SET_2_DESC= "#2 号装备配置 Iakoni's Gear Changer",
            AC_IAKONI_CATEGORY_SET_3= "装备配置#3",
            AC_IAKONI_CATEGORY_SET_3_DESC= "#3 号装备配置 Iakoni's Gear Changer",
        },
    }
This particular table provides support for German, English, and Chinese strings. You would then
initialize your localizations when your addon loads, and after that in order to get the appropriate
string for AC_IAKONI_TAG you would simply call GetString(AC_IAKONI_TAG).


Loading the Language Table(s)

Basically the default language table is loaded first as the strings table for your addon
and then the client language table (if available) updates the strings table with the new
language strings where provided.

Note that your default language table must have ALL of the strings defined for your addon.
If the other language tables miss a particular string value, then the default language value
is left unchanged.

function LibLanguage.LoadLanguage(localization_languages_table, defaultLang)
Add strings to the string table for the client language (or the default language if the client language did not have strings defined for it). The localization_strings parameter is a table of language tables of localization strings, and the defaultLang parameter defaults to "en" if not provided.
With the example table defined above, you would call you initialization function:
Code:
    
        LibLanguage.LoadLanguage(AC_localization_strings,"en")
If you were running a German client, then the above function call would load the default language English (en) first, and then override the English strings with the corresponding German (de) ones where available.


Separate Language Files

You can split the definitions of
Code:
        AC_localization_strings["en"], 
	AC_localization_strings["de"], and 
	AC_localization_strings["zh"]
into separate files (en.lua for the default, de.lua, zh.lua, etc). Following our example we would have a Localisation\de.lua file which would contain:
Code:
	AC_localization_strings = AC_localization_strings  or {}

	AC_localization_strings]["de"] = {
		AC_IAKONI_TAG= "Iakoni's Gear Changer",
		AC_IAKONI_CATEGORY_SET_1= "Set#1",
		AC_IAKONI_CATEGORY_SET_1_DESC= "#1 Set aus dem AddOn Iakoni's Gear Changer",
		AC_IAKONI_CATEGORY_SET_2= "Set#2",
		AC_IAKONI_CATEGORY_SET_2_DESC= "#2 Set aus dem AddOn Iakoni's Gear Changer",
		AC_IAKONI_CATEGORY_SET_3= "Set#3",
		AC_IAKONI_CATEGORY_SET_3_DESC= "#3 Set aus dem AddOn Iakoni's Gear Changer",
	}
Then add the following lines to your addon manifest (at the top - the string tables must be declared before the code tries to use them):
Code:
Localisation\en.lua
Localisation\$(language).lua
(This assumes that your strings files are in a directory called Localization, and that en.lua is your default language. $(language) is interpreted into the language that the client is set for, so if you are running a German client, the manifest would try to load Localisation\de.lua if it exists.)

This way you will only load the strings for two languages (at most) instead of all of them you have available.
37:
  • API bump.

36:
  • API bump (Necrom).

35:
  • API bump (Scribes of Fate).

34:
  • API bump (High Isle).

33
  • API bump.

32
  • API bump.

31
  • API bump. Waking Flame.

30
  • API bump. Blackwood.

29
  • API bump. Flames of Ambition.

28
  • Split language string management capability out of LibSFUtil version 28 for better availability.


Previously, in LibSFUtil:

LibSFUtil 16
  • Modified and tested the LibSFUtil enhanced version of SafeAddString() to support the LoadLanguage() function.
  • Fixed error in loading of default language strings in LoadLanguage().

LibSFUtil 15
  • Added new Localization function LoadLanguage().

    function sfutil.LoadLanguage(localization_strings, defaultLang)
    Add strings to the string table for the client language (or
    the default language if the client language did not have strings
    defined for it). The localization_strings parameter is a table of tables
    of localization strings, and defaultLang defaults to "en" if not
    provided.

    An example of a localization_strings table might be:

    MyAddon.localization_strings = {
    ["en"] = {
    MYADDON_RANDOM_TEXT = "This is a test",
    MYADDON_SPECIAL_TEXT = "This will only be in English",
    },
    ["de"] = {
    MYADDON_RANDOM_TEXT = "Dies ist ein Test",
    },
    ["fr"] = {
    MYADDON_RANDOM_TEXT = "C'est un test",
    },
    }

    Then the sfutil.LoadLanguage(MyAddon.localization_strings, "en") would load
    the appropriate strings table for the client language, or the "en" strings
    if there isn't a table for the language (say it was "jp").
    If there is a table for the current language, but there is not a string
    defined in it, then the string for the default language will be used instead.
Optional Files (0)


Archived Files (9)
File Name
Version
Size
Uploader
Date
36
6kB
Shadowfen
05/27/23 04:18 PM
35
6kB
Shadowfen
03/12/23 07:11 PM
34
6kB
Shadowfen
06/05/22 05:30 PM
33
6kB
Shadowfen
03/12/22 03:18 PM
32
6kB
Shadowfen
11/04/21 09:04 PM
31
53kB
Shadowfen
08/27/21 01:52 PM
30
6kB
Shadowfen
04/30/21 01:51 PM
29
6kB
Shadowfen
03/07/21 09:41 PM
28
6kB
Shadowfen
11/11/20 02:08 PM


Post A Reply Comment Options
Unread 11/11/20, 02:52 PM  
Baertram
Super Moderator
 
Baertram's Avatar
ESOUI Super Moderator
AddOn Author - Click to view AddOns

Forum posts: 4940
File comments: 6015
Uploads: 78
Thanks for the clarification.
Report comment to moderator  
Reply With Quote
Unread 11/11/20, 02:31 PM  
Shadowfen
AddOn Author - Click to view AddOns

Forum posts: 83
File comments: 752
Uploads: 15
Originally Posted by Baertram
Hi Shadowfen,
I do not seem to understand what this library is doing. What is different from the en.lua, de.lua, zh.lua files you'd normally use to define the localized texts + added to the txt manifest file, like your described it in this's lib's description too?

Does LibLanguage.LoadLanguage(AC_localization_strings,"en") handle the creation of the string numbers you have added to your AC_localization_strings table, so that GetString is able to use these constants afterwards?
Or is there any other benefit this lib provides?
Yes, GetString() works with these string table keys. I have been using it in all of my addons that are localized - it is just that the functionality was bundled with everything else inside my LibSFUtils library. I decided to split it into a separate library to help others who might want to use it.

If you look at different addons, some do tables like I have described. Some put the strings in calls to SafeAddString() (or other function). And some have lua code at the bottom of the localization file to loop through the table to load them into the string tables. All of these additions/specializations make it more difficult for translators to provide you with new languages because of the content that does not actually have anything to do with the language they are providing.

This library simplifies the syntax for the strings as much as possible and provides a single function call to allow you to load your string tables for the current language with the backing of a default language if one of the strings is missing from the client language. Also, by this simplification, the addition of a new language is as simple as including the new language file in the addon package (as long as the table is well-formed, it will work).

It also protects the addon author from the intricacies of getting the strings loaded properly while avoiding some of ZOS's "surprising" behavior - such as string versioning not actually working correctly in the simplest usage of their functions. This library provides an enhanced version of SafeAddString (does not overwrite the original) to avoid the known string versioning problem.

And, yes, it creates the string numbers for you if necessary.
Last edited by Shadowfen : 11/16/20 at 05:16 PM.
Report comment to moderator  
Reply With Quote
Unread 11/11/20, 02:19 PM  
Baertram
Super Moderator
 
Baertram's Avatar
ESOUI Super Moderator
AddOn Author - Click to view AddOns

Forum posts: 4940
File comments: 6015
Uploads: 78
Hi Shadowfen,
I do not seem to understand what this library is doing. What is different from the en.lua, de.lua, zh.lua files you'd normally use to define the localized texts + added to the txt manifest file, like your described it in this's lib's description too?

Does LibLanguage.LoadLanguage(AC_localization_strings,"en") handle the creation of the string numbers you have added to your AC_localization_strings table, so that GetString is able to use these constants afterwards?
Or is there any other benefit this lib provides?
Last edited by Baertram : 11/11/20 at 02:20 PM.
Report comment to moderator  
Reply With Quote
Post A Reply



Category Jump: