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.
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.
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?
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?
So, you are using Express?
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.
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:
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'); }
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?
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?
> 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:
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); } }
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?
I've cleared cache, refreshed the page... but the strings are NOT translated (see pics).
What am I missing?
> I've cleared cache
Do you mean the concrete5 cache or the browser cache?
Do you mean the concrete5 cache or the browser cache?
Concrete5's, both
BTW, does it only translate from English to other languages or can it also translate from a language to English?
BTW, does it only translate from English to other languages or can it also translate from a language to English?
> 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.
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.
Excuse my ignorance, where can I do that, where/how do I set that value? Is that a config value or an attribute?
Seehttps://documentation.concrete5.org/developers/appendix/cli-commands... :
./concrete/bin/concrete5 c5:config set concrete.misc.enable_translate_locale_base_locale true
Can I do this in the package controller install():
$this->setConfigValue('concrete.misc.enable_translate_locale_base_locale', true);
It depends a lot on the package use case.
In any case, you may want to use:
In any case, you may want to use:
$this->app->make('config')->save('concrete.misc.enable_translate_locale_base_locale', true);
That didn't help. None of the strings is translated from en to ru nor from ru to en.
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?
What's the contents of the /application/languages/site directory?
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
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
Nope, nothing's translated
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.
#: 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.
What do you have at the line packages/PACKAGE_NAME/src/Cars/Install/SinglePages.php:49 ?
$this->installSinglePage('/policy/consent', t('Consent'), $pkg);
it's totally unrelated
And in your .po file what do you have right after the line
#: packages/PACKAGE_NAME/src/Cars/Install/SinglePages.php:49
?
#: packages/PACKAGE_NAME/src/Cars/Install/SinglePages.php:49
?
please see attached
it's an unused file with an underscore. Its p. 46:
it's an unused file with an underscore. Its p. 46:
$this->installPage('consent', 'policy', t('Consent'), true, $this->pkg);
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.
The following strings ("Budget", "Economy") doesn't have any comments.
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.
So, just try to add this code right before the t() calls that don't work for you:
<?php echo "Current locale: " . \Localization::activeLocale() ?>
<?php echo "Current locale: " . \Localization::activeLocale() ?>
Yes, it shows "Current locale: ru_RU" and "Current locale: en_US" on the ru and en pages correspondingly
What was that supposed to do? What do I do?
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...
Can I simply create a .po file by hand with all words and translations and then compile it into .mo? Will that work?
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?