plone / plone.api

The Plone API
https://6.docs.plone.org/plone.api
Other
86 stars 53 forks source link

api.portal.translate fails with country-specific language codes #524

Closed ericof closed 9 months ago

ericof commented 9 months ago

Background

Plone, internally (plone.app.multilingual, GenericSetup), uses the <language>-<lowercase two-letter country code> format. It also stores the value on the language field of Dexterity content items. On the other hand, locales use the <language>_<uppercase two-letter country code>.

plone.api support

plone.api.portal.translate uses the localesformat, which requires the developer to first manually convert frompt-brtopt_BR` to get the correct translation.

Example code

>>> api.portal.translate("Page", lang="pt-br")
'Page'
>>>  api.portal.translate("Page", lang="pt_BR")
'Página'

Suggested fix

In plone.api.portal.translate, detect if the language code uses the plone.app.multilingual|GenericSetup format and convert it to the locales format.