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

Different domains with multiple languages each #1161

Open
toooni opened this issue Apr 30, 2021 · 37 comments · May be fixed by #2914
Open

Different domains with multiple languages each #1161

toooni opened this issue Apr 30, 2021 · 37 comments · May be fixed by #2914

Comments

@toooni
Copy link

toooni commented Apr 30, 2021

I am not a 100% sure if this really isn't possible yet, but it seems like it isn't. We have multiple TLD's and one of them (.ch) needs to have two languages which should result in the following domain/path/locale combinations:

  • weekend4two.ch de-CH
  • weekend4two.ch/fr fr-CH
  • weekend4two.de de-DE
  • weekend4two.at de-AT
  • weekend4two.fr/fr OR weekend4two.fr fr-FR (We do not really care since the .fr TLD does only need one language)

I've tried a few configs to achieve what I need but had no success. This one was the closest:

/* nuxt.config.js */
...
i18n: {
  differentDomains: true,
  strategy: 'prefix_except_default',
  locales: [
    {
      code: 'de-CH',
      iso: 'de-CH',
      domain: 'weekend4two.ch',
    },
    {
      code: 'fr-CH',
      iso: 'fr-CH',
      domain: 'weekend4two.ch',
    },
    {
      code: 'fr-FR',
      iso: 'fr-FR',
      domain: 'weekend4two.fr',
    },
    {
      code: 'de-DE',
      iso: 'de-DE',
      domain: 'weekend4two.de',
    },
    {
      code: 'de-AT',
      iso: 'de-AT',
      domain: 'weekend4two.at',
    },
  ],
}
...
  
/* index.vue */
...
<script>
export default {
  nuxtI18n: {
    paths: {
      de: '/',
      fr: '/fr',
      'fr-CH': '/fr',
    },
  },
}
</script>
...

The seems to result in a working solution where all the domains/path exist. But when going to weekend4two.ch/fr, the locale is set to de-CH. Whatever I do, it takes the locale of the first entry with the same domain.

Describe the solution you'd like

It would be cool if the locales config in nuxt.config.js could be extended with a prefix property like this:

/* nuxt.config.js */
locales: [
  {
    code: 'de-CH',
    iso: 'de-CH',
    domain: 'weekend4two.ch',
    prefix: '', /* could be removed too */
  },
  {
    code: 'fr-CH',
    iso: 'fr-CH',
    domain: 'weekend4two.ch',
    prefix: '/fr',
  },
  ...
]

Describe alternatives you've considered

I am open to all alternatives. It may be possible that it even is possible in a way I haven't figured out yet. I am new to NUXT.

@andorfermichael
Copy link

I would appreciate that too.

lfglopes added a commit to visayasolutions/i18n-module that referenced this issue May 26, 2021
@lfglopes
Copy link

lfglopes commented May 26, 2021

Hello all,

I face the same requirement and attempted to forked this repo and add (in a quick and dirty manner) support for this.

See commit visayasolutions@af22fda

If you want to try it out

npm install https://github.com/visayasolutions/i18n-module.git#feature/domains-with-prefix

The configuration I use is the following:

  {
    code: 'nl_BE',
    file: 'nl-BE.js',
    prefix: '/nl',
    domain: 'be.app.local:3003',
  },
  {
    code: 'fr_BE',
    file: 'fr-BE.js',
    prefix: '/fr',
    domain: 'be.app.local:3003'
  }

Haven't tested it extensively, consider it a proof of concept.

I'm open to any feedback, thanks

@toooni
Copy link
Author

toooni commented Jun 1, 2021

Hi @lfglopes
I've just tried your solution with my desired config and it works perfectly! Since I am not very familiar with nuxt and the other strategies, I am not able to tell from your source if something might be problematic.

@algj
Copy link

algj commented Jun 8, 2021

This is a lot to write when there's a lot of domains (6) and couple of languages (4), in total 24 "locales".
There should be a better solution maybe, like seperate section for domains and defaultLocale for each of them.

@stale
Copy link

stale bot commented Aug 18, 2021

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Aug 18, 2021
@stale stale bot removed the stale label Aug 18, 2021
@marcmalerei
Copy link

I face the same requirements. Would be great if the solution provide by @lfglopes could be merged.

@nikolashaag
Copy link

I have the same requirements for a page that has multiple locales on each domain. This seems like a common usecase, so would be great if this can get merged!

@coeurofbear
Copy link

coeurofbear commented Nov 12, 2021

Same here, it'd be great if this gets merged! 🤞

@MarcoBomfim
Copy link

Our company has been using a forked version of the plugin for quite some time now due to this issue being staled, it would be cool to have it merged. Is there something I can do to get this moving forward?

@rchl
Copy link
Collaborator

rchl commented Jun 20, 2022

I don't see any PR that could be merged.

@MarcoBomfim
Copy link

@lfglopes think you can get a PR open with your changes? If not I can raise one, just don't want to take your credits for it 😄

@lfglopes
Copy link

lfglopes commented Jun 21, 2022

@MarcoBomfim thanks for reaching out!

I didn't open a PR back then because I assumed the code was not good enough as it is.

Which after reading the contributing guidelines and running lint on it, I can confirm :P

I can attempt some changes tomorrow and see if it works out. I'd assume one might need to update docs as well? Something else?

@lfglopes
Copy link

@MarcoBomfim please go ahead and submit a PR :)

I haven't used nuxt for a little while and I need more time than I can afford right now to fix the code.

@rchl
Copy link
Collaborator

rchl commented Jun 22, 2022

I don't want to discourage anyone but a certain level of quality is required for a PR to be accepted. The change should have tests, be added to documentation if it's a feature or behavior change, pass the lint checks and in general fit the design of the module. The last one is a bit hard to quantify so it could be discussed before the PR to avoid wasting anyone's time.

@MarcoBomfim
Copy link

Thanks folks. I'll be working on the PR mentioned above, but it may take some time since I'm slowly getting the hang of how everything works. @rchl should I move any further discussion to the PR instead?

@rchl
Copy link
Collaborator

rchl commented Jun 23, 2022

The high level design of the change could be discussed before PR to avoid wasting time on implementation that would potentially be rejected but it's up to you.

@MarcoBomfim
Copy link

My initial idea was to create a new strategy, and follow the logic for PREFIX_AND_DEFAULT under @/src/helpers/routes.js. Something like DOMAINS_AND_PREFIX or MULTIPLE_LOCALES_SINGLE_DOMAIN.

Assuming the following configuration:

i18n {
    baseUrl: 'nuxt-app.localhost',
    strategy: 'multiple_locales_single_domain',
    defaultLocale: 'en-ca',
    locales: [
      {
        code: 'en',
        iso: 'en',
        name: 'English',
        domain: 'en.nuxt-app.localhost'
      },
      {
        code: 'en-ca',
        iso: 'en-CA',
        name: 'Canadian English',
        domain: 'ca.nuxt-app.localhost',
        suffix: '/en'                                     // Required for multiple languages per domain
      },
      {
        code: 'fr-CA',
        iso: 'fr-CA',
        name: 'Canada Français',
        domain: 'ca.nuxt-app.localhost',
        suffix: '/fr'
      }
    ],
    ...
}

What needs to happen then, is that to build the localizedRoute, we would have to verify a couple of things:

  1. The route belongs to defaultLocale and is the defaultLanguage?
    If so, don't add anything to the route (ie. ca.nuxt-app.localhost/*).
    If not, we would have to include a suffix for the language (ie. ca.nuxt-app.localhost/fr/*)

  2. The route belongs to a different domain?
    If it does, the route should be built using the proper domain name. (ie en.localhost/* instead of ca.nuxt-app.localhost/en/*)

As far as I've seen walking around the module this seems to cover enough ground for a multi-lang per domain feature to be implemented. Although I have a couple of questions still:

  • Is there anything I'm missing here?
  • Does this follows the module design? Is there anything I could change here in order to be in accordance with it?

@golubvladimir
Copy link

What about my path for fix this problem - https://github.com/golubvladimir/nuxt-i18n-different-domains-and-prefixes , I use https://github.com/nuxt-community/router-module for creating custom Nuxt router.

@hoersamu
Copy link
Contributor

Any updates on this?

@Red-Asuka
Copy link

This seems to be a common use case, and it would be great if it could be supported.

@coeurofbear
Copy link

It would be great to have this supported 🙏 !

@oneseb
Copy link

oneseb commented Jun 19, 2023

We'd need exactly that as well!

@patricksengalrayan
Copy link

This is exactly what I need.

@Zavtramen
Copy link

Hello all,

I face the same requirement and attempted to forked this repo and add (in a quick and dirty manner) support for this.

See commit visayasolutions@af22fda

If you want to try it out

npm install https://github.com/visayasolutions/i18n-module.git#feature/domains-with-prefix

The configuration I use is the following:

  {
    code: 'nl_BE',
    file: 'nl-BE.js',
    prefix: '/nl',
    domain: 'be.app.local:3003',
  },
  {
    code: 'fr_BE',
    file: 'fr-BE.js',
    prefix: '/fr',
    domain: 'be.app.local:3003'
  }

Haven't tested it extensively, consider it a proof of concept.

I'm open to any feedback, thanks

Thanks a lot. It helps me! Please, keep alive https://github.com/visayasolutions/i18n-module.git#feature/domains-with-prefix

@alisspers
Copy link

My initial idea was to create a new strategy, and follow the logic for PREFIX_AND_DEFAULT under @/src/helpers/routes.js. Something like DOMAINS_AND_PREFIX or MULTIPLE_LOCALES_SINGLE_DOMAIN.

Assuming the following configuration:

i18n {
    baseUrl: 'nuxt-app.localhost',
    strategy: 'multiple_locales_single_domain',
    defaultLocale: 'en-ca',
    locales: [
      {
        code: 'en',
        iso: 'en',
        name: 'English',
        domain: 'en.nuxt-app.localhost'
      },
      {
        code: 'en-ca',
        iso: 'en-CA',
        name: 'Canadian English',
        domain: 'ca.nuxt-app.localhost',
        suffix: '/en'                                     // Required for multiple languages per domain
      },
      {
        code: 'fr-CA',
        iso: 'fr-CA',
        name: 'Canada Français',
        domain: 'ca.nuxt-app.localhost',
        suffix: '/fr'
      }
    ],
    ...
}

What needs to happen then, is that to build the localizedRoute, we would have to verify a couple of things:

  1. The route belongs to defaultLocale and is the defaultLanguage?
    If so, don't add anything to the route (ie. ca.nuxt-app.localhost/*).
    If not, we would have to include a suffix for the language (ie. ca.nuxt-app.localhost/fr/*)
  2. The route belongs to a different domain?
    If it does, the route should be built using the proper domain name. (ie en.localhost/* instead of ca.nuxt-app.localhost/en/*)

As far as I've seen walking around the module this seems to cover enough ground for a multi-lang per domain feature to be implemented. Although I have a couple of questions still:

  • Is there anything I'm missing here?
  • Does this follows the module design? Is there anything I could change here in order to be in accordance with it?

@rchl Did you have any input on the solution proposal from @MarcoBomfim ?

@rchl
Copy link
Collaborator

rchl commented Oct 4, 2023

I don't have time to work on new features like that. The bulk of the effort (not from me) goes into the Nuxt 3 version right now.

@bjerggaard
Copy link
Contributor

I am in a need for similar feature and couldn't find any up-to-date workaround so thought I would have a go for it myself.
My suggested implementation only requires setting a domainDefault: true on the default/fallback locale for each domain when using multiple domains and multiple languages on one or more of them.

It should support the various strategies except no_prefix as that does not make any sense.
You can see the changes in this commit: bjerggaard/nuxt-i18n@d610dd6
Let me know of any feedback/improvements and I will be happy to create a pull request.

This feature did break some of the differentDomains tests and I couldn't get my head around the reason for following line in the current code:

return !options.differentDomains && DefaultLocalizeRoutesPrefixable(opts)

For me, it looks like no_prefix is implied when using differentDomains in the current codebase. Setting the no_prefix strategy in those tests did resolve them.

Also posted this in following issue: https://volta.net/nuxt-modules/i18n/issues/2418

@BobbieGoede BobbieGoede added the v7 label Apr 5, 2024
corentinbailly added a commit to D3T-Distribution/i18n that referenced this issue Apr 5, 2024
corentinbailly added a commit to D3T-Distribution/i18n that referenced this issue Apr 5, 2024
@DarthGigi
Copy link

Any updates on this?

@toooni
Copy link
Author

toooni commented Apr 10, 2024

@DarthGigi This has been implemented in #2705. The release is out since last week.

I tried to migrate from our manual solution, and I am still having some use cases which I can't implement with this - so I haven't tested it properly.

@toooni toooni closed this as completed Apr 10, 2024
@DarthGigi
Copy link

@toooni Thank you for your response, however, it doesn't quite fit our use case.

We are looking for something that works as follows:

If something like this is our config:

export default defineNuxtConfig({
  // ...
  i18n: {
    locales: [
      {
        code: 'en',
      },
      {
        code: 'nl',
        domain: 'mydomain.nl',
        domainDefault: true
      },
      {
        code: 'be',
        domain: 'mydomain.be',
        domainDefault: true
      },
      {
        code: 'de',
        domain: 'mydomain.de',
        domainDefault: true
      },
      {
        code: 'fr',
        domain: 'mydomain.fr',
        domainDefault: true
      }
    ],
    strategy: 'prefix_except_default',
    differentDomains: true
  },
  // ...
})

We would like to achieve the following result:

NL domain:
https://mydomain.nl/ -> https://mydomain.nl/ (nl language)
https://mydomain.nl/en -> https://mydomain.nl/en (en language)
https://mydomain.nl/de -> https://mydomain.nl/de (de language)

https://mydomain.nl/nl -> https://mydomain.nl/nl (404)

DE domain:
https://mydomain.de/ -> https://mydomain.nl/ (nl language)
https://mydomain.de/en -> https://mydomain.nl/en (en language)
https://mydomain.de/nl -> https://mydomain.nl/de (de language)

https://mydomain.de/de -> https://mydomain.de/de (404)

etc...

Do you have an idea on how we can achieve this behavior? We tried setting an unique code for each domain locale:

{
  code: 'de-fr',
  domain: 'mydomain.de',
  domainDefault: false
},
{
  code: 'fr-de',
  domain: 'mydomain.fr',
  domainDefault: false
}

But that results in paths like domain/de-fr/page which isn't what we want.

Any help would be appreciated 😊.

@toooni
Copy link
Author

toooni commented Apr 10, 2024

@DarthGigi You are right. And this was exactly what I meant by

I am still having some use cases which I can't implement with this - so I haven't tested it properly

I will reopen the issue.

I also have use cases where I want to overwrite the prefix inside page components like this:

<script setup>
  defineI18nRoute({
    paths: {
      de: '/entdecken',
      fr: '/decouvrir', // -> this is without prefix
    }
  })
</script>

When we started, I was fairly new to nuxt. I am now pretty content that I might be able to contribute here. But I am not having much time right now. If you want, I could maybe provide you the files plugin we wrote to achieve a working solution without this module in www.weekend4two.ch. It's not very sexy, but it works.

@toooni toooni reopened this Apr 10, 2024
@DarthGigi
Copy link

@toooni Thanks for reopening this issue!

The plugin files would be great to have for the time being.

Maybe an idea is to make the following config possible:

    locales : [
      {
        code : 'nl',
        domains : [ 'mydomain.nl', 'mydomain.be', 'mydomain.de', 'mydomain.fr' ],
        defaultDomain : 'mydomain.nl'
      },
      {
        code : 'be',
        domains : [ 'mydomain.nl', 'mydomain.be', 'mydomain.de', 'mydomain.fr' ],
        defaultDomain : 'mydomain.be'
      },
      {
        code : 'en',
        domains : [ 'mydomain.nl', 'mydomain.be', 'mydomain.de', 'mydomain.fr' ],
      },
    ]

@toooni
Copy link
Author

toooni commented Apr 10, 2024

I've created a public repo: https://github.com/toooni/nuxt-i18n-poc. It might be easier for you to adapt a similar solution for now and move further if this module is ready for our use cases. If you have question, you can @ me inside the repository, so we do not spam this issue anymore.

@e-kravtsov
Copy link

@DarthGigi, I have this config in my implementation, I've made patch for version 8.20 a few weeks ago. Maybe it will help as a starting point:
e-kravtsov@7b96fbc

@DarthGigi
Copy link

DarthGigi commented Apr 11, 2024

@e-kravtsov I've forked the official repo and added the changes found in your commit.
After some internal testing, it works perfectly for us, just how I described our use case above.

If wanted, I could make a pull request for it to be included in the official package.

@DarthGigi
Copy link

DarthGigi commented Apr 12, 2024

I've created a pull request: #2914

@DarthGigi DarthGigi linked a pull request Apr 16, 2024 that will close this issue
9 tasks
@DarthGigi
Copy link

DarthGigi commented Apr 29, 2024

I've created a pull request: #2914

I think I've added everything to the pull request. The logic, documentation and tests have been added, all tests pass as well. So for those who (really) need it, you can give it a try 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.