Merge pull request #3757 from JohniFi/add-internationalization

Add internationalization (i18n via gettext)
This commit is contained in:
Griatch 2025-04-26 12:11:55 +02:00 committed by GitHub
commit 532ccea050
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 40 additions and 34 deletions

View file

@ -1382,13 +1382,13 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
if obj.has_account: if obj.has_account:
if home: if home:
string = "Your current location has ceased to exist," string = _(
string += " moving you to (#{dbid})." "Your current location has ceased to exist, moving you to (#{dbid})."
obj.msg(_(string).format(dbid=home.dbid)) )
obj.msg(string.format(dbid=home.dbid))
else: else:
# Famous last words: The account should never see this. # Famous last words: The account should never see this.
string = "This place should not exist ... contact an admin." obj.msg(_("This place should not exist ... contact an admin."))
obj.msg(_(string))
obj.move_to(home, move_type="teleport") obj.move_to(home, move_type="teleport")
@classmethod @classmethod
@ -1793,9 +1793,9 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
exits = self.filter_visible(self.contents_get(content_type="exit"), looker, **kwargs) exits = self.filter_visible(self.contents_get(content_type="exit"), looker, **kwargs)
exit_names = (exi.get_display_name(looker, **kwargs) for exi in exits) exit_names = (exi.get_display_name(looker, **kwargs) for exi in exits)
exit_names = iter_to_str(_sort_exit_names(exit_names)) exit_names = iter_to_str(_sort_exit_names(exit_names), endsep=_(", and"))
e = _("Exits")
return f"|wExits:|n {exit_names}" if exit_names else "" return f"|w{e}:|n {exit_names}" if exit_names else ""
def get_display_characters(self, looker, **kwargs): def get_display_characters(self, looker, **kwargs):
""" """
@ -1812,10 +1812,10 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
self.contents_get(content_type="character"), looker, **kwargs self.contents_get(content_type="character"), looker, **kwargs
) )
character_names = iter_to_str( character_names = iter_to_str(
char.get_display_name(looker, **kwargs) for char in characters (char.get_display_name(looker, **kwargs) for char in characters), endsep=_(", and")
) )
c = _("Characters")
return f"|wCharacters:|n {character_names}" if character_names else "" return f"|w{c}:|n {character_names}" if character_names else ""
def get_display_things(self, looker, **kwargs): def get_display_things(self, looker, **kwargs):
""" """
@ -1841,8 +1841,9 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
thing = thinglist[0] thing = thinglist[0]
singular, plural = thing.get_numbered_name(nthings, looker, key=thingname) singular, plural = thing.get_numbered_name(nthings, looker, key=thingname)
thing_names.append(singular if nthings == 1 else plural) thing_names.append(singular if nthings == 1 else plural)
thing_names = iter_to_str(thing_names) thing_names = iter_to_str(thing_names, endsep=_(", and"))
return f"|wYou see:|n {thing_names}" if thing_names else "" s = _("You see")
return f"|w{s}:|n {thing_names}" if thing_names else ""
def get_display_footer(self, looker, **kwargs): def get_display_footer(self, looker, **kwargs):
""" """
@ -2141,7 +2142,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
puppeting this Object. puppeting this Object.
""" """
self.msg(f"You become |w{self.key}|n.") self.msg(_("You become |w{key}|n.").format(key=self.key))
self.account.db._last_puppet = self self.account.db._last_puppet = self
def at_pre_unpuppet(self, **kwargs): def at_pre_unpuppet(self, **kwargs):
@ -2320,7 +2321,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
if msg: if msg:
string = msg string = msg
else: else:
string = "{object} is leaving {origin}, heading for {destination}." string = _("{object} is leaving {origin}, heading for {destination}.")
location = self.location location = self.location
exits = [ exits = [
@ -2332,9 +2333,9 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
mapping.update( mapping.update(
{ {
"object": self, "object": self,
"exit": exits[0] if exits else "somewhere", "exit": exits[0] if exits else _("somewhere"),
"origin": location or "nowhere", "origin": location or _("nowhere"),
"destination": destination or "nowhere", "destination": destination or _("nowhere"),
} }
) )
@ -2405,9 +2406,9 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
mapping.update( mapping.update(
{ {
"object": self, "object": self,
"exit": exits[0] if exits else "somewhere", "exit": exits[0] if exits else _("somewhere"),
"origin": origin or "nowhere", "origin": origin or _("nowhere"),
"destination": destination or "nowhere", "destination": destination or _("nowhere"),
} }
) )
@ -2671,9 +2672,11 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
""" """
if not target.access(self, "view"): if not target.access(self, "view"):
try: try:
return "Could not view '%s'." % target.get_display_name(self, **kwargs) return _("Could not view '{target_name}'.").format(
target_name=target.get_display_name(self, **kwargs)
)
except AttributeError: except AttributeError:
return "Could not view '%s'." % target.key return _("Could not view '{target_name}'.").format(target_name=target.key)
description = target.return_appearance(self, **kwargs) description = target.return_appearance(self, **kwargs)
@ -2798,7 +2801,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
# TODO: This if-statment will be removed in Evennia 1.0 # TODO: This if-statment will be removed in Evennia 1.0
return True return True
if not self.access(dropper, "drop", default=False): if not self.access(dropper, "drop", default=False):
dropper.msg(f"You cannot drop {self.get_display_name(dropper)}") dropper.msg(_("You cannot drop {obj}").format(obj=self.get_display_name(dropper)))
return False return False
return True return True
@ -2910,15 +2913,15 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
# whisper mode # whisper mode
msg_type = "whisper" msg_type = "whisper"
msg_self = ( msg_self = (
'{self} whisper to {all_receivers}, "|n{speech}|n"' _('{self} whisper to {all_receivers}, "|n{speech}|n"')
if msg_self is True if msg_self is True
else msg_self else msg_self
) )
msg_receivers = msg_receivers or '{object} whispers: "|n{speech}|n"' msg_receivers = msg_receivers or _('{object} whispers: "|n{speech}|n"')
msg_location = None msg_location = None
else: else:
msg_self = '{self} say, "|n{speech}|n"' if msg_self is True else msg_self msg_self = _('{self} say, "|n{speech}|n"') if msg_self is True else msg_self
msg_location = msg_location or '{object} says, "{speech}"' msg_location = msg_location or _('{object} says, "{speech}"')
msg_receivers = msg_receivers or message msg_receivers = msg_receivers or message
custom_mapping = kwargs.get("mapping", {}) custom_mapping = kwargs.get("mapping", {})
@ -2927,7 +2930,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
if msg_self: if msg_self:
self_mapping = { self_mapping = {
"self": "You", "self": _("You"),
"object": self.get_display_name(self), "object": self.get_display_name(self),
"location": location.get_display_name(self) if location else None, "location": location.get_display_name(self) if location else None,
"receiver": None, "receiver": None,
@ -2943,7 +2946,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
if receivers and msg_receivers: if receivers and msg_receivers:
receiver_mapping = { receiver_mapping = {
"self": "You", "self": _("You"),
"object": None, "object": None,
"location": None, "location": None,
"receiver": None, "receiver": None,
@ -2970,7 +2973,7 @@ class DefaultObject(ObjectDB, metaclass=TypeclassBase):
if self.location and msg_location: if self.location and msg_location:
location_mapping = { location_mapping = {
"self": "You", "self": _("You"),
"object": self, "object": self,
"location": location, "location": location,
"all_receivers": ", ".join(str(recv) for recv in receivers) if receivers else None, "all_receivers": ", ".join(str(recv) for recv in receivers) if receivers else None,
@ -3195,7 +3198,7 @@ class DefaultCharacter(DefaultObject):
""" """
if account and cls.objects.filter_family(db_key__iexact=name): if account and cls.objects.filter_family(db_key__iexact=name):
return f"|rA character named '|w{name}|r' already exists.|n" return _("|rA character named '|w{name}|r' already exists.|n").format(name=name)
def basetype_setup(self): def basetype_setup(self):
""" """
@ -3499,7 +3502,9 @@ class ExitCommand(_COMMAND_DEFAULT_CLASS):
""" """
if self.obj.destination: if self.obj.destination:
return " (exit to %s)" % self.obj.destination.get_display_name(caller, **kwargs) return _(" (exit to {destination})").format(
destination=self.obj.destination.get_display_name(caller, **kwargs)
)
else: else:
return " (%s)" % self.obj.get_display_name(caller, **kwargs) return " (%s)" % self.obj.get_display_name(caller, **kwargs)

View file

@ -36,6 +36,7 @@ from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
from django.utils.text import slugify from django.utils.text import slugify
from django.utils.translation import gettext as _
import evennia import evennia
from evennia.locks.lockhandler import LockHandler from evennia.locks.lockhandler import LockHandler
@ -892,7 +893,7 @@ class TypedObject(SharedMemoryModel):
""" """
if self.location == looker: if self.location == looker:
return " (carried)" return _(" (carried)")
return "" return ""
def at_rename(self, oldname, newname): def at_rename(self, oldname, newname):