Jump to content
LAPS

How to translate strings globally?

Recommended Posts

Hi, I've successfully installed and used Language Support & Co. and Functional Fields modules so that I can translate almost all the strings of my website in English (default) and Italian.

Now, in my template files I've "See also" strings that I would like to translate globally (not per template by using Functional Fields). How should I make this?

I read the doc and maybe what I need is to use the textdomain, but I didn't understand how to use it. That is, what text/code should be in the file? any example?

Also, in the admin area I have the menu items Default and Italian under Setup > Languages. Once accessed one of them, there is the field "Site Translation Files" with the description: "Use this field for translations specific to your site (like files in /site/templates/ for example)". Should I use this field (instead of textdomains) to set globally translated strings? If so, what text/code should be in the file? any example?

Share this post


Link to post
Share on other sites

I do it usually as follows:
1.) Create a file /site/translations.php

<?php namespace ProcessWire;

/**
 * TRANSLATABLE STRINGS
 * Define globally available translatable strings
 *
 * USAGE
 * place this file under /site/translations.php
 * __("My translatable string", $ferry);
 * The wire property $ferry refers to the textdomain of this file
 *
 */
__("My translatable string");

2.) Define a wire derived object and place it in /site/ready.php

/**
 * TRANSLATABLE STRINGS
 * Include translatable strings
 * @see /site/translations.php
 * @var $ferry
 */
$this->wire('ferry', '/site/translations.php', true);

output in any template

/**
 * output in template
 */
echo __("My translatable string", $ferry);

Now you need to translate it only once, by using the string in any template. Usecase: strings like "read more" etc.

For strings specific to a template you can use simply:

/**
 * output in template
 */
echo __("My translatable string");

 

  • Like 4

Share this post


Link to post
Share on other sites

How do you translate the string? That is, where do you put English and (in my case) Italian strings (I see only the English one in file /site/translations.php)?

What do you think about this solution?

Share this post


Link to post
Share on other sites
21 hours ago, LAPS said:

What do you think about this solution?

Of course a good solution using the database instead of textdomain

21 hours ago, LAPS said:

How do you translate the string?

To translate globally defined strings as described in my post:

  • go to  SETUP > LANGUAGES > ITALIAN
  • klick button "Find files to translate"
  • select /site/translations.php
  • start to translate
  • Like 1

Share this post


Link to post
Share on other sites

Hi @kixe

Thanks for this technique! I followed your instructions and it seems to work as expected, however I get the following message on the translate page, maybe you or someone else can explain why and/or what it means:

Quote

Translate to other languages: Unable to load textdomain for other languages.

see the screenshot:

pw-textdomain-message.png.f3138d1e0fc54d96b4a051e9edf3d5a0.png

Any ideas? Thanks in advance.

 

Share this post


Link to post
Share on other sites

It should be AOS, if the same file is added for translation in other languages you can navigate easily between the them.

  • Like 2

Share this post


Link to post
Share on other sites
1 hour ago, tpr said:

It should be AOS, if the same file is added for translation in other languages you can navigate easily between the them.

I see, thanks for the quick reply! Yes, disabling AdminOnSteroids "makes it go away". I thought it might be some sort of issue I must be aware of. I can live with it, I mean because the site will not have another language just the default plus English there will be no other translations to switch to.

BTW, would it be possible to somehow show us what is modified/added by AOS? Maybe by hovering over the element and by waiting for a few seconds? Something like: https://codeburst.io/css-tips-and-tricks-using-pseudo-class-fa83248bb6e0 I'm just thinking out loud. I know that it is easy to disable AOS but in this case I was not even aware of the core features so I could not tell them apart.

Share this post


Link to post
Share on other sites

It would possible of course but I'm not sure that would be too beneficial. It would further increase the DOM weight and make more clutter, even when it could be disabled. Furthermore, many features couldn't have such indicators (eg. hotkeys) and it would be hard to create a "trademark" that would fit for most feature. But PR's are welcomed, as always 🙂

  • Like 1

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...