diff options
| author | Patrick Storz <eduard.braun2@gmx.de> | 2019-02-17 00:06:56 +0000 |
|---|---|---|
| committer | Patrick Storz <eduard.braun2@gmx.de> | 2019-02-17 00:13:52 +0000 |
| commit | b1217475b861a290b8ef783330c6b97dedadd826 (patch) | |
| tree | d097294acbe20024a3ac0a087008190f12c81aea /src | |
| parent | Tutorials: Improve logic for loading localized versions (diff) | |
| download | inkscape-b1217475b861a290b8ef783330c6b97dedadd826.tar.gz inkscape-b1217475b861a290b8ef783330c6b97dedadd826.zip | |
Move logic to build localized filenames into io/ressource.cpp
Now all that's needed to prefer a localized version of the file
(if available) is a Boolean.
Diffstat (limited to 'src')
| -rw-r--r-- | src/file.cpp | 3 | ||||
| -rw-r--r-- | src/help.cpp | 7 | ||||
| -rw-r--r-- | src/io/resource.cpp | 48 | ||||
| -rw-r--r-- | src/io/resource.h | 3 |
4 files changed, 32 insertions, 29 deletions
diff --git a/src/file.cpp b/src/file.cpp index 1de03951f..1e2271d2c 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -160,8 +160,7 @@ SPDesktop *sp_file_new(const std::string &templ) Glib::ustring sp_file_default_template_uri() { - // TRANSLATORS: translate this into "default.<your_language_code>.svg" (for instance, default.fr.svg) - return Inkscape::IO::Resource::get_filename(TEMPLATES, "default.svg", _("default.svg")); + return Inkscape::IO::Resource::get_filename(TEMPLATES, "default.svg", true); } SPDesktop* sp_file_new_default() diff --git a/src/help.cpp b/src/help.cpp index fd935e8c2..a32f8529a 100644 --- a/src/help.cpp +++ b/src/help.cpp @@ -35,11 +35,8 @@ void sp_help_about() void sp_help_open_tutorial(Glib::ustring name) { Glib::ustring filename = name + ".svg"; - // TRANSLATORS: 'en' is a ISO 639-1 language code. - // Replace with language code for your language (i.e. the name of your .po file) - Glib::ustring filename_localized = name + "." + _("en") + ".svg"; - - filename = get_filename(TUTORIALS, filename.c_str(), filename_localized.c_str()); + + filename = get_filename(TUTORIALS, filename.c_str(), true); if (!filename.empty()) { sp_file_open(filename.c_str(), nullptr, false, false); } else { diff --git a/src/io/resource.cpp b/src/io/resource.cpp index 96a110098..8c3ab65bf 100644 --- a/src/io/resource.cpp +++ b/src/io/resource.cpp @@ -18,6 +18,7 @@ #include <shlobj.h> // for SHGetSpecialFolderLocation #endif +#include <glibmm/i18n.h> #include <glibmm/miscutils.h> #include <glibmm/stringutils.h> #include <glibmm/fileutils.h> @@ -140,30 +141,37 @@ Glib::ustring get_path_ustring(Domain domain, Type type, char const *filename) * Same as get_path, but checks for file's existence and falls back * from USER to SYSTEM modes. * - * type - The type of file to get, such as extension, template, ui etc - * filename - The filename to get, i.e. preferences.xml - * locale - The local language version of the file (if needed) + * type - The type of file to get, such as extension, template, ui etc + * filename - The filename to get, i.e. preferences.xml + * localized - Prefer a localized version of the file, i.e. default.de.svg instead of default.svg. + * (will use gettext to determine the preferred language of the user) */ -Glib::ustring get_filename(Type type, char const *filename, char const *locale) +Glib::ustring get_filename(Type type, char const *filename, bool localized) { Glib::ustring result; - if(locale != nullptr) { - char *user_locale = _get_path(USER, type, locale); - char *sys_locale = _get_path(SYSTEM, type, locale); - - if (file_test(user_locale, G_FILE_TEST_EXISTS)) { - result = Glib::ustring(user_locale); - } else if(file_test(sys_locale, G_FILE_TEST_EXISTS)) { - result = Glib::ustring(sys_locale); - } - g_free(user_locale); - g_free(sys_locale); - - if(!result.empty()) { - g_info("Using translated resource file: %s", result.c_str()); - return result; - } + // TRANSLATORS: 'en' is an ISO 639-1 language code. + // Replace with language code for your language, i.e. the name of your .po file + if (localized && strcmp(_("en"), "en")) { + Glib::ustring localized_filename = filename; + localized_filename.insert(localized_filename.rfind('.'), "."); + localized_filename.insert(localized_filename.rfind('.'), _("en")); + + char *user_locale = _get_path(USER, type, localized_filename.c_str()); + char *sys_locale = _get_path(SYSTEM, type, localized_filename.c_str()); + + if (file_test(user_locale, G_FILE_TEST_EXISTS)) { + result = Glib::ustring(user_locale); + } else if(file_test(sys_locale, G_FILE_TEST_EXISTS)) { + result = Glib::ustring(sys_locale); + } + g_free(user_locale); + g_free(sys_locale); + + if(!result.empty()) { + g_info("Using translated resource file: %s", result.c_str()); + return result; + } } char *user_filename = _get_path(USER, type, filename); diff --git a/src/io/resource.h b/src/io/resource.h index c0f8cb226..fb814b946 100644 --- a/src/io/resource.h +++ b/src/io/resource.h @@ -61,8 +61,7 @@ Util::ptr_shared get_path(Domain domain, Type type, Glib::ustring get_path_ustring(Domain domain, Type type, char const *filename=nullptr); -Glib::ustring get_filename(Type type, char const *filename, - char const *locale=nullptr); +Glib::ustring get_filename(Type type, char const *filename, bool localized = false); Glib::ustring get_filename(Glib::ustring path, Glib::ustring filename); std::vector<Glib::ustring> get_filenames(Type type, |
