Jump to content

How to translate strings globally?


LAPS
 Share

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?

Link to comment
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 6
  • Thanks 2
Link to comment
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
Link to comment
Share on other sites

  • 3 months later...

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.

 

Link to comment
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.

Link to comment
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
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...