libobs: Add functions to get locale text from modules

Useful for sharing translated text from modules with the frontend.  This
is technically already done via properties, but it would be nice to just
be able to explicitly look up locale text directly rather than have to
go through properties.
master
jp9000 2020-08-08 10:01:17 -07:00
parent de2e89d972
commit d45017370b
4 changed files with 31 additions and 1 deletions

View File

@ -92,6 +92,8 @@ struct obs_module {
void (*unload)(void);
void (*post_load)(void);
void (*set_locale)(const char *locale);
bool (*get_string)(const char *lookup_string,
const char **translated_string);
void (*free_locale)(void);
uint32_t (*ver)(void);
void (*set_pointer)(obs_module_t *module);

View File

@ -55,9 +55,29 @@ static int load_module_exports(struct obs_module *mod, const char *path)
mod->name = os_dlsym(mod->module, "obs_module_name");
mod->description = os_dlsym(mod->module, "obs_module_description");
mod->author = os_dlsym(mod->module, "obs_module_author");
mod->get_string = os_dlsym(mod->module, "obs_module_get_string");
return MODULE_SUCCESS;
}
bool obs_module_get_locale_string(const obs_module_t *mod,
const char *lookup_string,
const char **translated_string)
{
if (mod->get_string) {
return mod->get_string(lookup_string, translated_string);
}
return false;
}
const char *obs_module_get_locale_text(const obs_module_t *mod,
const char *text)
{
const char *str = text;
obs_module_get_locale_string(mod, text, &str);
return str;
}
static inline char *get_module_name(const char *file)
{
static size_t ext_len = 0;

View File

@ -137,7 +137,7 @@ MODULE_EXTERN const char *obs_module_text(const char *lookup_string);
/** Helper function for looking up locale if default locale handler was used,
* returns true if text found, otherwise false */
MODULE_EXTERN bool obs_module_get_string(const char *lookup_string,
MODULE_EXPORT bool obs_module_get_string(const char *lookup_string,
const char **translated_string);
/** Helper function that returns the current module */

View File

@ -423,6 +423,14 @@ EXPORT bool obs_init_module(obs_module_t *module);
/** Returns a module based upon its name, or NULL if not found */
EXPORT obs_module_t *obs_get_module(const char *name);
/** Returns locale text from a specific module */
EXPORT bool obs_module_get_locale_string(const obs_module_t *mod,
const char *lookup_string,
const char **translated_string);
EXPORT const char *obs_module_get_locale_text(const obs_module_t *mod,
const char *text);
/** Logs loaded modules */
EXPORT void obs_log_loaded_modules(void);