First things first, all translation files must be of the .ini file type and be located in your server's ../resource/source-python/translations/ folder. If you have a few different files for your addon, you can create your own directory within that directory to use.
When using LangStrings (our class used to store translations for your addon), you must use the path "from" the ../resource/source-python/translations/ directory. For this example, the file I use is my server's ../resource/source-python/translations/test.ini file:
Syntax: Select all
from translations.strings import LangStrings
# Notice we only pass "test" and not "test.ini" or the full path to test.ini
mystrings = LangStrings('test')
When LangStrings imports your strings, it automatically turns them into TranslationStrings instances. So, when setting a message's "message" attribute, you should set it to the TranslationStrings instance:
Syntax: Select all
from messages import SayText2
from translations.strings import LangStrings
mystrings = LangStrings('test')
mymessage = SayText2(mystrings['Test One'])
There will probably be times in your scripts that you will want to get a language specific message without directly sending it. For those times, you will want to utilize the following:
Syntax: Select all
from core import GameEngine
from events import Event
from players.helpers import index_from_userid
from translations.strings import LangStrings
mystrings = LangStrings('test')
@Event
def player_say(GameEvent):
userid = GameEvent.GetInt('userid')
index = index_from_userid(userid)
language = GameEngine.GetClientConVarValue(index, 'cl_language')
# Get a string with the player's language with no tokens
one = mystrings['Test One'].get_string(language)
# Get a string with the player's language using keywords
two = mystrings['Test Two'].get_string(language, token_one='test_token_one', token_two='test_token_two')
# Get a strings with the player's language using a dictionary for tokens
three = mystrings['Test Three'].get_string(language, **{'token_three': 'test_token_three', 'token_four': 'test_token_four'})
You can find a list of valid languages and their shortnames in the ../_libs/_data/languages.ini file. You can use either the language name or the shortname in your .ini files.
* Please note that when you get a LangString instance, another file will automatically be created at the same location with _server appended to the filename you pass. Please do not upload this server specific file with your addons, as the purpose of these types of files is so that the server can have their own langstrings added to your script. If you were to include the file in your upload, it would cause server owners to accidentally overwrite their server specific file defeating the purpose of the file, as they would have to re-add their langstrings.
I am sure there are probably things I missed in this tutorial, so please feel free to ask questions and make comments/suggestions.
Thanks,
Satoon