i18n icon indicating copy to clipboard operation
i18n copied to clipboard

Different domains with multiple languages each

Open toooni opened this issue 3 years ago • 37 comments

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.

toooni avatar Apr 30 '21 14:04 toooni

I would appreciate that too.

andorfermichael avatar May 06 '21 09:05 andorfermichael

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 https://github.com/visayasolutions/i18n-module/commit/af22fdaebc47c51e7fb0b068f151bbdc1f485d31

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

lfglopes avatar May 26 '21 19:05 lfglopes

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.

toooni avatar Jun 01 '21 09:06 toooni

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.

algj avatar Jun 08 '21 11:06 algj

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[bot] avatar Aug 18 '21 02:08 stale[bot]

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

marcmalerei avatar Oct 21 '21 20:10 marcmalerei

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!

nikolashaag avatar Nov 12 '21 10:11 nikolashaag

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

coeurofbear avatar Nov 12 '21 11:11 coeurofbear

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?

mbomfim33 avatar Jun 20 '22 16:06 mbomfim33

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

rchl avatar Jun 20 '22 21:06 rchl

@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 😄

mbomfim33 avatar Jun 21 '22 17:06 mbomfim33

@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 avatar Jun 21 '22 21:06 lfglopes

@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.

lfglopes avatar Jun 22 '22 06:06 lfglopes

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.

rchl avatar Jun 22 '22 06:06 rchl

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?

mbomfim33 avatar Jun 23 '22 20:06 mbomfim33

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.

rchl avatar Jun 23 '22 20:06 rchl

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?

mbomfim33 avatar Jun 24 '22 13:06 mbomfim33

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.

golubvladimir avatar Nov 16 '22 18:11 golubvladimir