I am not looking for help with creating dictionaries (English-French) stc. I’m not that clever.
I am looking to implement translation in an Add-On Report.
The only thing I have been able to translate is the date, and I think Gramps does that for me. Is there some kind soul who knows how to do it, who could give me a few pointers.
Then we can link to specific lines in the code on GitHub and how to make the strings work with Weblate translation. Whether that means tagging the string to be translateable with Weblate translation or finding an existing string in the weblate that can be used instead.
And then maybe someone can run through examples of translating a new string to a existing language dictionary or create a new language dictionary.
Existing reports - no. I am trying to improve reports so existing ones will not do.
In Gramps Modules
from gramps.gen.const import GRAMPS_LOCALE as glocale
try:
_trans = glocale.get_addon_translator(file)
except ValueError:
_trans = glocale.translation
_ = _trans.gettext
In the add_menu_options I have this which gives rise to a box on the menu of the report asking for a language
locale_opt = stdoptions.add_localization_option(menu, “Report Options”)
In the init()
self.set_locale(self.menu.get_option_by_name(‘trans’).get_value())
And in the program
tagcheck = _(“Failed”) always outputs Failed
The narrative web is a good example.
If you want to use specific language for a report, You have to do the following:
the_lang=self.menu.get_option_by_name(‘trans’).get_value()
self.rlocale = self.set_locale(the_lang) # The lang you want to use (i.e. fr, de_AT...)
self._ = self.rlocale.translation.sgettext
... and then you can use:
self._("Your message")
@GBaynes You’re getting good guidance already and in addition to that, here is a good place to start in the Gramps dev wiki: Writing a plugin - Internationalize it. This para links to other pages which have much more detail.
Thanks for the help. I am definitely making progress. I was wondering about the “(" and "self.(”. But I think you have answered it. Thanks again.
George Baynes
Off topic but I had noticed this use of the underscore in Gramps. I saw quite recently his implementation of the style: _ = method to do the translation
This means that the generally agreed-upon usage or convention by python programmers of the underscore as a substitution variable - e.g. to unpack something: a, _ = (“x”, “y”) - has been hijacked to assign a translation function to it, with the risk, I suppose, that entails.
In gramps this has another meaning. It is used to replace a fonction name by something shorter.
When we use _(“string”), the real method shoud be glocale.translation.sgettext(“string”)
It is more readable to use the “_” method. We could have use other names as _pd for place_display, _nd for name_display…
When used in reports, I think it might be faster if all the translations are done at once creating a dictionary, then just using a dictionary lookup when outputting. The number of words is limited as only headings and english words used by Gramps within the database are required to be translated.
This is only applicable to the report output, not the options.
I think this is how it works.
For each addon, you have a po directory in which you have a file by translated language. If this file does not exist, you don’t have the messages translated.
I am not sure I made myself clear. If we do this in init().
then substitution of individual words remove the overhead involved in calling the translation function each time
word = (“Gramps ID”, “Description”, “Mime Type”, “Media Path”, “Privacy”,
“Attributes”, “Tags”, “Notes”, “Citations”, “People”, “Date”, “Unknown”,
“Public”, “Private”, “None”, “Copyright”, ‘File does not exist’)
self.tword = {}
for each in word:
teach = self._(each)
self.tword[each] = teach
translating a word is merely a case of interrogating the dictionary.
Still talking about translations…
Does Gramps translate things like “Note Type” which stored within gramps as a number but output as a string. Similarly with “Event Type” and probably other “Types”
Last 2 questions hopefully.
Does the Translation Object, in Reports, affect Gramps translation?
If not is there a way to force it to use the selected language?
Those options do not affect the language used by Gramps when translating things like “Note Type”
“self.underline” does use the option language
but “underline” used by Gramps continues to use the Operating System language of choice.