Neo Posted February 25, 2016 Share Posted February 25, 2016 In order to generate dynamic hreflang tags, I use the following code: <?php // handle output of 'hreflang' link tags for multi-language // this is good to do for SEO in helping search engines understand // what languages your site is presented in foreach($languages as $language) { // if this page is not viewable in the language, skip it if(!$page->viewable($language)) continue; // get the http URL for this page in the given language $url = $page->localHttpUrl($language); // hreflang code for language uses language name from homepage $hreflang = $pages->get('/')->getLanguageValue($language, 'name'); // output the <link> tag: note that this assumes your language names are the same as required by hreflang. echo "\n\t<link rel='alternate' hreflang='$hreflang' href='$url' />"; } ?> The site is based on 2 languages: English (default) and Spanish. The default language does not have a language segment, Spanish uses "es". The problem with the above code is that hreflang will logically display nothing for the default, but should ideally show "en". Is this possible or is there a different way to access the multi-language URLs instead of looping through an array? Link to comment Share on other sites More sharing options...
Sergio Posted February 25, 2016 Share Posted February 25, 2016 Change this line: $hreflang = $pages->get('/')->getLanguageValue($language, 'name'); To $hreflang = $pages->get('/')->getLanguageValue($language, 'name'); if ($hreflang !== 'es') { $hreflang = 'en'; } Link to comment Share on other sites More sharing options...
LostKobrakai Posted February 25, 2016 Share Posted February 25, 2016 I'd suggest just adding a new field to the language template (show system templates in the template list) and putting the iso codes there. Then you can just do $language->iso or similar. That's nothing that should depend on any settings on the homepage. Also there's $language->isDefault() if you need to change any logic only for the default language. 2 Link to comment Share on other sites More sharing options...
Sergio Posted February 25, 2016 Share Posted February 25, 2016 @LostKobrakai suggestion is better, but if you decided to not add the field to the language page, the previous code will be: <?php // handle output of 'hreflang' link tags for multi-language // this is good to do for SEO in helping search engines understand // what languages your site is presented in foreach($languages as $language) { // if this page is not viewable in the language, skip it if(!$page->viewable($language)) continue; // get the http URL for this page in the given language $url = $page->localHttpUrl($language); // hreflang code for language uses language name from homepage $hreflang = $pages->get('/')->getLanguageValue($language, 'name'); if ( $language->isDefault() ) $hreflang = 'en'; // output the <link> tag: note that this assumes your language names are the same as required by hreflang. echo "\n\t<link rel='alternate' hreflang='$hreflang' href='$url' />"; } ?> 2 Link to comment Share on other sites More sharing options...
Neo Posted February 25, 2016 Author Share Posted February 25, 2016 Thanks for your repsonses. "if ( $language->isDefault() ) $hreflang = 'en';" will do it. @Sérgio: The $homepage variable would break the code. Link to comment Share on other sites More sharing options...
Sergio Posted February 25, 2016 Share Posted February 25, 2016 I saw it and changed it after. The $homepage is defined in my _init.php. Thanks for your repsonses. "if ( $language->isDefault() ) $hreflang = 'en';" will do it. @Sérgio: The $homepage variable would break the code. 1 Link to comment Share on other sites More sharing options...
adrian Posted September 15, 2016 Share Posted September 15, 2016 Hey guys - as a few of you know, I am in the middle of my first ML site and was wondering about the hreflang tag. Using the recommended: $hreflang = $pages->get('/')->getLanguageValue($language, 'name'); to set the value in the hreflang seems to contradict what I am reading elsewhere. I thought that the hreflang tag must always include a country, in my case: pt-br. But I don't really want pt-br in the url - I really just want: http://mysite.com/pt/ rather than http://mysite.com/pt-br/ Now obviously I can set the hreflang another way, but it seems like most of you are doing it this way, so just wondering if it's actually ok to leave the country code off, eg. hreflang="pt" rather than hreflang="pt-br" Also, what about: x-default vs en-us for my default language? Thanks again for your advice! PS - I have gone with @LostKobrakai's approach of adding an iso field to the language template so I can easily define en-us (or x-default - still don't know what is best here) and pt-br, but I would still like to know why it seems that most are just using the two letter language code without the country code. Link to comment Share on other sites More sharing options...
adrian Posted September 15, 2016 Share Posted September 15, 2016 Maybe getting a little OT, but how is: $page->viewable($language) determined? Is it based on whether the page has an alternate "name" in the requested language, or some other means? I might actually take another approach, because it's actually only one branch of my site that has more than one language (english) at the moment, so it would be wrong to list the hreflang as having pt-br for the rest of the site. Any ideas please on the best practice approach to automate this? Link to comment Share on other sites More sharing options...
LostKobrakai Posted September 15, 2016 Share Posted September 15, 2016 https://github.com/ryancramerdesign/ProcessWire/blob/devns/wire/modules/LanguageSupport/LanguageSupportPageNames.module#L395-L406 Here it's defined 1 Link to comment Share on other sites More sharing options...
adrian Posted September 15, 2016 Share Posted September 15, 2016 13 minutes ago, LostKobrakai said: https://github.com/ryancramerdesign/ProcessWire/blob/devns/wire/modules/LanguageSupport/LanguageSupportPageNames.module#L395-L406 Here it's defined Sorry that was lazy of me to not look for that. So essentially it's all about the "Active" checkbox on each page. It's actually kinda weird to me that when you add a new language all existing pages are automatically activated in that language. Guess I need a little API script (or actually maybe some SQL will be easier) to deactivate all pages outside the one branch that is available in PT. Although I expect I still want the homepage and any direct parents between that ML branch and the homepage activated so that all segments of the URL will be in PT when requested - does that sound correct? That way I'd still get: /pt/cartoes-relatorios/baia-de-guanabara/indicadores/nitrogenio-inorganico-dissolvido/ rather than: /report-cards/baia-de-guanabara/indicadores/nitrogenio-inorganico-dissolvido/ where /baia-de-guanabara/ is the parent page of the one branch that is available in both EN and PT. Link to comment Share on other sites More sharing options...
adrian Posted September 15, 2016 Share Posted September 15, 2016 Ok, so the problem with making a language not checked "active" for a pages is that the page disappears from menus when viewing in that language. So obviously I could either override that active check when generating the array of pages for the menu, or I could use another method for determining whether to show a hreflang tag for that language for that page. I don't want English-only pages hidden from users who are viewing the site in Portuguese, but I also don't want Google thinking that there is a PT specific page when there isn't. I am assuming it's good practice to keep the /pt/ in the URL even when there is no PT language version for that page? Otherwise if the page redirected to the default/english version, then if they navigated back to the pages that do have a PT version, then they would have to change the language again. I think the logical thing is to not use: $page->viewable($language) to determine whether to add a hreflang for the language, but I would love to know what you guys do when you have a site that is only ML for certain sections, but you want all sections to be available to all users - they can of course always use Google Translate for the other sections so I don't want them hidden/not active. Sorry for rambling - just trying to figure out what the best practice is here. Thanks in advance! 1 Link to comment Share on other sites More sharing options...
szabesz Posted September 16, 2016 Share Posted September 16, 2016 Probably it is more of a UI/UX design issue than a technical one. I think the default behavior of the menu item not showing up when the page is not available in a given language is ok, at least that is what I would go with anyway. If you just simply include the not translated page in the menu linking to its English counterpart, the visitor might easily believe that it is an error and/or the page has not been translated by mistake. You might want to find other means to present these pages in question to the visitors. For example if your site menu is not huge, you can put two site tree like menus in a "mega footer", labelled: "Pages in Portuguese:" and "Pages in English", so that visitors interested in the English version can still find it. Sure, pages are still a bit hidden this way, but it is just one idea of solving this, you might want something else that is more sophisticated than this. 1 Link to comment Share on other sites More sharing options...
adrian Posted September 16, 2016 Share Posted September 16, 2016 Thanks for your thoughts @szabesz - it's a pretty complex site with most sections only relevant to people in the USA, although it adds value to users from other countries/languages to see the content from other regions, even though it's not translated. Down the road I can see that we'll have other sections that are only specific to other languages. Ideally, the overview pages will be translated into all these languages at some point. I think I have a decent approach now - I was just wondering what the general principle is when parts of a site are translated and other parts aren't, and dealing with setting the hreflang tag, but I think in my case I need some custom logic which is what I have done. 1 Link to comment Share on other sites More sharing options...
szabesz Posted September 16, 2016 Share Posted September 16, 2016 7 minutes ago, adrian said: I think I have a decent approach now I'm interested in it, so if you could just jot it down as a short case study after you have finished the site, that would be cool 1 Link to comment Share on other sites More sharing options...
Christophe Posted September 17, 2016 Share Posted September 17, 2016 I often use a 'hreflang' field instead of a 'name' field, so I can enter anything in the field. But the few websites that I've created multilingual from the beginning - in case - have only one language activated for the frontend as of today... 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now