Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2020-05-24 06:39:59

Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 106

My self-tailored-multi-language-approach with "SEO"-URLs

As mentioned elsewhere I would just like to share the way I deal with multilanguage pages meanwhile waiting for a native textpattern way of doing it.
Let´s say it is not the cleanest way but in IMHO it works pretty well.

So what you would need is the plugins smd_query and arc_meta installed.
arc_meta will add some fields like meta title and meta description to your sections and makes the “old” field “Description” obsolete.

We will then have unique sections in each language. For example “kontakt” for the german contact page and “contact” for the english one.
We are now going to use this obsolete field “Description” to give the sections “kontakt” and the “contact” a common name. So that they know they belong together. We will just call it “contact” in both sections.

I am not using the stylesheet selectbox in the sections neither. So hey… another obsolete field, that I could use for my purposes.
I create the styles “de” and “en” and just leave it empty.

In each section I can choose now with the help of the style selectbox the language of each section. So the “kontakt” section gets a “de” and the “contact” sections gets a “en”.

So far so good.

The language switch on the frontend will now look like this:

<a href='<txp::borg language="de"/>'>DE</a>

I did not come up with a more clever idea for a name of my output form here sorry.
In the output form I have this:

<txp:smd_query query='SELECT name FROM txp_section WHERE description IN (SELECT description FROM txp_section WHERE name = "?s") AND css = "<txp:yield name="language"/>"'>/{name}/</txp:smd_query>

So what it does is, it looks up with the help of our field Description the counterpart of the current section in another given language.

Of course things like

<txp:if_section name="something"/></txp:if_section>

Won´t work anymore in a multilingual setup. So what you want is again, that depending on your language the correct section is shown. And the sections are bundled to a group by the use of the description field.
So you just need another output from which I called “sec”. It is basically the same then “<txp:section/>” but it is aware of the language of the current section:

<txp:variable name="query">css = "<txp:variable name='lang'/>"</txp:variable><txp:smd_query query='SELECT name FROM txp_section WHERE description = "<txp:yield name="n"/>" AND <txp:variable name="query"/>'>{name}</txp:smd_query>

So all together it looks like this:

<txp:if_section name='<txp::sec n="contact"/>'>

Will always give you the contact-related section in your desired current language.
This is also where the highjacked css field comes into play.

The last thing is one little trick I did for the HOME pages. I created sections called “en” and “de” which work as the HOME pages.
Then I added a

RedirectMatch 301 ^/?$ /de/

into the htacces to make www.mydomain.de always switch to /de/ – the german principal version. (Of course this can also be done different with browser detection or something else)

So that´s basically it. It has the disadvantage that if you have a website (with 2 languages) that needs 12 pages/sections you will have 24 sections in total but it gives you the advantage that you have nice “speaking” URLs in the proper language.

For testing purpose I put this in the head of my page:

<txp:smd_query query="SELECT css, description FROM txp_section WHERE name = '?s'">
<txp:variable name="lang">{css}</txp:variable>
<txp:variable name="base">{description}</txp:variable>

Where base is always the “base name” of a section. So no matter what is called in the current language (greek for example: ΕΠΙΚΟΙΝΩΝΙΑ), the base name will always help you and show you what it is even if you do not speak the language.

I am curious what you think about this and hopefully it helps someone.

EDIT: changed references to aks_header to arc_meta.


#2 2020-05-27 20:38:28

From: Germany
Registered: 2005-01-20
Posts: 3,841

Re: My self-tailored-multi-language-approach with "SEO"-URLs

Thanks for outlining your method. It’s certainly more sophisticated than what I’ve done up to now, but my cases up to now have had just two languages.

A few questions/notes about your method:

  1. You said aks_header but did you mean perhaps arc_meta? (changed above)
  2. A parallel problem (that the MLP plugin in its day also addressed) is that some text strings vary from language to language. With some judicious designing, one can try and minimise the occurrences but sometimes its unavoidable. How do you deal with that? Maybe a sort of “if_language -> then this string” thing for each language? For two languages you could can use the txp:else case, for multiple languages maybe smd_multi_choice works better?
  3. If you wanted to avoid repurposing existing section fields, you could try my jcr_section_custom plugin that adds custom fields to the sections panel. There’s a separate branch for a variant of the plugin that supports multiple custom fields. You could use the same method, just replacing description and css with the respective custom field column names.

So that’s basically it. It has the disadvantage that if you have a website (with 2 languages) that needs 12 pages/sections you will have 24 sections in total but it gives you the advantage that you have nice “speaking” URLs in the proper language.

Like you mention, for my two language sites I did have two sections for each “section of the site”, one in each language. That looked neater in the respective language. For that I used a kind of lookup table made of reciprocally named variables (I described that here). It worked well using normal tags but quickly gets too complex for more than two languages. For sites with deep structures, I have extended that with an article custom field that holds the article_id of the “partner article” in the respective other language.

Another variant I’ve explored works for multiple languages but the url has to remain constant with the language code changing and the article holds all the different languages in several fields (made with glz_custom_fields to get extra textareas). See this thread. That was built for a particular case where a specific “messy” url scheme was requested, but it may be reworkable to a txp url-scheme.

TXP Builders – finely-crafted code, design and txp


#3 2020-05-27 21:53:36

Plugin Author
From: Germany
Registered: 2008-06-29
Posts: 106

Re: My self-tailored-multi-language-approach with "SEO"-URLs

Damn! Of course arc_meta ! Sorry. (changed above)

Will come back to you soon. Sounds interesting and thank you for sharing your method. I have to have a closer look over the weekend.

2) For textstrings I usually use an output form with a bunch of variables and an if_language setup, yes.
3) jcr_section_custom sounds great! Did not know that it exist. It would be better using that instead of highjacking arc_meta and it sounds like it will be easier. I guess no need for using smd_query in combination then.

I just went online with a new website using my technique and it has 3 languages (de,en and gr). I find it quite convinient to mantain it. I´d say it is a “normal” sized page.

Also never used smd_multi_choice. Seems like you just introduced me some fine new plugins. Thanks!


#4 2020-05-28 00:28:25

From: Kyiv, Ukraine
Registered: 2017-03-12
Posts: 134

Re: My self-tailored-multi-language-approach with "SEO"-URLs

jcr_section_custom – is very good!

Sorry my horror English. I’m learning textpattern, I’m learning English


Board footer

Powered by FluxBB