8.4.2 How to translate user entered text?

Permalink
I enter some text in single pages in the Dashboard. The text is stored in the DB. Then the text is shown in a block. I set up a multilingual site. All php strings are translated and work, get translated fine.

But how do I get the text, which I entered myself, translated? I tried the Interface Translation in the dashboard, but none of my text is found by the tool.

linuxoid
 
mlocati replied on at Permalink Reply
mlocati
> I enter some text in single pages in the Dashboard. The text is stored in the DB. Then the text is shown in a block.

Do you mean that you developed a custom package that allows users to enter some text in your custom single pages, and that you would like that this text should be translatable with the t() functions?
linuxoid replied on at Permalink Reply
linuxoid
Hi!

No. I have single pages in the admin panel for the admin to enter various settings. For example admin enters car categories. So those categories are then displayed for the user. But because the text is entered only in one language, how can it then be translated?
mlocati replied on at Permalink Reply
mlocati
So, you are using Express?
linuxoid replied on at Permalink Reply
linuxoid
No. Just standard html/php form on a Dashboard single page to enter and store text in the DB with Doctrine. Similar to Community Store.
mlocati replied on at Permalink Reply
mlocati
You have to tell the translation system which are the strings that can be translated.

AFAIK, for custom data stored in custom database tables this can only be done within a package and with concrete5 8.1+.

In the package `controller.php` file, you can implement this method:

/**
 * {@inheritdoc}
 *
 * @see \Concrete\Core\Package\Package::getTranslatableStrings()
 */
public function getTranslatableStrings(\Gettext\Translations $translations)
{
    // Load the translatable strings from the database,
    // and add them to the $translations instance, for example:
    $translations->insert('', 'A strings to be translated');
}
linuxoid replied on at Permalink Reply
linuxoid
I guess that can't work as I wouldn't know during the package development what text the admin will enter later.

Can a language file work in a similar manner as the package's translation? Something the admin can edit/translate after entering the text?

Or even better, can a job be set up in the Dashboard to gather all those strings from the DB and put them into a .po file for the admin to translate?
mlocati replied on at Permalink Best Answer Reply
mlocati
> I guess that can't work as I wouldn't know during the package development what text the admin will enter later.

I understand it, that's why I wrote the following comment in the method body:
// Load the translatable strings from the database,
// and add them to the $translations instance, for example:
...

For example, if you store your translatable strings in the "MyName" column of a database table called "MyStuff", you could have a function body like this:

/**
 * {@inheritdoc}
 *
 * @see \Concrete\Core\Package\Package::getTranslatableStrings()
 */
public function getTranslatableStrings(\Gettext\Translations $translations)
{
    $app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();
    $db = $app->make(\Concrete\Core\Database\Connection\Connection::class);
    $rs = $db->executeQuery('select MyName from MyStuff');
    while (($myName = $rs->fetchColumn()) !== false) {
        $translations->insert('', $myName);
    }
}
linuxoid replied on at Permalink Reply 2 Attachments
linuxoid
Ok, I've added that function to the package controller and now all column strings appeared in the interface translation tool. I added their translation and saved the file. I've checked the .mo file - the translations are there.

I've cleared cache, refreshed the page... but the strings are NOT translated (see pics).

What am I missing?
mlocati replied on at Permalink Reply
mlocati
> I've cleared cache

Do you mean the concrete5 cache or the browser cache?
linuxoid replied on at Permalink Reply
linuxoid
Concrete5's, both

BTW, does it only translate from English to other languages or can it also translate from a language to English?
mlocati replied on at Permalink Reply
mlocati
> BTW, does it only translate from English to other languages or can it also translate from a language to English?

concrete5 usually translates from American English (en_US) to other languages.
If the destination language is already en_US, strings aren't translated until you set the concrete.misc.enable_translate_locale_base_locale configuration key to true.
linuxoid replied on at Permalink Reply
linuxoid
Excuse my ignorance, where can I do that, where/how do I set that value? Is that a config value or an attribute?
mlocati replied on at Permalink Reply
mlocati
Seehttps://documentation.concrete5.org/developers/appendix/cli-commands... :

./concrete/bin/concrete5 c5:config set concrete.misc.enable_translate_locale_base_locale true
linuxoid replied on at Permalink Reply
linuxoid
Can I do this in the package controller install():

$this->setConfigValue('concrete.misc.enable_translate_locale_base_locale', true);
mlocati replied on at Permalink Reply
mlocati
It depends a lot on the package use case.

In any case, you may want to use:

$this->app->make('config')->save('concrete.misc.enable_translate_locale_base_locale', true);
linuxoid replied on at Permalink Reply
linuxoid
That didn't help. None of the strings is translated from en to ru nor from ru to en.
mlocati replied on at Permalink Reply
mlocati
Did you click the "Save to file" button at the /dashboard/system/multilingual/translate_interface dasbhboard page?
What's the contents of the /application/languages/site directory?
linuxoid replied on at Permalink Reply 4 Attachments
linuxoid
Please check the pics attached.

Yes, of course I've saved the file. See the page in ru, the word is not translated. See the page in en, the word is not translated.

PS. Just noticed I've circled wrong words, but it doesn't change the fact - you can see all words remain the same regardless of the selected language
linuxoid replied on at Permalink Reply
linuxoid
Nope, nothing's translated
linuxoid replied on at Permalink Reply
linuxoid
I've opened the .po files, searched for the words and I see that they are listed under

#: packages/PACKAGE_NAME/src/Cars/Install/SinglePages.php:49

while the page I'm viewing them on is

http://localhost/c584/index.php/cars...

or

http://localhost/c584/index.php/en/cars...

Why are they in SinglePages.php? There are no such strings in that file. They only live in the DB!

Looks like ALL my translations from my own package .po file got appended at that line in the site's language file.
mlocati replied on at Permalink Reply
mlocati
What do you have at the line packages/PACKAGE_NAME/src/Cars/Install/SinglePages.php:49 ?
linuxoid replied on at Permalink Reply
linuxoid
$this->installSinglePage('/policy/consent', t('Consent'), $pkg);

it's totally unrelated
mlocati replied on at Permalink Reply
mlocati
And in your .po file what do you have right after the line
#: packages/PACKAGE_NAME/src/Cars/Install/SinglePages.php:49

?
linuxoid replied on at Permalink Reply 2 Attachments
linuxoid
please see attached

it's an unused file with an underscore. Its p. 46:
$this->installPage('consent', 'policy', t('Consent'), true, $this->pkg);
mlocati replied on at Permalink Reply
mlocati
The #: packages/PACKAGE_NAME/src/Cars/Install/SinglePages.php:49 comment is only for the first string ("Consent").

The following strings ("Budget", "Economy") doesn't have any comments.
linuxoid replied on at Permalink Reply
linuxoid
Exactly. The rest looks like all my package's language translation file got appended right after that line. And I've circled the word which is supposed to be translated. But it's not.
mlocati replied on at Permalink Reply
mlocati
So, just try to add this code right before the t() calls that don't work for you:

<?php echo "Current locale: " . \Localization::activeLocale() ?>
linuxoid replied on at Permalink Reply
linuxoid
Yes, it shows "Current locale: ru_RU" and "Current locale: en_US" on the ru and en pages correspondingly
linuxoid replied on at Permalink Reply
linuxoid
What was that supposed to do? What do I do?
mlocati replied on at Permalink Reply
mlocati
The last thing I can think of is to do a step-by-step debug (with xdebug) to see why the language file is not loaded...
linuxoid replied on at Permalink Reply
linuxoid
Can I simply create a .po file by hand with all words and translations and then compile it into .mo? Will that work?