From d45017370b76ed002ae04261fa54bc0fb53caad4 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Sat, 8 Aug 2020 10:01:17 -0700 Subject: [PATCH] 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. --- libobs/obs-internal.h | 2 ++ libobs/obs-module.c | 20 ++++++++++++++++++++ libobs/obs-module.h | 2 +- libobs/obs.h | 8 ++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/libobs/obs-internal.h b/libobs/obs-internal.h index 38b8cba6f..df38f244c 100644 --- a/libobs/obs-internal.h +++ b/libobs/obs-internal.h @@ -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); diff --git a/libobs/obs-module.c b/libobs/obs-module.c index 56ca80547..f5f984121 100644 --- a/libobs/obs-module.c +++ b/libobs/obs-module.c @@ -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; diff --git a/libobs/obs-module.h b/libobs/obs-module.h index ea57cb2ed..5f9980671 100644 --- a/libobs/obs-module.h +++ b/libobs/obs-module.h @@ -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 */ diff --git a/libobs/obs.h b/libobs/obs.h index 95eb9e025..09ab78d56 100644 --- a/libobs/obs.h +++ b/libobs/obs.h @@ -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);