Browse Source

libobs: Add function for default module locale

This function is used to simplify the process when using the default
locale handling for modules.  It will automatically search in the plugin
data directory associated with the specific module specified, load the
default locale text (for example english if its default language is
english), and then it will load the set locale on top of the default
locale, which will cause text to use the default locale if the desired
locale text is not found.
jp9000 11 năm trước cách đây
mục cha
commit
ac760efb57
2 tập tin đã thay đổi với 53 bổ sung0 xóa
  1. 48 0
      libobs/obs-module.c
  2. 5 0
      libobs/obs.h

+ 48 - 0
libobs/obs-module.c

@@ -101,6 +101,54 @@ void free_module(struct obs_module *mod)
 	bfree(mod->name);
 	bfree(mod->name);
 }
 }
 
 
+lookup_t obs_module_load_locale(const char *module, const char *default_locale,
+		const char *locale)
+{
+	struct dstr str    = {0};
+	lookup_t    lookup = NULL;
+
+	if (!module || !default_locale || !locale) {
+		blog(LOG_WARNING, "obs_module_load_locale: Invalid parameters");
+		return NULL;
+	}
+
+	dstr_copy(&str, module);
+	dstr_cat(&str, "/locale/");
+	dstr_cat(&str, default_locale);
+	dstr_cat(&str, ".ini");
+
+	char *file = obs_find_plugin_file(str.array);
+	if (file)
+		lookup = text_lookup_create(file);
+
+	bfree(file);
+
+	if (!lookup) {
+		blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'",
+				default_locale, module);
+		goto cleanup;
+	}
+
+	if (astrcmpi(locale, default_locale) == 0)
+		goto cleanup;
+
+	dstr_copy(&str, module);
+	dstr_cat(&str, "/locale/");
+	dstr_cat(&str, locale);
+	dstr_cat(&str, ".ini");
+
+	file = obs_find_plugin_file(str.array);
+
+	if (!text_lookup_add(lookup, file))
+		blog(LOG_WARNING, "Failed to load '%s' text for module: '%s'",
+				locale, module);
+
+	bfree(file);
+cleanup:
+	dstr_free(&str);
+	return lookup;
+}
+
 #define REGISTER_OBS_DEF(size_var, structure, dest, info)                 \
 #define REGISTER_OBS_DEF(size_var, structure, dest, info)                 \
 	do {                                                              \
 	do {                                                              \
 		struct structure data = {0};                              \
 		struct structure data = {0};                              \

+ 5 - 0
libobs/obs.h

@@ -19,6 +19,7 @@
 
 
 #include "util/c99defs.h"
 #include "util/c99defs.h"
 #include "util/bmem.h"
 #include "util/bmem.h"
+#include "util/text-lookup.h"
 #include "graphics/graphics.h"
 #include "graphics/graphics.h"
 #include "graphics/vec2.h"
 #include "graphics/vec2.h"
 #include "graphics/vec3.h"
 #include "graphics/vec3.h"
@@ -250,6 +251,10 @@ EXPORT bool obs_get_audio_info(struct audio_output_info *ai);
  */
  */
 EXPORT int obs_load_module(const char *path);
 EXPORT int obs_load_module(const char *path);
 
 
+/** Helper function for using default module locale */
+EXPORT lookup_t obs_module_load_locale(const char *module,
+		const char *default_locale, const char *locale);
+
 /**
 /**
  * Enumerates all available inputs source types.
  * Enumerates all available inputs source types.
  *
  *