Skip to content
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

i18n -with subdirectories, like www.site.com/en #1369

Open
heyt0ny opened this issue Oct 1, 2019 · 3 comments

Comments

@heyt0ny
Copy link

commented Oct 1, 2019

Hello!

I'm using i18n middleware for lang localization and currently my urls look like www.site.com/?lang=en-US. According to many sources (e.g. https://webmasters.stackexchange.com/questions/403/how-should-i-structure-my-urls-for-both-seo-and-localization) it's much better to use subdirectories instead of query params. So how to make

www.site.com/?lang=en-US -> www.site.com/en-US/

Thanks!

@heyt0ny heyt0ny added the type:idea label Oct 1, 2019
@heyt0ny heyt0ny changed the title i18n - like www.site.com/en i18n -with subdirectories, like www.site.com/en Oct 1, 2019
@kataras

This comment has been minimized.

Copy link
Owner

commented Oct 1, 2019

Hello @heyt0ny,

What i18n middleware are you using?

With both of them you can specify the way that a language is accepted.
See for example the kataras/iris/middleware/i18n will try to get the language first by language's context's key(can be customized too).

iris/configuration.go

Lines 717 to 718 in df88227

// Defaults to "iris.language"
TranslateLanguageContextKey string `json:"translateLanguageContextKey,omitempty" yaml:"TranslateLanguageContextKey" toml:"TranslateLanguageContextKey"`

langKey := ctx.Application().ConfigurationReadOnly().GetTranslateLanguageContextKey()
language := ctx.Values().GetString(langKey)

setLangMiddleware := func(ctx iris.Context){
    langKey := ctx.Application().ConfigurationReadOnly().GetTranslateLanguageContextKey()
    languageByPath := ctx.Params().Get("lang") // see {lang}
    ctx.Values().Set(langKey, languageByPath)
    ctx.Next()
}
// app or per group (routes := app.Party; routes.Use...)
app.Use(setLangMiddleware)
app.Use(theI18nMiddlewareInstance)
app.Get("/{lang}/path", yourHandler)
@heyt0ny

This comment has been minimized.

Copy link
Author

commented Oct 1, 2019

Thank you.

I have 2 questions though:

  1. How to apply {lang} to a lot of urls:
    Imagine I have 2 type of urls (first A without locale and second B with local):
    www.test.com/sitemap.xml (A) and www.test.com/en-US/some-page (B). If I create

A) appAssets := app.Party("/") and put A into it
B) appMultilang := app.Party("/{lang}") and put B into it

it won't understand A url and will think that {lang} = sitemap.xml. I don't think it's a good practice to check whether {lang} is one of locales, and if's not -> redirect to A. How to properly handle that?

  1. if the user enters www.test.com/some-page - he will be assigned some {lang} based on some logic (that I define). How to do then this redirection (knowing {lang} value) in iris to make url look like www.test.com/{lang}/some-page?
@kataras

This comment has been minimized.

Copy link
Owner

commented Oct 4, 2019

@heyt0ny You are welcome.You can do it understand by mapping the locale keys with the param and if it is NOT matching then redirect to the default (en-us or without param) otherwise handle the request with the specific locale. You can make use of ctx.Values() inside the app's middleware and call the necessary handlers.

However, you can describe us the net/http (or other framework at any language) way because it depends on what fits you the most, I can help you convert everything to Iris.

If you don't have any idea about it, give me time until tomorrow and I will provide such a feature (if necessary).


Please vote here as well whenever you have time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.