I build a multilingual web app using Python and webapp2.
I have an object called Tag, which has translations to multiple languages. For this reason, I have created the following models:
class Language(ndb.Model):
code = ndb.StringProperty()
name = ndb.StringProperty(indexed=False)
class MultilingualText(ndb.Model):
language = ndb.KeyProperty(kind=Language)
text = ndb.TextProperty(indexed=False)
class Tag(ndb.Model):
translations = ndb.StructuredProperty(MultilingualText, repeated=True, indexed=False)
I would like to ask if this is the correct way to do such task, and how this structure can be used along with WTForms for validation, etc.
Thanks a lot in advance!
I think the best implementation can change depending on your goal and my answer here may not fulfill your needs.
For Language
class, I would rather not use datastore for this purpose. I would use babel.Locale
for determining display names.
As Tim said in the comment, I prefer using language code as the entity key. Here is an example Tag
implementation, assuming every Tag
needs a urlsafe slug.
def get_urlsafe_slug_from_tag(tag_text):
# ...
# ...
class Slug(ndb.Model):
# use urlsafe slug as the key_name
# You can optionally use the property bellow.
available_translations = ndb.StringProperty(repeated=True)
class Tag(ndb.Model):
# use language code as the key_name
text = ndb.TextProperty()
When a tag is newly created, I will create two entities; a Slug
entity with a unique urlsafe string (slug) for that tag as the key, as well as a Tag
entity with the language code as the key and this Slug
entity as its parent.
In this example, there is a property named available_translations, which will allow you to negotiate with the users language choices, and even execute a query which will return Slugs with a translation for a specified language (e.g. list Slugs with Japanese translation).
For WTForm validation, can you tell me how you want to validate the post data? I think you will be able to get better answer if you share your detailed needs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With