Page 1 of 1

Strip Color Codes (HEX) from TranslationStrings

Posted: Tue Apr 07, 2020 6:39 pm
by decompile
How can I strip color codes from TranslationStrings?

I tried:

Syntax: Select all

RE_STRIP_COLORS = re.compile(r'\x07[a-fA-F0-9]{6}|\x08[a-fA-F0-9]{8}')

def strip_colors(message):
print(type(message))
return RE_STRIP_COLORS.sub('', message)


def tell_console(index, message):
message = strip_colors(message)

TextMsg(message, HudDestination.CONSOLE).send(index)


but i'm getting

Code: Select all

<class 'translations.strings.TranslationStrings'>

    return RE_STRIP_COLORS.sub('', message)

TypeError: expected string or bytes-like object

Re: Strip Color Codes (HEX) from TranslationStrings

Posted: Wed Apr 08, 2020 1:07 pm
by Sam
http://wiki.sourcepython.com/developing ... get_string

Try:

Syntax: Select all

def strip_colors(message):
return RE_STRIP_COLORS.sub('', message.get_string())

Re: Strip Color Codes (HEX) from TranslationStrings

Posted: Wed Apr 08, 2020 2:29 pm
by satoon101
If you do that, you will always send the default language translation to every user, which makes using translations useless. It's been a while since I have done anything SP related, but my guess is that instead of doing that, it might be easier to add translations that don't have the color codes in them and send them separately.

Re: Strip Color Codes (HEX) from TranslationStrings

Posted: Wed Apr 08, 2020 3:14 pm
by L'In20Cible
Using different strings is a possibility, another would be to inherit the message class:

Syntax: Select all

import re
from messages import TextMsg
from messages import HudDestination

RE_STRIP_COLORS = re.compile(r'\x07[a-fA-F0-9]{6}|\x08[a-fA-F0-9]{8}')

class MyTextMsg(TextMsg):
def protobuf(self, buffer, kwargs):
kwargs.message = RE_STRIP_COLORS.sub('', kwargs.message)
super().protobuf(buffer, kwargs)

def bitbuf(self, buffer, kwargs):
kwargs.message = RE_STRIP_COLORS.sub('', kwargs.message)
super().bitbuf(buffer, kwargs)

def tell_console(index, message):
MyTextMsg(message, HudDestination.CONSOLE).send(index)

Re: Strip Color Codes (HEX) from TranslationStrings

Posted: Thu Apr 09, 2020 3:43 am
by decompile
L'In20Cible wrote:Using different strings is a possibility, another would be to inherit the message class:

Syntax: Select all

import re
from messages import TextMsg
from messages import HudDestination

RE_STRIP_COLORS = re.compile(r'\x07[a-fA-F0-9]{6}|\x08[a-fA-F0-9]{8}')

class MyTextMsg(TextMsg):
def protobuf(self, buffer, kwargs):
kwargs.message = RE_STRIP_COLORS.sub('', kwargs.message)
super().protobuf(buffer, kwargs)

def bitbuf(self, buffer, kwargs):
kwargs.message = RE_STRIP_COLORS.sub('', kwargs.message)
super().bitbuf(buffer, kwargs)

def tell_console(index, message):
MyTextMsg(message, HudDestination.CONSOLE).send(index)


Works like a charm! Thanks for the quick answer.