How do I internationalize exception messages? Currently, I am using resourcestring for store the messages, but I am not sure if it translates the messages as well. Something like the image below :D
I'm not a big fan of resource strings, because it makes it more difficult to understand the code. I prefer dxGetText instead. But resource strings do work.
Yup and the problem with your code is that the translation does not work anymore when someone decides to "fix" a typo in the string literal - been there, done that.
In my book if the string that gets shown has any meaning to the understanding of the code, something is wrong.
Stefan Glienke if the string that gets shown should not have any meaning to the understanding of the code, that would mean that you can't use format for strings that get translated. If somebody botches the translation by removing the superfluous % or "translates" the s e.g. to German z for Zeichenkette the code breaks.
(BorCoDero had such a bug in the error message "list index out of range (%d)" btw., they forgot to actually pass it to format.)
Thomas Mueller BTW, GetText also converts into resource strings and hooking into resource string loading. Resourcestrings can be changed at run time by casting them to pResStringRec and changing its identifier under lock by Virtualprotect.
IMHO exception messages should be in plain English, then handled as expected by the UI level into translated messages, according to the context. Exceptions should not reach the end-user, but in the form of "detailed technical information": UI should display a clear and intuitive translated text, asking for further user action. For translating this end-user error messages, I usually use resource strings (and our https://github.com/synopse/mORMot/blob/master/SQLite3/mORMoti18n.pas on-the-fly translation system, based on plain text files).
Resourcestrings should work: http://docwiki.embarcadero.com/RADStudio/XE8/en/Internationalization_and_Localization
ReplyDeleteI'm not a big fan of resource strings, because it makes it more difficult to understand the code. I prefer dxGetText instead. But resource strings do work.
ReplyDeleteI'm very happy with tsiLang. It's a commercial product, support all platforms and is very powerfull. All info on www.tsilang.com
ReplyDelete+1 for dxGetText, Resourcestrings are translated
ReplyDeleteI am using tsilang too, and happy with it.
ReplyDeleteThomas Mueller I really wonder what is difficult to understand on code like this:
ReplyDeleteraise EProgrammerNotFound.CreateFmt(SProgrammerNotFound, [programmer.Name]);
(apart from the fact that I could name the resourcestring even better to make clear it takes one string fmt arg)
Stefan Glienke in my book it's easier to understand something like this:
ReplyDeleteraise ProgrammerNotFound.CreateFmt(_('Programmer %s not found'), programmer.Name);
Especially since resource strings tend to be defined somewhere else in the code, possibly in a different unit.
Yup and the problem with your code is that the translation does not work anymore when someone decides to "fix" a typo in the string literal - been there, done that.
ReplyDeleteIn my book if the string that gets shown has any meaning to the understanding of the code, something is wrong.
Stefan Glienke if the string that gets shown should not have any meaning to the understanding of the code, that would mean that you can't use format for strings that get translated. If somebody botches the translation by removing the superfluous % or "translates" the s e.g. to German z for Zeichenkette the code breaks.
ReplyDelete(BorCoDero had such a bug in the error message "list index out of range (%d)" btw., they forgot to actually pass it to format.)
I uses i18n package. http://www.delphiarea.com/products/delphi-packages/i18n/
ReplyDeleteIt provide some special component like a calendar and some more. And it free and open-source :)
Thomas Mueller BTW, GetText also converts into resource strings and hooking into resource string loading.
ReplyDeleteResourcestrings can be changed at run time by casting them to pResStringRec and changing its identifier under lock by Virtualprotect.
IMHO exception messages should be in plain English, then handled as expected by the UI level into translated messages, according to the context. Exceptions should not reach the end-user, but in the form of "detailed technical information": UI should display a clear and intuitive translated text, asking for further user action.
ReplyDeleteFor translating this end-user error messages, I usually use resource strings (and our https://github.com/synopse/mORMot/blob/master/SQLite3/mORMoti18n.pas on-the-fly translation system, based on plain text files).
All my Resourcestrings goes to a type of Data Module or a special unit. Simple that.
ReplyDelete