Jump to content

Using different domain names for each language


ZAP
 Share

Recommended Posts

Howdy -

I've read through a few forum posts and experimented a bit on my own, but I feel like I'm missing something obvious so I thought I'd ask a dumb question here.

I am setting up a simple bilingual website (English and Portuguese) and I started with the multilingual site template and a server that allows for two domain names (I'll call them "english.org" and "portuguese.org") to function equivalently. The language switcher works as expected and pages show the correct values for each language, so there's no problem in the setup. But there are two additional things that I need to do and haven't yet figured out:

  1. I want to set the user language based on the domain name, so that anyone coming in via a portuguese.org link will see pages in Portuguese (including the home page) and otherwise it will default to English.
  2. I do not want to show /en/ or /pt/ in my URLs, since the domain names themselves are what is distinguishing between the two languages.

I've tried things like this in a prepended file to set the language to Portuguese, but so far it hasn't worked as intended:

$rootdomain='english.org';
$hreflang='en';
	
foreach($languages as $language) {
	if(strpos($config->httpHost, "portuguese") === 0) { // PT domain
		$rootdomain='portuguese.org';
		$hreflang='pt';
		$user->language = $language; 
		break;
	  }
} 

What I hoped to get from this code was presetting the language to Portuguese based on the incoming domain name and from that I could include $rootdomain and $hreflang in the language switcher so that the domain name would change also when the switcher is used, but so far it hasn't worked. I also feel like I'm doing this wrong by looping through $languages since I only have two, but I tried this after other methods failed. Someone who is more familiar with this class can probably point out my error easily.

And as for #2 (removing the language code from the URL), my first instinct is to do this using .htaccess (portuguese.org -> portuguese.org/pt/) but based on other forum posts I've seen that may not be necessary. What's the PW way to get these URLs? I assume that I must have unique (and not empty) slugs for both languages in every page for this to work without .htaccess, since both domain names resolve to the same place, right?

Thanks in advance for your help with this!

Link to comment
Share on other sites

Hmm. I wish I could edit my post above because that loop is obviously flawed, but here is code I would think should work and doesn't:

$rootdomain='english.org';
$hreflang='en';

if(strpos($config->httpHost, "portuguese") === 0) { // PT domain
	$rootdomain='portuguese.org';
	$hreflang='pt';
	$languages->setLanguage('pt');
}

 

Link to comment
Share on other sites

Hello @ZAP,

this may not be the answer you are looking for, but I would really consider your approach in making a website available for multiple domains. For search engines that could be considered duplicate content and for users it could be annoying, because f.e. they would have to approve the cookie consent again.

I would recommend you to figure out what the default language and domain of your website should be (English or Portuguese) and then redirect the other domain to the language of the default domain (portuguese.org to english.org/pt/). The default language (in this case english.org) doesn't require a language URL (/en/).

If you look at major companies like f.e. Apple, Adobe, Microsoft or Amazon that is how they handle it.

If you really want to do this, you could look at this solution:

Regards, Andreas

  • Like 1
Link to comment
Share on other sites

Hey there. Thanks for your reply! I can see how that would be good advice for another project, but in this case it's not really an option.

This site is for a major published report that is produced jointly by a US-based and a Brazilian organization. The content for each language is not really just the same thing translated, since it's written for very different audiences and although it's based on the same research and data and makes the same main points, it changes a lot in the two editing processes. For example, to the English-speaking audience we need to explain elements of Brazilian history, geography, government functions, etc., whereas the Brazilian audience doesn't need any of that context but might need more explanation on a US policy or company with which the English-speaking audience is very likely to be familiar. I'm always amazed by how much texts like this need to be completely rewritten when "translated" in order to make sense in an entirely different context.

And in any case, this is the 5th annual report for which the US and Brazilian groups have been promoting it using these separate domain names (I'm trying to bring all of the past separate HTML sites plus this new one under one organized PW roof), so that horse has left the barn.

But yes I can certainly imagine that for other projects your advice might be very helpful and timely!

I actually looked at that example before I posted my question here, and it was helpful for me to get started and understand where and how I needed to approach this. But I couldn't quite adapt the methods in it to my specific situation, since I'm using different domains entirely (not subdomains) and I don't want to include the /en/ and /pt/ anywhere in the URL. Maybe .htaccess is actually a better way to approach this particular case?

 

 

Link to comment
Share on other sites

I just got back to this and I figured out how to modify the language switcher to specify the appropriate domain name when clicked (see below). I haven't delved into how to remove the subdirectories in the URLs or how to set the language to Portuguese whenever the portuguese.org domain name is used. Kinda wondering if in my case it wouldn't be easier to just start over with a non-multilingual installation and just create separate English and Portuguese pages (perhaps with a field in each mapping to its counterpart in the other language).

			if($language->name == 'pt') $rootdomain='https://portuguese.org';
			else $rootdomain='https://english.org';

			$hreflang = $homepage->getLanguageValue($language, 'name'); 
			echo "<a hreflang='$hreflang' href='$rootdomain$url'>$language->title</a></li>";
Link to comment
Share on other sites

Interesting! I wasn't aware of the multisite option, and that might be a better option in my case. With the multilingual site that I'm testing now, I was imagining just having a separate home page template that pulls the correct language data depending upon the domain name and displays the appropriate content, but I'm starting to see how I'm forcing a square peg into a round hole.

OTOH, do I even need a multisite installation, or could I just have a normal installation with both English and Portuguese pages? To switch languages, I could just have a field in each page to link to its counterpart (or I could also just do what the existing HTML sites that I'm assimilating do now: have a single link to the home page in the alternate language). The only glitches that I can think of at the moment would be that a) every page name/URL would need to be unique (not really an issue in my case), and b) search would return results from both languages unless I added a filter.

And of course as you mention I'd need to find a way to have two home pages (one per domain name), but I imagine that I could do that with either a home page template that adapts (perhaps just by pulling data from another page for an alternate language) or an .htaccess trick (rewriting one of the home page URLs).

Link to comment
Share on other sites

  • 2 weeks later...

I wrote that last post on a Saturday and I needed to have the new site up and running on Monday, so I ended up just using .htaccess to separate traffic into en and pt subdirectories and PHP files to create the pages. Which works, but at some point I'd still like to figure out how to do this using Processwire. Additionally I realized that in some cases the page names were the same for the two languages, whereas in others they were not (e.g., map and mapa). So I would have needed to futz with that detail as well, which didn't seem insurmountable but was another example of "square peg != round hole." Since one of the main reasons that I wanted this site in PW was to allow others to make edits and it turns out that no one else has shown any interest in doing that, I'm putting this on the shelf for a while.

Thanks very much for the suggestions and info!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...