Strip Color Codes (HEX) from TranslationStrings

Please post any questions about developing your plugin here. Please use the search function before posting!
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Strip Color Codes (HEX) from TranslationStrings

Postby decompile » Tue Apr 07, 2020 6:39 pm

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
Sam
Senior Member
Posts: 100
Joined: Tue Jul 03, 2018 3:00 pm
Location: *DELETED*
Contact:

Re: Strip Color Codes (HEX) from TranslationStrings

Postby Sam » Wed Apr 08, 2020 1:07 pm

http://wiki.sourcepython.com/developing ... get_string

Try:

Syntax: Select all

def strip_colors(message):
return RE_STRIP_COLORS.sub('', message.get_string())
Last edited by Sam on Wed Apr 08, 2020 1:10 pm, edited 1 time in total.
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Re: Strip Color Codes (HEX) from TranslationStrings

Postby satoon101 » Wed Apr 08, 2020 2:29 pm

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.
Image
User avatar
L'In20Cible
Project Leader
Posts: 1533
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Strip Color Codes (HEX) from TranslationStrings

Postby L'In20Cible » Wed Apr 08, 2020 3:14 pm

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)
decompile
Senior Member
Posts: 416
Joined: Sat Oct 10, 2015 10:37 am
Location: Germany
Contact:

Re: Strip Color Codes (HEX) from TranslationStrings

Postby decompile » Thu Apr 09, 2020 3:43 am

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.

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 14 guests