i18next / i18next-browser-languageDetector

language detector used in browser environment for i18next
MIT License
873 stars 90 forks source link

How to just accept /de/ and /en/ from path? #210

Closed mitchobrian closed 4 years ago

mitchobrian commented 4 years ago

I have searched a lot but nothing found. Could you provide me an example for how to validate the pattern from path.

Example: example.de/de/team should get de Example: example.de/en-EN/team should get de-EN But Example: example.de/blog/post1 shouldn' get blog

const detectionOptions = {
  order: ["path", "cookie"],
  lookupFromPathIndex: 0,
};

I need a validation in changeLanguage to check if the read path is a correct lang.

adrai commented 4 years ago

and what should "example.de/blog/post1" get?

jamuhl commented 4 years ago

there is no validation...either you have languages in path / or not -> be consistent

mitchobrian commented 4 years ago

and what should "example.de/blog/post1" get?

it should get blog and this should be invalid because it's no language - so then I expected the cookie will be selected or we use the fallback.

mitchobrian commented 4 years ago

there is no validation...either you have languages in path / or not -> be consistent

consistency is always good ;) but I just think about to have a complete page merged with a blog which isn't language dependent.

adrai commented 4 years ago

I think, It should at least fallback to your fallbackLng... isn't this the case?

mitchobrian commented 4 years ago

I think, It should at least fallback to your fallbackLng... isn't this the case?

Unfortunately no, it recognizes "blog" as a path.

adrai commented 4 years ago

Yes, this is expected. But my question was: which language is shown in your app? because lng=blog can not be found i18next should use your fallbackLng (i18next option)

mitchobrian commented 4 years ago

Yes, this is expected. But my question was: which language is shown in your app? because lng=blog can not be found i18next should use your fallbackLng (i18next option)

Ah okay, it falls back properly to de. But the var i18next.language is set to blog - how can I fix this?

jamuhl commented 4 years ago

there are two things:

i18next.language -> always what comes from detector and was saved to any detector cache i18next.languages -> an array of languages that are use to lookup translations -> in your case ['blog', 'fallback']

mitchobrian commented 4 years ago

But i18next.languages should just expect: ["de", "en"]

adrai commented 4 years ago

no, i18next.languages are all languages to lookup the translation... blog is the one detected by the language detector, and de is your fallbackLng, so ["blog", "de"]...

adrai commented 4 years ago

try to set checkWhitelist to true and define a whitelist

mitchobrian commented 4 years ago

try to set checkWhitelist to true and define a whitelist

Thanks a lot! That exactly what i was looking for! https://github.com/i18next/i18next-browser-languageDetector/issues/148#issuecomment-353372087

mitchobrian commented 4 years ago

https://www.i18next.com/misc/migration-guide