0

I have a system plugin that will let a user set and display some custom messages to users.

As those messages are not available in a language file (are not static strings) how can I be able to allow the user to have those custom messages translated?

Thank you!

2

I might have misread the question, I provide you two answers with each interpretation I guessed.

** 1 if the strings are dynamic**

So first of all, name and use your language constants in the plugin: you might want to include placeholders. So imagine you have something like:

echo "something ".$somevar." somethingelse<br>";

you will create a constant string with placeholders:

MY_PLUGIN_SOMETHING="something ".$somevar." somethingelse<br>"

then the command would become:

printf(JText::_("MY_PLUGIN_SOMETHING"), $somevar);

Now we need to decide where to put the translations. Joomla has an awesome feature (language overrides) that will allow you to do the customization.

But what about English? Two options come to mind:

Option 1. Use JLanguage

add the ./plugins/system/myplugin/language/fr-FR/ and load it explicitly:

$language = JFactory::getLanguage();
// load the default language
$language->load('myplugin', JPATH_BASE . "/plugins/system/myplugin", 'en-GB', false, false);
// load your translations (if you provide them), if the third param is null the localized version is loaded:
$language->load('myplugin', JPATH_BASE . "/plugins/system/myplugin", null, false, false);

This is the best way to go.

Option 2: Populate the overrides in the installer

In the post flight install method you can write your default english strings to the english language override.

Note: updates may be nightmare-ish. Also, you run the risk of a user adding the placeholders wrong and breaking the whole extension.

**Conclusion on dynamic strings **

Both methods work, the first is a bit more solid. In any case, if you have strings with placeholders, you should write your code so it's resilient to placeholder problems, for example wrap it in a try..catch and return a default hardcoded message.

** 2 if the strings are customized by an admin **

There is no standard here.

The first thing that comes to mind, is create a language constant such as

MYPLUGIN_001=""

Then add buttons / iframes where you'll load the language override feature asking the user to insert any and all translations available.

Another option would be to create your custom field type, which will provide a dynamic grid where users can add many lines with language-translation. You can save the field using json it's not too hard.

I would choose this last option.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.