martenframework / marten

The pragmatic web framework.
https://martenframework.com
MIT License
399 stars 21 forks source link

Add support for localized route paths #52

Open ellmetha opened 1 year ago

ellmetha commented 1 year ago

Description

The framework should let users easily configure localized routes. There are two types of localized routes to consider:

  1. localized routes whose paths are translated (eg. /cars and /voitures)
  2. localized routes whose paths are translated and that are prefixed by the current locale (eg. /en/cars and /fr/voitures)

In order to let users define such routes, let's add the ability to leverage a #localized block when defining routes. For example:

Marten.routes.draw do
  localized(prefix: true) do
    path "routes.article_list", ArticleListHandler, name: "article_list"
    path "routes.article_detail", ArticleDetailHandler, name: "article_detail"
    path "routes.my_app", MyApp::ROUTES, name: "my_app"
  end
end

This method should accept the following arguments:

Using #localized would instruct the routing map that the wrapped path must be localized and that their actual "paths" must be treated as localization paths. For example, the associated translations could look like this:

en:
  routes:
    article_list: /articles
    article_detail: /articles/<pk:int>
    my_app: /my-app

When generating the full URL of localized routes, the associated translations should be used in order to generate the localized URL and (optionally) prefix it with the current locale.

kates commented 2 months ago

@ellmetha I've started some work on this. I needed a few clarifications.

ellmetha commented 1 month ago

@kates Thanks for looking into that! To answer your question, the language extracted from the route path should always have precedence.