Textpattern CMS support forum

You are not logged in. Register | Login | Help

#11 2019-05-06 06:59:46

agovella
Member
Registered: 2005-05-01
Posts: 33

Re: Multi-Lingual Site

I would like to have the language code as part of the URL: /en/milk, /es/leche, /fr/lait

Is the best way to do this to make sections for “en”, “es”, and “fr”?

And a related question, how do you get URLs with multiple sub-directories like this:
/en/about/history/first-10-years

When you add a “/” in the article URL, it converts to “%2F”. If you add a “/” in the section name, it converts to a hyphen (“-”).

Offline

#12 2019-05-06 08:17:46

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,488
Website

Re: Multi-Lingual Site

agovella wrote #317896:

I would like to have the language code as part of the URL: /en/milk, /es/leche, /fr/lait
Is the best way to do this to make sections for “en”, “es”, and “fr”?

That approach is possible if you only need one level of depth beneath, e.g. “en”, “es”, and “fr” are sections and “milk”, “leche” and “lait” are articles. Judging by your next question you want more depth, though.

Another approach: It would be conceivable to use an .htaccess rewrite to convert /(en|es|fr)/ to an url variable such as lang=$1, i.e. an url like /en/milk would be rewritten to /milk?lang=en or index.php?s=milk&lang=en before being passed to textpattern. With this approach, “milk” is the section, so you could have another level to your URLs, such as /en/milk/lactose-free

You’d then use a plugin like adi_gps or rah_gps to retrieve the lang url variable as a textpattern variable for using on your page. You’d also need to construct your own permlinks and section/category links as the standard functions won’t include the language part of the url. That’s not difficult, though.

And a related question, how do you get URLs with multiple sub-directories like this:
/en/about/history/first-10-years

That’s something of a holy grail. Out of the box, you can only get the url schemes in the drop-down on the Admin › Settings panel. You can fake depth in your menu structure but your actual urls are still of the /section/article-title or /category/category-name type.

In your example, you might have:

menu url textpattern
About /about section
About › History /about/history article
About › History › First 10 years /about/history-first-10-years article

… and so on.

Two other thoughts:

1) The plugin rah_pathway allows you to give an article a completely user-defined url including forward slashes. While it doesn’t add any actual depth structure to Textpattern, it would enable you to fake the last line of the above table to look like this:

menu url textpattern
About › History › First 10 years /about/history/first-10-years article

You would again need to construct your own permlinks. The plugin puts the user-defined url in a custom field, which is helpful for doing that.

2) There was a plugin called gbp_permlinks that worked with earlier versions of textpattern. It was extraordinarily powerful at making custom url schemes for different sections, also with user-definable snippets in the url, but it was also notoriously difficult to use and I think no longer compatible with current versions of textpattern. That would require some archeological reconstruction work (probably paid) from a plugin author but was much more powerful.

BTW: I have your book in my shelf here!


TXP Builders – finely-crafted code, design and txp

Offline

#13 2019-05-06 08:32:40

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,488
Website

Re: Multi-Lingual Site

jakob wrote #317863:

By way of example, this is what you’d do for the minimum theme … [to update to texpattern 4.7+‘s theme facility.]

FWIW, I gave this a whirl, and you can find the converted theme here. It really is a very basic theme, so I’m not sure how useful it is, but there you go…


TXP Builders – finely-crafted code, design and txp

Offline

#14 2019-05-06 22:26:28

agovella
Member
Registered: 2005-05-01
Posts: 33

Re: Multi-Lingual Site

jakob wrote #317900:

BTW: I have your book in my shelf here!

POINTS! :-)

Offline

#15 2019-05-07 01:45:19

mori
New Member
Registered: 2019-05-01
Posts: 9

Re: Multi-Lingual Site

By way of example, this is what you’d do for the minimum theme:

  • Download the repository from GitHub
  • Make a folder called minimum
  • Copy the forms, pages and styles folders into it.
  • In the pages folder, rename the file extensions from .page to .txp.
  • in the forms folder, make some folders article, comment, file, link and misc and move the ___.misc.form files into the misc folder, and ___.article.form files into the article folder, and so on. Then rename all the file endings from ___.misc/article/image/link/file.form to .txp.
  • Now make a text file called manifest.json in the minimum folder and put this in it:

{...

And save that. Take a look at the default theme to see an example of how it should look in the end.

Now move the entire folder to the /themes folder of your textpattern installation. When you visit Presentation › Themes, you should now be able to import Minimum from disk.

The last bits you’ll need to do is add the section names that don’t already exist as shown here.

And install the plugins the theme recommends. Of the plugins that the minimum theme suggests, cxc_templates is not necessary. The others should all work but all can be replaced by txp’s own built-in tags. To install them, visit that page in Github, e.g. here, click on the Raw button and copy the long string of letters and numbers to the clipboard. Visit Admin › Plugins and paste that string of letters and numbers into the install plugin textbox and click install and then activate (switch to “on”).

Thank you for the tutorial! I followed your guide and uploaded the theme onto my Textpattern installation. Then I created a new section which uses Minimum and its “default_minimum page”, and made an article (with a test string) for that Test section. As it turns out, nothing came out on screen. I didn’t investigate further. I inserted plain HTML into a blank page template under Minimum theme to test things out, and it worked, so it probably wasn’t an installation issue. All in all, I have a better Idea of how themes work – thank you! I’ll probably customize themes when I have the time.

To link different language menu sections to each other, you can set up a list of variables that indicate which is the section name in the parallel language. By naming the variables according to a pattern, you can make your language switcher switch between the correct sections at the section landing page level.

For the navigation you can likewise use a variable to hold the list of sections that should appear in each language. While you can use a cookie to keep track of which language you are on, it’s not strictly necessary here as this method essentially uses several sets of article trees and you restrict the menu sections to those that you need for each language.

Sorry..I don’t have the knowledge to know the exact steps. By “variable” do you mean inserting variables into the URL and using .htaccess to rewrite the URL before passing it to Textpattern and then use adi_gps or rah_gps plugin to extract the variable for use – like what you mentioned in another post?

The plugin rah_pathway allows you to give an article a completely user-defined url including forward slashes. While it doesn’t add any actual depth structure to Textpattern, it would enable you to fake the last line of the above table to look like this:

That looks really nice. I too would really like to have urls with multiple sub-directories, which would involve constructing my own permlinks and section category links. Does that mean making use of URL variables and txp tags or would I have to touch the core code files?

The rah_pathway github page says to install it using composer. Unfortunately, I think my hosting provider doesn’t have Composer, nor can I ssh into the server. A quick search on my host provider’s forum gave the following:

Our general recommendation for composer stuff is to install it and run all the commands on your own PC, then upload the resulting files. It works just fine and doesn’t require we install it or run anything on our servers that way.

It looks as if I have to do a local installation, install the plugin with composer, search for the newly generated files and upload them…if that works at all… Might there be another way of installing rah_pahthway?

Update I installed Textpattern with the rah_pathyway plugin on my local machine, zipped it up and unzipped on hosting server. The “Free-form article URL” field didn’t show up on Admin > Site panel. Might there be another way of installing the plugin?

Last edited by mori (2019-05-07 05:07:44)

Offline

#16 2019-05-07 05:20:50

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,488
Website

Re: Multi-Lingual Site

mori wrote #317912:

Thank you for the tutorial! I followed your guide and uploaded the theme onto my Textpattern installation. Then I created a new section which uses Minimum and its “default_minimum page”, and made an article (with a test string) for that Test section. As it turns out, nothing came out on screen. I didn’t investigate further.

Have a look here for an example of the converted minimum theme.


TXP Builders – finely-crafted code, design and txp

Offline

#17 2019-05-07 05:30:00

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,488
Website

Re: Multi-Lingual Site

mori wrote #317912:

By “variable” do you mean inserting variables into the URL and using .htaccess to rewrite the URL before passing it to Textpattern and then use adi_gps or rah_gps plugin to extract the variable for use – like what you mentioned in another post?

This thread has got a bit complicated with the different methods, so there’s a risk of mixing them up.

Going back to Variant 1 with parallel sections in different languages, I meant using textpattern’s own variables which you set and use with the txp:variable tag. With this variant, you have a host of sections with articles in their respective languages but you need to control which sections show in the menu in each language and a make a switcher that allows you to switch between the corresponding sections in each language. The following is an example showing the principle with sections only and assumes:

  • You have set up sections for each area of your site in each language (using distinct names).
  • For the homepage view in another language, make an extra section and name it with the language code, e.g. “de”. If “en” is your standard language, it’s not strictly necessary to make that section unless you really want it.

To begin with, you need to set up some helper variables for the site as a whole. Typically I put these in a form of their own such as page_vars and call that form with txp:output_form at the top of the page template (the plugin adi_variables provides a UI for making that more user-friendly but it’s not essential).

1) For the navigation, set a variable for each language with a list of the sections that should appear in the menu in that language, e.g.:

<txp:variable name="menu_sections_en">about,music,gallery,reviews,contact</txp:variable> 
<txp:variable name="menu_sections_de">ueber,musik,galerie,pressestimmen,kontakt</txp:variable> 

2) Then set a variable for each section in each language pointing to the corresponding parallel section, e.g.:

<txp:variable name="langswitch_about">ueber</txp:variable>
<txp:variable name="langswitch_ueber">about</txp:variable>
<txp:variable name="langswitch_music">musik</txp:variable>
<txp:variable name="langswitch_musik">music</txp:variable>
<txp:variable name="langswitch_gallery">galerie</txp:variable>
<txp:variable name="langswitch_galerie">gallery</txp:variable>
...
<txp:variable name="langswitch_default">de</txp:variable>
<txp:variable name="langswitch_de"></txp:variable>

Make sure the naming matches your section names. Note: the last two are for the homepage in the respective language (“default” is the standard textpattern name for the homepage).

3) At the top of the page template (or at the end of the page_vars form), set a variable to detect the current language of the section you are on:

<txp:if_section name='<txp:variable name="menu_sections_en" />'>
    <txp:variable name="lang" value="en" />
<txp:else />
    <txp:variable name="lang" value="de" />
</txp:if_section>

(Note the use of ' single quotes for using a txp-tag as a variable of a txp-tag).

If you have more than two languages, you need to do several separate <txp:if_section> … </txp:if_section> one after the other instead of using txp:else. This lang variable will be useful throughout the rest of your page template and forms to control output according the respective language.

To create the site navigation for the respective language, you feed txp:section_list with the respective menu_section_{language} variable, so that only those sections appear in the menu. (Note: here it’s necessary to use ' single quotes as above, and in this case also two single quotes '' when using a txp-tag as a variable of a txp-tag as a variable of a txp-tag!)

<!-- navigation -->
<nav class="c-main-nav">
    <txp:section_list sections='<txp:variable name=''menu_sections_<txp:variable name="lang" />'' />' break="" wraptag="ul">
        <li<txp:if_section name='<txp:section />'> class="active"</txp:if_section>>
            <a href="<txp:site_url /><txp:section />"><txp:section title="1" /></a>
        </li>
    </txp:section_list>
</nav>

For the language switcher, the code looks like this:

<nav class="c-lang-switch">
    <ul>
<txp:if_variable name="lang" value="en">
        <li class="en active">
            <a href="<txp:site_url /><txp:section />">EN</a>
        </li>
        <li class="de">
            <a href="<txp:site_url /><txp:variable name='langswitch_<txp:section />' />">DE</a>
        </li>
</txp:if_variable>
<txp:if_variable name="lang" value="de">
        <li class="en">
            <a href="<txp:site_url /><txp:variable name='langswitch_<txp:section />' />">EN</a>
        </li>
        <li class="de active">
            <a href="<txp:site_url /><txp:section />">DE</a>
        </li>
</txp:if_variable>
    </ul>
</nav>

The principle is that by naming your variables to match the section names, your language switcher will point to the corresponding section in the other language.

The language switcher code also shows how you can use the lang variable created at the top of the page, to show different content, e.g.

<h2><txp:if_variable name="lang" value="en">Welcome<txp:else />Willkommen</txp:variable></h2>

Because that can get a bit cumbersome over time (and increasingly complex if you have more than two languages), you can use smd_babel to define named strings in each language via the admin interface. If you name them something like mysite_welcome_en and mysite_welcome_de, you can use the variable tag-in-tag trick in combination with txp:text to output the respective language string, e.g.:

<h2><txp:text item='mysite_welcome_'<txp:variable name="lang" />' /></h2>

You get the principle by now…

All this is just at the section level. If you need to extend the language switcher to the article level, you can do that by creating one or more custom_fields, e.g. parallel_article_id when you have just one other language or en_article_id, de_article_id, fr_article_id etc. when you have several languages. You place the id number of the corresponding article(s) in the custom field, e.g. if “about” is article id#1 and “ueber” is article id#2, you would put 2 in the parallel_article_id custom field of the “about” article.

With a combination of txp:if_article_list, txp:if_individual_article, and custom_field and if_custom_field as well as <txp:article_custom id='<txp:custom_field name="parallel_article_id" />'><txp:permlink /></txp:article_custom> you can make your switcher switch between corresponding articles in each language.


TXP Builders – finely-crafted code, design and txp

Offline

#18 2019-05-07 05:44:37

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 3,488
Website

Re: Multi-Lingual Site

mori wrote #317912:

… which would involve constructing my own permlinks and section category links. Does that mean making use of URL variables and txp tags or would I have to touch the core code files?

No, you don’t need to touch the core files, it just means the standard txp:permlink and txp:section url="1" etc. won’t include your language-specific aspects, and you need to make (i.e. “write out the parts of”) your hrefs using txp:tags, e.g.

<txp:site_url />en/<txp:section/<txp:article_url_title />

or

<txp:site_url /><txp:section/<txp:article_url_title />?lang=<txp:variable name="lang" />

etc.

The rah_pathway github page says to install it using composer. Unfortunately, my hosting provider doesn’t have Composer, nor can I ssh into the server. … Might there be another way of installing rah_pathway?

Ah, it seems the plugin author gocom has gone over to not providing the .txt installer files in the (currently wrong) assumption that everyone can use composer. He also uses a clever but non-standard setup for his plugins, so you need to use his plugin compiler on your local computer to create a compiled txt. Hang on, and I’ll see if I can do that for you. Here you go:

# Name: rah_pathway v0.1.0
# Custom article permlinks
# Author: Jukka Svahn
# URL: https://github.com/gocom/rah_pathway
# Recommended load order: 5
# .....................................................................
# This is a plugin for Textpattern - http://textpattern.com/
# To install: textpattern > admin > plugins
# Paste the following text into the "Install plugin" box:
# .....................................................................

H4sIAAAAAAAAE+1Z6XLbOBL+r6fo1biGksuW5CuHvE7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igPTrs
1l8eP++cHB4eH7HgiD077Bwenbx47k/5y5PDEz7pHNVPv/4P+Bg9R2MeAAA=

TXP Builders – finely-crafted code, design and txp

Offline

#19 2019-05-07 06:17:23

mori
New Member
Registered: 2019-05-01
Posts: 9

Re: Multi-Lingual Site

Wow! Thank you so so much for the excellent tutorials as always! By far the best I’ve ever experienced, thank you! I can’t express my gratitude enough!

You definitely showed the power of Textpattern! I’ve gone through the basic documentation and will poke around the tags reference a bit more while giving a bit more thought to how I should set things up.

And thank you! for compiling it for me – worked like a charm.

Update After a bit of thought, I’ve decided to go the route of using the same url with language code query parameter at the end, because the site would have east-asian languages which doesn’t use the alphabet. Although the words could be romanized, the url would be a pain to read and not pretty at all. I’ll take the SEO loss.

Thank you for teaching me how to write code for using distinct urls though! It’s a very good reference for how I should implement my permalinks even when I’m using the query parameter method.

Last edited by mori (2019-05-09 05:36:04)

Offline

Board footer

Powered by FluxBB