• How can I create a multilanguage website without plugins? I have Loco Translate for the translation of a certain number of labels, but it’s not enough, I have to translate for example the values of custom taxonomies, the description (what you write in the post editor right under the title) and so on. Some words are inside the functions.php

    The page I need help with: [log in to see the link]

Viewing 8 replies - 1 through 8 (of 8 total)
  • Moderator bcworkz

    (@bcworkz)

    For words within PHP functions you can use the gettext system with functions like __() to get translated values.

    For words the come from the DB like taxonomy term names, you could filter their retrieval, for example by using the "get_{$taxonomy}" filter. The thing is, how would your filter code know what the translations is for any given term? The gettext system requires static strings that are known ahead of time. When users can add any term they like, how would your code know what the correct translation would be?

    Thread Starter sacconi

    (@sacconi)

    Everything must be translated in advance, like in my current website: https://www.sacconicase.it/

    Moderator bcworkz

    (@bcworkz)

    You can either make use of gettext or develop your own translation lookup scheme.

    For strings that are hardcoded, use gettext as intended. For content in the DB, it can be filtered and run through gettext as well. Normally we shouldn’t pass variables to translation functions, but if you’re sure the variable value can be translated with existing data in your .mo file it’ll work.

    Thread Starter sacconi

    (@sacconi)

    I’d to start with an experiment, I’d like to add a suffix …/de for all the pages that are supposed to be in german. Or better a subdomain with a prefix de./….? Than I’ll start to create a translation file to get translated all the labels. Which is the point of start?

    Moderator bcworkz

    (@bcworkz)

    Because permalinks in WP can have a varying number of elements, the “/de” element needs to be in a known, constant position, usually very first or very last. And you’d need a language element for all URLs in any language: /it, /fr, /en, etc. If you can conform to those caveats, a custom rewrite rule can be used to capture the requested language and use it to set a custom query var. You can then use the query var to alter what content is served.

    A language sub-domain is possibly a little easier. You define the desired sub-domains in your hosting account, pointing them all to the same WP installation. You can then extract the requested sub-domain out of the $_SERVER array and use it to conditionally serve the requested language. In this case a sub-domain is not required to be provided. If there is none or it’s www, then serve the default language as usual.

    If you plan to use the gettext translation scheme, set the requested language through the “locale” filter.

    An alternative to the sub-domain solution I’ve described is to create a multisite installation and each sub-site will be an independent site in the requested language. By default these really are independent sites from a data standpoint. You could end up with a lot of redundant information, such as the same images repeated on every sub-site. It is feasible to get images and other data from a “master” site, but it’ll take some custom coding to do so.

    Thread Starter sacconi

    (@sacconi)

    If the easiest solution is having sub-domains, I could ask the server manager to create de.test.sacconicase.com for the german language. Then I point the same content of .test.sacconicase.com, that is to say I dont duplicate the content/data-base, I run the same content, am I right?

    then I could start using gettext for my functions. I.e.: if I’d want to translate this function

    $titolo_prezzo_include = '<h4 class="titolo_affitti">Il prezzo include:</h4>';

    Shoud I re-write this way?

    $titolo_prezzo_include? = '<h4 class="titolo_affitti">'. esc_html__('Il prezzo include:', 'my-text-domain') . </h4>';

    After that where I’m going to see “Il prezzo include:”, for a translation, let’s say in german?

    This system is good for all short repetitive words, such as payment conditions, check-in and check-out times, everything you see for example at the bottom of of each apartment/post, but for the title and description of every apartment/post is not good.
    My idea was to create title and description fields in the editor of each post, so the same post in German will have a different title and description. Is this path possible?

    Thread Starter sacconi

    (@sacconi)

    I corrected my code:

    $titolo_prezzo_include  = '<h4 class="titolo_affitti">'. esc_html__('Il prezzo include:', 'my-text-domain') . '</h4>';
    

    Now it doesnt breack the site, but it helps my purpose?

    Thread Starter sacconi

    (@sacconi)

    It’s not clear to me what I need to tell my server manager if I want to use the subdomain method. For example I would need https://de.test.sacconicase.com/ for german, should I tell the manager to create this address and connect it to the main site? (At the moment my site is already a sub-domain because it is a test area.) Then if I understood correctly I have to create a .POT file, where do I start? using gettext can I put all the words to be translated into the .POT file?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘multilanguage website’ is closed to new replies.