I have this function below and I need to also check if each of these variables are none before translating.
def save(self):
# add an an if condition to check all the below variables if they are none
# if self.name_es or ..... is not None:
self.name_es = translateField(self.name, 'es') if not None
self.name_ar = translateField(self.name, 'ar')
self.description_es = translateField(self.description, 'es')
self.description_ar = translateField(self.description, 'ar')
super(City, self).save()
This is the translate function:
def translateField(text, lang):
try:
return client.translate(text, lang)
except:
pass
The problem is that I have the above in like many classes in my Model. In each save function I'm doing the same for different variables.
What can I do over here to minimize the amount of code? The '_es' and '_ar' are always appended to these variables that I need to return the output of the translation.