New Translations module available!!!

Official Announcements about Source.Python.
User avatar
satoon101
Project Leader
Posts: 2698
Joined: Sat Jul 07, 2012 1:59 am

New Translations module available!!!

Postby satoon101 » Wed Jan 23, 2013 4:18 pm

We finally have a translations module set up to send language specific messages to players!!

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'])
Now, when sending "mymessage", the correct translation will automatically be retrieved for each specific user.

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'})
Notice in the last line that we have to turn the dictionary into keywords by using **

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
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Mon Mar 02, 2015 6:36 pm

How do I format the translations? I think I'm supposed to use $key inside the translation string, but how exactly do I set the value of the key when I use the translation, for example with SayText2, or inside a menu's option?

Edit:
I just got an error even without trying to format the translation.

test.ini:

Code: Select all

[Test2]
en = "Welcome!"
fi = "Tervetuloa!"

test.py:

Syntax: Select all

from translations.strings import LangStrings
from messages import SayText2
from events import Event
from players.helpers import index_from_userid

@Event
def player_say(game_event):
userid = game_event.get_int('userid')
index = index_from_userid(userid)
my_strings = LangStrings('test')
my_message = SayText2(my_strings['Test2'])
my_message.send(index)


Error:

Code: Select all

[SP] Successfully loaded plugin 'test'.
Mahi: ASD
[libprotobuf ERROR ..\src\google\protobuf\descriptor.cc:4153] "MSG_SPLITSCREEN_T
YPE_BITS" uses the same enum value as "MSG_SPLITSCREEN_REMOVEUSER". If this is i
ntended, set 'option allow_alias = true;' to the enum definition.

[SP] Caught an Exception:
Traceback (most recent call last):
  File '..\addons\source-python\packages\source-python\messages\base.py', line 5
14, in _send_message
    parameter_name, kwargs[parameter_name])
  File '..\addons\source-python\packages\source-python\messages\types\saytext.py
', line 28, in _prepare_parameter
    parameter_name, parameter_value)
  File '..\addons\source-python\packages\source-python\messages\base.py', line 4
36, in _prepare_parameter
    parameter_value = field_converter(parameter_value)

TypeError: int() argument must be a string or a number, not 'TranslationStrings'



'{'en': 'Welcome!', 'fi': 'Tervetuloa!'}' is not a valid value for 'SayText2.ind
ex'
User avatar
L'In20Cible
Project Leader
Posts: 1534
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Mon Mar 02, 2015 7:08 pm

Syntax: Select all

my_message = SayText2(my_strings['Test2'])
Should be:

Syntax: Select all

my_message = SayText2(message=my_strings['Test2'])
As for the keywords, pass them to the send method:

Syntax: Select all

my_message.send(index, keyword='hey', keyword2='oh')
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Mon Mar 02, 2015 7:35 pm

L'In20Cible wrote:

Syntax: Select all

my_message = SayText2(my_strings['Test2'])
Should be:

Syntax: Select all

my_message = SayText2(message=my_strings['Test2'])
As for the keywords, pass them to the send method:

Syntax: Select all

my_message.send(index, keyword='hey', keyword2='oh')
Thank you very much. :)
User avatar
L'In20Cible
Project Leader
Posts: 1534
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Tue Mar 03, 2015 7:36 pm

You can set the encoding you want using the encoding keyword:

Syntax: Select all

strings = LangStrings('file', encoding='encoding')
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Tue Mar 03, 2015 9:16 pm

L'In20Cible wrote:You can set the encoding you want using the encoding keyword:[python]strings = LangStrings('file', encoding='encoding')[/python]

This did the job, with 'utf-8' :) Thank you
Edit: Never mind, I failed. It still doesn't work :/
User avatar
L'In20Cible
Project Leader
Posts: 1534
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Tue Mar 03, 2015 9:28 pm

Mahi wrote:This did the job, with 'utf-8' :) Thank you


To be honnest, using the following:[python]strings = LangStrings('file', encoding='utf-8')[/python]Is pointless since the default encoding is already defaulted to UTF-8. I think you changed the encoding of your file, right?
User avatar
L'In20Cible
Project Leader
Posts: 1534
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Tue Mar 03, 2015 9:39 pm

Then, change the encoding of your file to UTF-8. This worked just fine for me (did you miss this reply?)!
User avatar
Mahi
Senior Member
Posts: 236
Joined: Wed Aug 29, 2012 8:39 pm
Location: Finland

Postby Mahi » Tue Mar 03, 2015 9:42 pm

L'In20Cible wrote:Then, change the encoding or your file to UTF-8. This worked just fine for me (did you miss this reply?)!
I did change it through notepad's save dialog, but didn't help :/

Edit: Ugh it's all messed up, I did a system restore earlier today and apparently the Source.Python files were affected... Now it says source.pythons/packages/_core is missing :D

Edit2: Reinstalled SP, everything works. Thanks for help, my bad here :)

Return to “News & Announcements”

Who is online

Users browsing this forum: No registered users and 8 guests