Skip to content

KeyHintText does not support TranslationStrings in hints #109

@KirillMysnik

Description

@KirillMysnik

keyhinttext.ini

[message]
en="Hey there"

keyhinttext.py

from filters.players import PlayerIter
from messages import KeyHintText
from translations.strings import LangStrings

strings = LangStrings('keyhinttext')

def load():
    KeyHintText(strings['message']).send(
        *[player.index for player in PlayerIter()])

Exception

[SP] Caught an Exception: 
Traceback (most recent call last): 
  File '..\addons\source-python\packages\source-python\plugins\manager.py', line 77, in __missing__ 
    instance.globals['load']() 
  File '..\addons\source-python\plugins\keyhinttext\keyhinttext.py', line 11, in load 
    *[player.index for player in PlayerIter()]) 
  File '..\addons\source-python\packages\source-python\messages\base.py', line 71, in send 
    self._send(indexes, self._get_translated_kwargs(language, tokens)) 
  File '..\addons\source-python\packages\source-python\messages\base.py', line 86, in _send 
    self.bitbuf(user_message.buffer, translated_kwargs) 
  File '..\addons\source-python\packages\source-python\messages\base.py', line 397, in bitbuf 
    buffer.write_string(hint) 

Boost.Python.ArgumentError: Python argument types in 
    BitBufferWrite.write_string(BitBufferWrite, TranslationStrings) 
did not match C++ signature: 
    write_string(class bf_write {lvalue}, char const *) 

Problem code: messages/base.py#L107

    def _get_translated_kwargs(self, language, tokens):
        """Return translated and tokenized arguments."""
        translated_kwargs = AttrDict()
        for key, value in self.items():
            if isinstance(value, TranslationStrings):
                value = value.get_string(language, **tokens)

            translated_kwargs[key] = value

        return translated_kwargs

The issue is that we translate user messages by iterating over values of the particular message trying to locate TranslationStrings instances.
So this message

{
    'message': <TranslationStrings instance>
}

gets translated to

{
    'message': "Hey there"
}

But in case of KeyHintText, the hints are stored in a tuple:

{
    'hints': (<TranslationStrings instance>, )
}

And they are not translated, leading to a signature mismatching.

My suggestion is not to blindly iterate over dict in hope that we will translate something. We need to implement _get_translated_kwargs method for every stock user message class. SayText, SayText2, HintText would translate only message key; KeyHintText would translate everything in hints key, etc. Some user message classes don't even need translation (Fade, Shake or ResetHUD).

That'd be more accurate, too. Look at HudMsg:

    def __init__(
            self, message, x=-1, y=-1, color1=WHITE, color2=WHITE, effect=0,
            fade_in=0, fade_out=0, hold_time=4, fx_time=0, channel=0)

Currently we iterate over every single key of those, when the only thing we need to translate is message

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions