开发者

What is the best way to have multi-language "log messages" in Django?

In my Django application users interact via a series of web forms and a log is generated. The log may, for example, look something like this:

  1. Battle has begun!
  2. Attacker (Elver) used Whirlwind. It was super effective!
  3. Defender (Angeleyes) built a moat. Defense +5.
  4. A storm has broken out. Attacker (Elver) misses a turn.

The problem is that this log should be displayed in other languages as well. And if a new language is added, previous log messages must automatically also display in the new language.

As you can see there are multiple types of messages (event without params, event with params, attack event, construction event, etc.)

What is the best data structure to store these in so that they would be easily translatable based on simple templates?

My thinking originally was to build a separate model for each event. For example class AttackEvent(models.Model) with fields attacker, spell, result. But these would have to refer back to some battle so that it would be easy to query for all of them and sort them as well. Could they all refer back to that battle using a ForeignKey with the same related_name? Probably not.

Another option would be to have a huge GenericLogEvent class that has an insane number of params with m开发者_如何学Pythonost of them blank=True, null=True. and then we switch based on eventType.

What's the best way here?


I’d store the message and the parameters (have a look at django-picklefield) in your log message model, and use Django’s built in translation stuff to translate messages into other languages.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜