I understand the motivation is to have a uniform style for all plugins, but as it stands the LangStrings and TranslationStrings classes are both lacking and restricting. I've highlighted some of the key points I have in mind:
- LangStrings attempts to always load from TRANSLATION_PATH directory. You can get around it without any modifications to the code, but it's still there in the __init__, deceiving people.
- LangStrings is built to only operate on .ini files. You have to build your own classes from scratch for any other types, there's no common interface. Adding your own types should be as easy as subclassing a common base class and providing your own parsing.
- TranslationStrings supports no subsections, modifications, or join operations. Say I have one translation for a food's name, and one translation for "My favourite food is {food_name}". How would I join these two messages and send them to a player?
1. LangStrings should receive a base class that implements all the common functionality, and LangStrings would only implement the .ini functionality. You could easily subclass the base class and create your own parsers for different types.
2. TranslationStrings should ideally be completely rewritten to support a tree hierarchy.
I've personally implemented the following method for TranslationStrings on my server:
Syntax: Select all
def __add__(self, other):
"""Join two TranslationStrings together as if they were strings.
:param dict other: The TranslationStrings to append
:return: A new, joined TranslationStrings instance.
:rtype: TranslationStrings
"""
# Create a copy of self as a result TranslationStrings
result = TranslationStrings()
result.update(self)
# If adding TranslationString-like instances together
if isinstance(other, dict):
# Add all languages together
for language, value in other.items():
result[language] = result.get(language, '') + value
# If adding a simple string to a TranslationString
elif isinstance(other, str):
# Add it to all languages
for language in result:
result[language] += other
# Return the result
return result
This allows me to sum TranslationStrings and strings together however I please:
Syntax: Select all
tr = lang_strings['buy'] + ' ' + fish.name_translation + ' (' + lang_strings['cost'] + ')'
SayText2(tr).send(player, cost=fish.cost)
Clearly it's still inferior to a tree solution that would allow:
Syntax: Select all
SayText2(lang_strings['buy food']).send(food=fish.name_translation, cost=fish.cost)
But I figured I'd ask for opinions here before spending my time on implementing such functionality.
So, any thoughts?