i18next / i18next-browser-languageDetector

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

Subdomain Detection is not Working on Localhost #263

Closed handbremse closed 2 years ago

handbremse commented 2 years ago

🚀 Feature Proposal

The subdomain detection only works on public domains. In a development area is often localhost the domain. Then the subdomain detection is not working.

Motivation

The detection should also work on localhost development.

Example

The regex detection is based on the full URL. However, this is slightly unnecessary, since it is about your domain recognition. So you can take protocols or paths out of the detection.

The window.location object provides a property for this, which only outputs the hostname.

We assume that public domains always have at least one dot in them. The remaining characters are "word characters".

Everything that is before the Publik domain and has at least 2 and up to 5 characters (word characters) is the subdomain.

For the development area there is the or option "localhost" to the Publik Domain in the RegEx.

const language = window.location.hostname.match(/^(\w{2,5})\.((\w+\.\w{2,})|localhost)/i)

The regex option "g" (global) can be removed - on the kind of regex is has no impact.

adrai commented 2 years ago

Sorry, but why should the subdomain detection make sense on localhost?

handbremse commented 2 years ago

For tests on localhost ... e.g. ReactCreateApp in default working on http://localhost:3000 http://fr.localhost:3000 is working fine. The current detection is not finding any subdomain so the default language is displayed .. first gues: The whole translation is not working.

adrai commented 2 years ago

Would you like to send a Pull Request to address this?

adrai commented 2 years ago

v6.1.5