Skip to content

Simple browser vistor preference language detection for locale redirects and others utilities

License

Notifications You must be signed in to change notification settings

cara-tm/pat_lang_detect

Repository files navigation

<txp:pat_lang_detect />

Textpattern CMS plugin: Simple browser vistor preference language detection for locale redirects and other utilities.

This plugin intents to help for multi languages websites support. Detects the language preference of the visitors browser, then can redirect to the corresponding page named by the two matching letters (eg. en) if it exists or display a link to the matching section (support default locale section with this other compagnon plugin: pat_text).

Usage

After installation, activate this plugin into your preferences by ticking the “Enable pat_lang_detect?” field. Then, place this tag into your default page template but nothing other else:

  • <txp:pat_lang_detect redirect="1" display="0" />

And… Voilà!

So, create multilingual websites is as simple as to write articles attached to each locale sections. 😉

Your website structural organisation, section names and page templates, eg:

default: only one tag inside <txp:pat_lang_detect redirect="1" />
|
en: your entire home page template (markup) translated in English
|
de: your entire home page template (markup) translated in German
|
es: your entire home page template (markup) translated in Spanish

Note: Only one occurrence of this tag in your individual pages. You need to put your default home page template translated into each pages associated with a locale code (ISO2). For no redirect, keeping as this your ‘Default’ page, see below.

Attribute

  • redirect (boolean): set to true (i.e. 1) if you want a redirection to the corresponding locale page; set to false (i.e. 0) if you want only the URL (maybe for a link creation). Default “0” (false).
  • display (boolean): set to true (i.e. 1) returns locale link URL. Default false (do not returns anything).

Language detection overwriting

This plugin sniffs the URLs to detect a ?lang query inside.
So, you can overwriting the visitor language in your links by a new one into your current page, eg.

http://my-website.com/?lang=fr

TXP variables: <txp:variable name="visitor_lang" /> and <txp:variable name="visitor_iso" />

The plugin also creates TXP variables named <txp:variable name="visitor_lang" /> which contains the locale code country (ISO 639-1 format) of your visitors and available for any conveniences throught your entire website and <txp:variable name="visitor_iso" /> which contains language and country codes (ISO 639-1 followed by ISO 3166-1 Alpha 2 formats).

<txp:pat_lang_default />

This tag without any attributes returns the current TXP ISO2 active code language set in your interface.

<txp:pat_lang_compare />

A simple comparaison tag between the TXP default language and the visitor one. Can be use into breadcrumb links to redirect to the home page version translated into the visitor language.

<txp:pat_lang_detect_link />

This tag can create an HTML link to locale sections.

Attributes

  • label (string) required: the text for the link. Default: empty.
  • section (string) required: the section name. Default: empty.

Usage for Adblocks Plus

We noticed the Adblock Plus tool sometimes hides the <head> part of a page document if &lt;txp:pat_lang_detect /&gt; tag is present in it (especially within the <html> tag).
To avoid all problems, here is how to check and retrieve the ISO2 code into the lang attribute:

<html lang=“<txp:if_section name=”“><txp:lang /><txp:else /><txp:pat_lang_detect /><txp:variable name=”visitor_lang" /></txp:if_section>" dir=“<txp:text item=”lang_dir" />">

ie. Is this the “default” page? So, displays the native &lt;txp:lang /&gt; TXP tag; otherwise uses the plugin’s tags.

About redirection

Redirections can be a problem and/or identified as a bad practice by performance tools but without any noticed SEO impacts (on Google search engine). The default “redirect” attribute is set to “false” for this reason and you could prefer to display a simple locale message for your visitors instead:

<txp:pat_lang_detect />
<txp:if_variable name=“visitor_lang” value=“en”>
<p>This website is also available in your language: <a rel=“alternate” href=“<txp:pat_lang_detect display=”1" />">here</a></p>
</txp:if_variable>

For further infos with multiple translated strings support, see here too.

Above: A french website sample.

<txp:pat_lang_meta_href />

This tag, without any attributes, is dedicated to your <head> document part in order to add the alternate links to each locale pages of your website. This practice intents to get good spots for your SEO efforts.
This plugin works only within section into the permlinks preference choice of your website (i.e.: section/id/title or section/title permlinks format).

You will need to create your locale sections (i.e. en or en-us) and attach them to your default page template.

Note: This plugin can identify section names with only 2 letters and/or with 2 letters followed by an hyphen and 2 other letters.

Usage

Place this tag into the <head> part of your HTML document:

<txp:pat_lang_meta_href />

This tag checks all your locale sections available (named by 2 letters in ISO2 format and/or 4 letters with an hyphen separator) and injects all alternate links accordingly, e.g. (this case for an english site):

<link rel=“alternate” hreflang=“x-default” href=“http ://example.com/”>
<link rel=“alternate” hreflang=“de” href=“http ://example.com/de”>
<link rel=“alternate” hreflang=“es” href=“http ://example.com/es”>
<link rel=“alternate” hreflang=“fr” href=“http ://example.com/fr”>

Note: x-default value if a default section is found same as the website default active language sets in the languages preferences.
Alternative links are displayed in default order as populated into the ‘section’ table; individual alternative links are displayed in the order of the corresponding article #IDs.

Tested successfully with the online http://flang.dejanseo.com.au/ tool.

Custom field: <txp:custom_field name="Twin_ID" />

For individual alternate href links, use a custom field named Twin_ID to store a comma separated list of IDs of the corresponding translated articles (the current locale one, included). The alternate hreflang links will be generated automaticaly in an individual article context.

Histoy and Changelog

Plugin created for the “FOTO” prenium TXP theme.

  • 24 th December 2017. v 0.2.4.
  • 23 rd December 2017. v 0.2.3.
  • 13 th December 2017. v 0.2.1 & 0.2.2.
  • 6 th December 2017. v 0.2.
  • 23 td November 2017. v 0.1.9.
  • 16 th November 2017. v 0.1.8.
  • 15 th November 2017. v. 0.1.7.
  • 14 th November 2017. v 0.1.6.
  • 13 th November 2017. v 0.1.5.
  • 12 th November 2017. v 0.1.3. & v 0.1.4.
  • 11 th November 2017. v 0.1.2.
  • 8 th November 2017. v 0.1.0. & 0.1.1.

About

Simple browser vistor preference language detection for locale redirects and others utilities

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages