## General developer forum

### How to check if a localized string exist

How to check if a localized string exist

Hi,

is there a function like get_string to check if a localization string is available in the localization file?

In lib/moodlelib.php  i have found this function:

public function string_exists($identifier,$component);

This function is exactly what i'm looking for, but there is no way to call it out of the string_manager class, as i normally do with get_string function

Actually i see that the only way to check if a string exist is:

if (get_string_manager()->string_exists('pluginname', 'report_' . $delete)) {$strpluginname = get_string('pluginname', 'report_' . $delete); } take from admin/reports.php Thanks Federico Average of ratings: - Re: How to check if a localized string exist Hi Frederico, I don't understand what you are wanting to do. What if the string does not exist? What do you put in your else { } statement? Joseph Average of ratings: - Re: How to check if a localized string exist Hi, If the string does not exist I want to print a constant string, but it's not the point. The real point is: the code reported in my post does not work in my module (I'm developping a new module). It returns always false even if the string is declared in the lang file. The next question is: is there a function like get_string() to verify if a string exist in the lang file without calling the get_string_manager() function? Now i'm using this workaround$str = get_string($field->name, 'mymod'); if (strpos($str, '[[') === false) {
echo $str; } else { echo$field->name;
}

but this code outputs warning messages that i don't want.

Average of ratings: -
Re: How to check if a localized string exist

Ciao Federico,
AFAIK there's no alternative option. What's preventing you to use core_string_manager::string_exists()?
Anything related to strings is "cached" so there will be no real performance hit:

if (get_string_manager()->string_exists($field->name, 'mymod') { echo get_string($field->name, 'mymod');} else {    echo $field->name;} It will check for string in the current language so beware at your code, providing e.g. both en, the native, and it translations. HTH, Matteo Average of ratings:Useful (1) Re: How to check if a localized string exist what about trying to catch thrown exceptions in a try catch instruction ? or turning off$CFG->debug just before the call ? that is

$save=$CFG->debug;

$CFG->debug=0; do your stuff$CFG->debug = $save; Cheers Average of ratings: - Re: How to check if a localized string exist The 'right' way to do this is demonstrated by lib/pluginlib.php. E.g. if (get_string_manager()->string_exists('type_' .$type . '_plural', 'core_plugin')) {
// for most plugin types, their names are defined in core_plugin lang file
return get_string('type_' . \$type . '_plural', 'core_plugin');

} else ...

Average of ratings:Useful (1)
Re: How to check if a localized string exist

Hi,
thanks for all the reply, soon I am testing it.

Tim: i have tried exactly your code, but it doesn't work. Could it be a bug?

Soon i'll post the code i'm using.

Average of ratings: -
Re: How to check if a localized string exist

If you go to Admin -> Plugins -> Overview, that page works, so the code I gave works when in context.

Average of ratings: -
Re: How to check if a localized string exist

Sorry,

probably i have misspelled some instruction.  The code posted by Tim and Matteo works great (and that code is very similar to the code in my first post).

Thanks to all!

Average of ratings: -