views:

133

answers:

2

I am building an application in django, that is already using a lot of hardcoded strings. They are mostly in templates, but some also in js files and a few can be found inside the code. Now every time some string needs to be changed people comes to us and we have to waste our time finding it and changing. How could I start with cleaning this up and having all those strings in separate files, that could be edited by non-programmers?

+1  A: 

What about using i18n services (gettext)? Even if you are not planning to localize your application, they provide an easy and standard way to separate strings from actual code.

Moreover, being PO quite a common standard, there are plenty of tools to edit the resource files; one of them (available also on Windows) is Poedit.

Roberto Liffredo
Yes, I know, that there I18N system in Django that is based on gettext. However, it works like this: you write messages in one natural lang in your code and provide translations elsewhere. We don't have translations yet, but we would like not to hardcode any string inside the code. And the python code is only one place. What about the javascript or html stuff?
gruszczy
The templates can use the i18n features to get strings: http://docs.djangoproject.com/en/dev/topics/i18n/#in-template-code
S.Lott
+1  A: 

We keep all hard-coded strings in a separate module. However, since you want users to modify the strings as they like, you better keep them in the database. I think a simple model with a key (an identifier of the string) and a value (string itself) field will do. Then you can develop a simple page where user selects a string by its identifier and updates it however he wants.

About how to use them in your apps, you can fetch all of them into a dict when your app starts (a proper place may be the init module) and use them accordingly.

shanyu
This is cool, especially web administration of all strings. However, is there any point in keeping also gettext and instead just having translations in the database?
gruszczy
You'd have to store translations of the strings in the database, since it is again the users who will provide them. All other strings that users won't be allowed to modify will have to be 'gettext'ed.
shanyu
OK, so coder specific strings through gettext (what kind of coder specific strings can there be?? and why should we translate them??) and all the visible for users in the database. But what about string base for javascript, which can be kept in seperate file?
gruszczy
Not exactly. Strings that users need to modify go to the db. And strings that users can't touch stay in the code, even if they are visible. If you really really have strings that users must change in the js code, maybe it is better to take them out into the db and provide them to the js code from python. I do similar things in ajax calls, placing the translated strings in the response.
shanyu
I really dislike the idea of mixing javascript and django template language, because it quickly becomes a mess. But maybe this is the only way. Still, making a file with all scripts as a template and serving it through a view seems a little strange ;-)Thanks for the help :)
gruszczy
Well, you want translatable and user-modifiable strings that reside in js code. A payoff exists between cleanness and this level of flexibility, I would say. Good luck ;)
shanyu