]> git.lyx.org Git - features.git/commitdiff
Save as Template: Also consider document language
authorJuergen Spitzmueller <spitz@lyx.org>
Sat, 23 Mar 2019 09:57:22 +0000 (10:57 +0100)
committerJuergen Spitzmueller <spitz@lyx.org>
Sat, 23 Mar 2019 10:00:13 +0000 (11:00 +0100)
Attempt to save the template in the appropriate language subfolder of
the user template directory (and propose to create it if it doesn't exist
yet).

That way, user-generated templates (1.) get the correct language attribution
in the lyxfiles dialog and (2.) users can easily generate different
language versions of a template.

src/frontends/qt4/GuiView.cpp

index 2f59145af90aa3b1a0d01d7b1fa5721779b4fed8..360a6f5b278351d4b52f46f84b0f4b143eb38bfa 100644 (file)
@@ -51,6 +51,7 @@
 #include "FuncStatus.h"
 #include "FuncRequest.h"
 #include "Intl.h"
+#include "Language.h"
 #include "Layout.h"
 #include "LayoutFile.h"
 #include "Lexer.h"
@@ -2652,6 +2653,39 @@ string const GuiView::getTemplatesPath(Buffer & b)
 {
        // We start off with the user's templates path
        string result = addPath(package().user_support().absFileName(), "templates");
+       // Check for the document language
+       string const langcode = b.params().language->code();
+       string const shortcode = langcode.substr(0, 2);
+       if (!langcode.empty() && shortcode != "en") {
+               string subpath = addPath(result, shortcode);
+               string subpath_long = addPath(result, langcode);
+               // If we have a subdirectory for the language already,
+               // navigate there
+               FileName sp = FileName(subpath);
+               if (sp.isDirectory())
+                       result = subpath;
+               else if (FileName(subpath_long).isDirectory())
+                       result = subpath_long;
+               else {
+                       // Ask whether we should create such a subdirectory
+                       docstring const text =
+                               bformat(_("It is suggested to save the template in a subdirectory\n"
+                                         "appropriate to the document language (%1$s).\n"
+                                         "This subdirectory does not exists yet.\n"
+                                         "Do you want to create it?"),
+                                       _(b.params().language->display()));
+                       if (Alert::prompt(_("Create Language Directory?"),
+                                         text, 0, 1, _("&Yes, Create"), _("&No, Save Template in Parent Directory")) == 0) {
+                               // If the user agreed, we try to create it and report if this failed.
+                               if (!sp.createDirectory(0777))
+                                       Alert::error(_("Subdirectory creation failed!"),
+                                                    _("Could not create subdirectory.\n"
+                                                      "The template will be saved in the parent directory."));
+                               else
+                                       result = subpath;
+                       }
+               }
+       }
        // Do we have a layout category?
        string const cat = b.params().baseClass() ?
                                b.params().baseClass()->category()