-
-
Notifications
You must be signed in to change notification settings - Fork 251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Multilingual support #656
Closed
Closed
Multilingual support #656
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GeoSander
force-pushed
the
multilingual
branch
2 times, most recently
from
March 5, 2021 22:36
75a8482
to
d36895c
Compare
GeoSander
added a commit
to GeoSander/geocore-pygeoapi
that referenced
this pull request
Mar 15, 2021
GeoSander
force-pushed
the
multilingual
branch
5 times, most recently
from
March 17, 2021 14:10
874ee4f
to
d468835
Compare
… and plugins (wip).
* All routed API methods are now decorated by @pre_process (consistency) and no longer have a headers+format argument but a request argument (**kwargs also removed) * The pre_process decorator turns an incoming Flask/Starlette request into a generic APIRequest instance * The new APIRequest class extracts all relevant info (params, data, locale, etc.) from the request and exposes them as properties * Removed a lot of boilerplate (i.e. format checking) and wrapped that into methods * Updated server-specific API calls in each route method (pass entire request object, not headers and query params)
* Updated OpenAPI page with "l" query param * Added example translations (metadata) * Changed plugin signature: added explicit locale attribute (instead of **kwargs) * Moved locale processing to get_plugin_locale() function in l10n module * API should pass raw requested locale to plugins, locale should always be set * Fixed API tests and added APIRequest tests * Prepared utils.py for Jinja2 i18n extension * Rebased on commit b40297a and fixed compatibility with geopython#661 and geopython#662
* Fixed EDR provider signature (added locale) * Fixed EDR API routes and query function (and improved parameter-name handling) * Fixed EDR tests
* Added new translate_dict function to l10n module (+ tests) * Updated all render_j2_template calls with locale parameter * Updated pygeoapi-test-config.yml with some language structs
* support both 'language' and 'languages' property in server config and provider definitions * renamed and modified translate_dict() to more generic translate_struct() function (l10n module) * remove Content-Language header from provider responses if provider has no language support and format is json(ld) * updated tests
I note that the GEOS and PostGIS projects are using osgeo docker repository https://repo.osgeo.org/#browse/search/docker for test images (no pull rate limit there). |
Closing in lieu of #664 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Enables multilingual support for pygeoapi (core and plugins). Refer to issue #100 for discussion.
Work done
l
parameter orAccept-Language
header)Content-Language
headerhreflang
in links in configurationI've decided to push the last item to a next PR. This PR is already big enough...
The generation/processing of all API links should be done in a separate function, imho (see "Upcoming PRs" below).
What does it do?
Users can now append a
l=fr
parameter to a pygeoapi URL and pygeoapi will return the requested page in French (for example). If the user connects to pygeoapi using a browser where the language has been set to French, the requested page will automatically be rendered in French (without thel=fr
parameter), because the browser sent anAccept-Language
header along with the request. However, this can always be overridden again using thel
query parameter.Remarks
l10n
(localization) module, which turns the requested language (e.g. "fr", "fr-CH" or "fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5" into a best matching BabelLocale
object. TheseLocale
objects integrate well with Jinja2 templates using thei18n
extension;pygeoapi-config.yaml
now contains some example language structs for US English and Canadian French (more translations coming up);api.py
has undergone quite a lot of refactoring. All methods that requireRequest
object data are now decorated by the@pre_process
function, which converts the incoming Flask/StarletteRequest
into a (newly introduced)APIRequest
instance. This class processes headers and query parameters to extract the required properties for the API methods. This results in less boilerplate and cleaner API method signatures, while still being explicit;requested_locale=None
argument to the__init__
method of their plugin, and pass therequested_locale
to thesuper().__init__()
call as a second argument. This is all that needs to be done to make the plugin work again and to make it language-aware. However, it is up to the developer to implement the logic to query/process data and return it in the requested language. Please refer to the provided Sphinx documentation for more info.Follow-ups
This PR provides the foundation to make pygeoapi language-aware. However, for full language support, there are still some things that need to be done.
Upcoming PRs should address:
l=<language>
query parameter if the current page also was requested using that parameter (note: theadd_locale
function in thel10n
module can be used for that.Internal links should also set the
hreflang
property to the user-specified language. For external links, this only applies if the server speaks that language (hard to figure out).pybabel
. so they can be managed by a translation tool (e.g. Transifex);i18n
extension needs to be configured and the HTML templates need to be modified so that Jinja2 and Babel/gettext can inject the translations;l10n
module provides the necessary tools to do so;Some useful links concerning Jinja2/Babel/Transifex: