Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2023-02-20 01:51:41

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,437
Website GitHub

Flipping between two language translations of articles

This might be quite handy for anyone wanting to offer ad-hoc translations of some content on their site in one other language. It’s not ideal, and far from a true multi-lingual setup, but this is a method to go about it if you’re running Txp 4.8.8+ (which introduced the url_title filter in <txp:article_custom />)…

  • Set your sections up so you have all your regular sections that contain your main content, plus one special section to house all your translated documents in. You’ll probably not want to link this in your menu nav structures.
  • Create a Form shortcode called something like link_translation. Populate the form with the code given below (and season to taste).
  • In your page template(s), add <txp::link_translation /> wherever you want a language switcher to appear.

Then, whenever you want to offer a translation of an article:

  1. In the article you wish to translate, note its:
    1. section name (not visitor-friendly title).
    2. URL-only title name.
  2. Create a new article (or clone the original) in the section that houses all your translated content. Translate the text.
  3. Alter the translated article’s Meta>URL-only title field so it’s of the form: section__article. That’s:
    1. the original section name that the article appeared in.
    2. two underscores (or another delimiter of your choice if you tweak the code below to match).
    3. the original article URL title.
  4. Save the translated article, make Live, done.

Putting the section name and original URL-title as part of the translated article’s URL-title enables a backlink to be created to the original language version.

So here’s the language switcher widget shortcode:

<txp:variable name="meta_title"><txp:article_url_title /></txp:variable>
<if::section not name="{your-translation-section}">
    <txp:variable name="curr_section"><txp:section /></txp:variable>
    <article::custom section="{your-translation-section}" url_title='<txp:variable name="curr_section" />__<txp:variable name="meta_title" />'>
       <div class="translation"><txp:permlink class="xln-link">Read in {Second language}</txp:permlink></div>
    </article::custom>
<txp:else />
    <txp:hide> * Article is in 2nd language so offer backlink to original article * </txp:hide>
    <txp:variable name="orig_section" trim="/(.*)__.*/" replace="$1" value='<txp:variable name="meta_title" />' />
    <txp:variable name="orig_article" trim="/.*__(.*)/" replace="$1" value='<txp:variable name="meta_title" />' />
    <article::custom section='<txp:variable name="orig_section" />' url_title='<txp:variable name="orig_article" />'>
       <div class="translation"><txp:permlink class="xln-link">Read in {Original language}</txp:permlink></div>
    </article::custom>
</if::section>

Replace anything in curly braces with the relevant language names / sections. You could use imagery in the anchors if you preferred.

I’ve not tried to extend this concept with more than one language. I suspect it’ll get messy. It’s also annoying that the trim/replace has to be done twice to extract the original section and article. This could probably be simplified if you had etc_query or smd_wrap installed as it could split on the delimiter and extract both elements either side in one step. Maybe.

Anyway, hope this is of some use to someone. Refinements or extensions welcome.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#2 2023-02-20 07:51:34

Pat64
Plugin Author
From: France
Registered: 2005-12-12
Posts: 1,634
GitHub Twitter

Re: Flipping between two language translations of articles

Useful. Thanks ;)


Patrick.

Github | CodePen | Codier | Simplr theme | Wait Me: a maintenance theme | [\a mi.ni.ma]: a “Low Tech” simple Blog theme.

Offline

#3 2023-02-20 09:36:37

etc
Developer
Registered: 2010-11-11
Posts: 5,161
Website GitHub

Re: Flipping between two language translations of articles

Nice, thanks.

Bloke wrote #334681:

I’ve not tried to extend this concept with more than one language.

I have, fwiw, though the construction is slightly different (uses hidden sections). An extra problem you solve is staying within the current language when clicking, say, ‘Next’ link.

It’s also annoying that the trim/replace has to be done twice to extract the original section and article. This could probably be simplified if you had etc_query or smd_wrap installed as it could split on the delimiter and extract both elements either side in one step. Maybe.

You can do it in core, though it’s suboptimal (requires calling a breakform). Just create a form, say iterator, and here we are:

<!-- iterator form -->
<txp:variable name='var_<txp:yield item="count" />' value='<txp:yield item />' />

<!-- populate 'var_n' variables -->
<txp:variable name="meta_title" breakby="__" breakform="iterator" />

Offline

#4 2023-02-20 16:55:41

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,437
Website GitHub

Re: Flipping between two language translations of articles

Yeah the main downside to the approach is the loss of sections in the second language. Using hidden sections that feed the content into the displayed section is much more useful.

And thanks for the breakform example. That’s handy


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

Board footer

Powered by FluxBB