save and load language state
Allan Cornet [Wed, 14 Feb 2007 15:48:23 +0000 (15:48 +0000)]
Linux in SCIHOME/.language
Windows in HKEY_CURRENT_USER\SOFTWARE\Scilab\"VERSION"\Settings\LANGUAGE

12 files changed:
scilab/etc/scilab.start
scilab/modules/core/src/c/SciEnv.c
scilab/modules/gui/src/c/wsci/registry.c
scilab/modules/localization/Makefile.am
scilab/modules/localization/includes/setgetlanguage.h
scilab/modules/localization/sci_gateway/sci_setlanguage.c
scilab/modules/localization/src/InitializeLocalization.c
scilab/modules/localization/src/LibScilab_Import.def
scilab/modules/localization/src/loadsavelanguage.c [new file with mode: 0644]
scilab/modules/localization/src/loadsavelanguage.h [new file with mode: 0644]
scilab/modules/localization/src/localization.vcproj
scilab/modules/localization/src/setgetlanguage.c

index 91e30bc..a9ff10f 100644 (file)
@@ -57,11 +57,7 @@ PWD = getcwd()
 args=sciargs(); larg=find(args=="-l")
 if larg<>[] & larg<=size(args,"*") then
   L=args(larg+1);
-  if (~setlanguage(L)) then
-    write(%io(2),[" "
-                             "Unsupported language """+L+"""."+..
-                             "Choosing default language """+getlanguage()+"""."])
-  end
+  setlanguage(L);
 end
 clear larg 
 
index b471970..7ccb0fc 100644 (file)
@@ -5,6 +5,7 @@
 /*-----------------------------------------------------------------------------------*/
 #include "machine.h"
 #include "SciEnv.h"
+#include "SCIHOME.h"
 /*-----------------------------------------------------------------------------------*/
 #ifdef _MSC_VER
        extern void SciEnvForWindows(void);
@@ -22,5 +23,6 @@ void SciEnv(void)
 #else
        SetSci();
 #endif
+       setSCIHOME();
 }
 /*-----------------------------------------------------------------------------------*/
index c7acb12..7607972 100644 (file)
@@ -35,7 +35,7 @@ void ReadRegistryTxt (LPTW lptw)
        DWORD TextColor;
     DWORD BackgroundColor;
        int Toolbar;
-       int Language;
+//     int Language;
        char TextFontName[MAX_PATH];
        int TextFontSize;
        RECT rect;
@@ -166,29 +166,29 @@ void ReadRegistryTxt (LPTW lptw)
        {
                lptw->lpmw->ShowToolBar = Toolbar;
        }
-       
-       if ( RegQueryValueEx(key, "Language", 0, NULL, (LPBYTE)&Language, &size) !=  ERROR_SUCCESS )
-       {
-               int CodeLanguageInScilabDotStar=GetLanguageCodeInScilabDotStar();
-
-               if (CodeLanguageInScilabDotStar != -1)
-               {
-                       lptw->lpmw->CodeLanguage = CodeLanguageInScilabDotStar;
-               }
-               else lptw->lpmw->CodeLanguage = 0; /* English Default*/
-       }
-       else
-       {
-               int CodeLanguageInScilabDotStar=GetLanguageCodeInScilabDotStar();
 
-               if (Language != CodeLanguageInScilabDotStar)
-               {
-                       lptw->lpmw->CodeLanguage = CodeLanguageInScilabDotStar; 
-               }
-               else
-               if  ( (Language == 0) || (Language == 1) )      lptw->lpmw->CodeLanguage = Language;
-               else lptw->lpmw->CodeLanguage = 0; /* English Default*/
-       }
+       //if ( RegQueryValueEx(key, "Language", 0, NULL, (LPBYTE)&Language, &size) !=  ERROR_SUCCESS )
+ //    {
+       //      int CodeLanguageInScilabDotStar=GetLanguageCodeInScilabDotStar();
+
+       //      if (CodeLanguageInScilabDotStar != -1)
+       //      {
+       //              lptw->lpmw->CodeLanguage = CodeLanguageInScilabDotStar;
+       //      }
+       //      else lptw->lpmw->CodeLanguage = 0; /* English Default*/
+       //}
+       //else
+       //{
+       //      int CodeLanguageInScilabDotStar=GetLanguageCodeInScilabDotStar();
+
+       //      if (Language != CodeLanguageInScilabDotStar)
+       //      {
+       //              lptw->lpmw->CodeLanguage = CodeLanguageInScilabDotStar; 
+       //      }
+       //      else
+       //      if  ( (Language == 0) || (Language == 1) )      lptw->lpmw->CodeLanguage = Language;
+       //      else lptw->lpmw->CodeLanguage = 0; /* English Default*/
+       //}
                
        lptw->lpmw->LockToolBar=FALSE;
 
@@ -220,7 +220,7 @@ void WriteRegistryTxt (LPTW lptw)
        char TextFontName[MAX_PATH];
        int TextFontSize;
        BOOL ShowButtons;
-       int LanguageCode;
+//     int LanguageCode;
        LONG TstRegCreateKeyEx=0;
 
        wsprintf(Clef,"SOFTWARE\\Scilab\\%s\\Settings",SCI_VERSION_STRING);     
@@ -259,8 +259,9 @@ void WriteRegistryTxt (LPTW lptw)
                ShowButtons=lptw->lpmw->ShowToolBar;
                RegSetValueEx(key, "ToolBar", 0, REG_DWORD, (LPBYTE)&ShowButtons, dwsize);
 
-               LanguageCode=lptw->lpmw->CodeLanguage;
+               /*LanguageCode=lptw->lpmw->CodeLanguage;
                RegSetValueEx(key, "Language", 0, REG_DWORD, (LPBYTE)&LanguageCode, dwsize);
+               */
 
                RegCloseKey(key);
        }
index 5136782..f002b96 100644 (file)
@@ -6,7 +6,8 @@
 LOCALIZATION_C_SOURCES = src/hashtable_localization.c \
 src/localization.c \
 src/setgetlanguage.c \
-src/InitializeLocalization.c
+src/InitializeLocalization.c \
+src/loadsavelanguage.c
 
 GATEWAY_C_SOURCES = sci_gateway/gw_localization.c \
 sci_gateway/sci_setlanguage.c \
index c789402..1649cd4 100644 (file)
 
 BOOL setlanguage(char *lang);
 char *getlanguage(void);
-int getcurrentlanguagecode(void);
 char *getlanguagealias(void);
+int getcurrentlanguagecode(void);
+char *getlanguagefromcode(int code);
+int getcodefromlanguage(char *language);
 
 #endif /* __SETGETLANGUAGE_H__ */
 /*-----------------------------------------------------------------------------------*/ 
\ No newline at end of file
index dcf0de6..0b357f7 100644 (file)
@@ -4,8 +4,10 @@
 /*-----------------------------------------------------------------------------------*/ 
 #include "sci_setlanguage.h"
 #include "setgetlanguage.h"
+#include "tableslanguages.h"
 #include "Scierror.h"
 #include "sciprint.h"
+#include "warningmode.h"
 /*-----------------------------------------------------------------------------------*/ 
 int C2F(sci_setlanguage) _PARAMS((char *fname,unsigned long fname_len))
 {
@@ -22,7 +24,12 @@ int C2F(sci_setlanguage) _PARAMS((char *fname,unsigned long fname_len))
                param=cstk(l1);
                if (!setlanguage(param))
                {
-                       sciprint("%s is a invalid language. see 'help setlanguage'.\n\n");
+                       if (getWarningMode())
+                       {
+                               sciprint("\nUnsupported language \"%s\".\n",param);
+                               sciprint("Choosing default language : \"%s\".\n\n",SCILABDEFAULTLANGUAGE);
+                       }
+                       
                        CreateVar(Rhs+1, "b", &n1,&n1,&l1);
                        *istk(l1)=(int)(FALSE);
                }
index f8ceb0c..43bfe19 100644 (file)
@@ -4,6 +4,7 @@
 /*-----------------------------------------------------------------------------------*/ \r
 #include "InitializeLocalization.h"\r
 #include "localization.h"\r
+#include "loadsavelanguage.h"\r
 #include "setgetSCIpath.h"\r
 #include "MALLOC.h"\r
 /*-----------------------------------------------------------------------------------*/ \r
@@ -14,6 +15,9 @@ BOOL InitializeLocalization(void)
        char *SCIPATH=NULL;\r
        SCIPATH=getSCIpath();\r
        InitializeHashTableScilabErrors(SCIPATH);\r
+\r
+       loadlanguagepref();\r
+\r
        if (SCIPATH) {FREE(SCIPATH);SCIPATH=NULL;}\r
 \r
        return bOK;\r
index 04f16dd..ab86b21 100644 (file)
@@ -19,5 +19,7 @@ EXPORTS
  vstk_
  createvar_
  getSCIpath
+ getSCIHOME
+ getWarningMode
  
  
\ No newline at end of file
diff --git a/scilab/modules/localization/src/loadsavelanguage.c b/scilab/modules/localization/src/loadsavelanguage.c
new file mode 100644 (file)
index 0000000..39fa31d
--- /dev/null
@@ -0,0 +1,200 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#if _MSC_VER\r
+#include <Windows.h>\r
+#include "version.h"\r
+#endif \r
+#include <stdio.h>\r
+#include "loadsavelanguage.h"\r
+#include "setgetlanguage.h"\r
+#include "SCIHOME.h"\r
+#include "MALLOC.h"\r
+/*-----------------------------------------------------------------------------------*/ \r
+#define LINELENGTHMAX 1024\r
+#define TAGLANGUAGE "LANGUAGE"\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifdef _MSC_VER\r
+static BOOL loadlanguagepref_windows(void);\r
+static BOOL savelanguagepref_windows(void);\r
+#else\r
+static BOOL loadlanguagepref_linux(void);\r
+static BOOL savelanguagepref_linux(void);\r
+static char *getfilenamelanguagepref(void);\r
+#endif\r
+/*-----------------------------------------------------------------------------------*/ \r
+BOOL loadlanguagepref(void)\r
+{\r
+       #ifdef _MSC_VER\r
+               return loadlanguagepref_windows();\r
+       #else\r
+               return loadlanguagepref_linux();\r
+       #endif\r
+}\r
+/*-----------------------------------------------------------------------------------*/ \r
+BOOL savelanguagepref(void)\r
+{\r
+       #ifdef _MSC_VER\r
+               return savelanguagepref_windows();\r
+       #else\r
+               return savelanguagepref_linux();\r
+       #endif\r
+}\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef _MSC_VER\r
+static BOOL loadlanguagepref_linux(void)\r
+{\r
+       BOOL bOK=FALSE;\r
+       char *filepref=NULL;\r
+       FILE *fileR;\r
+\r
+       filepref=getfilenamelanguagepref();\r
+\r
+       if (filepref)\r
+       {\r
+               fileR= fopen(filepref, "rt");\r
+               if (fileR)\r
+               {\r
+                       char *LANGUAGE=NULL;\r
+                       char LINE[LINELENGTHMAX];\r
+                       \r
+                       LANGUAGE=(char*)MALLOC(sizeof(char)*LINELENGTHMAX);\r
+                       fgets(LINE, LINELENGTHMAX, fileR) ;\r
+                       strncpy(LANGUAGE,&LINE[strlen(TAGLANGUAGE)+1],strlen(LINE)-strlen(TAGLANGUAGE)-2);\r
+\r
+                       setlanguage(LANGUAGE);\r
+\r
+                       fclose(fileR);\r
+\r
+                       if (LANGUAGE){FREE(LANGUAGE);LANGUAGE=NULL;}\r
+                       bOK=TRUE;\r
+               }\r
+       }\r
+\r
+       return bOK;\r
+}\r
+#endif\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef _MSC_VER\r
+static BOOL savelanguagepref_linux(void)\r
+{\r
+       BOOL bOK=FALSE;\r
+       char *filepref=NULL;\r
+       FILE *fileW;\r
+\r
+       filepref=getfilenamelanguagepref();\r
+\r
+       if (filepref)\r
+       {\r
+               fileW= fopen(filepref, "wt");\r
+               if (fileW)\r
+               {\r
+                       char LINE[LINELENGTHMAX];\r
+                       char *LANGUAGE=NULL;\r
+                       LANGUAGE=getlanguage();\r
+                       sprintf(LINE,"%s=%s\n",TAGLANGUAGE,LANGUAGE);\r
+                       fputs(LINE,fileW);\r
+                       fclose(fileW);\r
+                       \r
+                       if (LANGUAGE){FREE(LANGUAGE);LANGUAGE=NULL;}\r
+                       bOK=TRUE;\r
+               }\r
+       }\r
+       return bOK;\r
+}\r
+#endif\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef _MSC_VER\r
+static char *getfilenamelanguagepref(void)\r
+{\r
+       #if _MSC_VER\r
+               #define namelanguagepref "\\language.ini"\r
+       #else\r
+               #define namelanguagepref "/.language"\r
+       #endif\r
+\r
+       char *retFilename=NULL;\r
+       char *SCIHOME=NULL;\r
+\r
+       SCIHOME=getSCIHOME();\r
+\r
+       retFilename=(char*)MALLOC((strlen(namelanguagepref)+strlen(SCIHOME)+1)*sizeof(char));\r
+       sprintf(retFilename,"%s%s",SCIHOME,namelanguagepref);\r
+\r
+       return retFilename;\r
+}\r
+#endif\r
+/*-----------------------------------------------------------------------------------*/ \r
+#if _MSC_VER\r
+static BOOL loadlanguagepref_windows(void)\r
+{\r
+       BOOL bOK=FALSE;\r
+       /* Modification Restauration depuis la base de registre dans \r
+       HKEY_CURRENT_USER\\SOFTWARE\\Scilab\\"VERSION"\\Settings\\LANGUAGE\r
+       "Version" correspondant à la version de Scilab\r
+       */\r
+       HKEY key;\r
+       \r
+       char Clef[LINELENGTHMAX];\r
+       int LanguageCode=0;\r
+\r
+       wsprintf(Clef,"SOFTWARE\\Scilab\\%s\\Settings",SCI_VERSION_STRING);\r
+\r
+       if ( RegOpenKeyEx(HKEY_CURRENT_USER, Clef, 0, KEY_QUERY_VALUE , &key) == ERROR_SUCCESS )\r
+       {\r
+               DWORD size=4;\r
+               if ( RegQueryValueEx(key, TAGLANGUAGE, 0, NULL, (LPBYTE)&LanguageCode, &size) ==  ERROR_SUCCESS )\r
+               {\r
+                       char *LANGUAGE=NULL;\r
+                       LANGUAGE=getlanguagefromcode(LanguageCode);\r
+\r
+                       if (LANGUAGE)\r
+                       {\r
+                               setlanguage(LANGUAGE);\r
+                               bOK=TRUE;\r
+                               FREE(LANGUAGE);\r
+                               LANGUAGE=NULL;\r
+                       }\r
+               }\r
+               RegCloseKey(key);\r
+       }\r
+       return bOK;\r
+}\r
+#endif\r
+/*-----------------------------------------------------------------------------------*/ \r
+#if _MSC_VER\r
+static BOOL savelanguagepref_windows(void)\r
+{\r
+       BOOL bOK=FALSE;\r
+       /* Modification Sauvegarde dans la base de registre dans \r
+       HKEY_CURRENT_USER\\SOFTWARE\\Scilab\\"VERSION"\\Settings\\LANGUAGE\r
+       "Version" correspondant à la version de Scilab\r
+       Sauvegarde dans HKEY_CURRENT_USER car données dépendant de l'utilisateur\r
+       */\r
+       HKEY key;\r
+       DWORD result,dwsize=4;\r
+       char Clef[LINELENGTHMAX];\r
+       char *LANGUAGE=NULL;\r
+       int LanguageCode=0;\r
+       LONG TstRegCreateKeyEx=0;\r
+\r
+       LANGUAGE=getlanguage();\r
+       LanguageCode=getcodefromlanguage(LANGUAGE);\r
+       if (LANGUAGE) {FREE(LANGUAGE);LANGUAGE=NULL;}\r
+\r
+       wsprintf(Clef,"SOFTWARE\\Scilab\\%s\\Settings",SCI_VERSION_STRING);\r
+\r
+       if (RegCreateKeyEx(HKEY_CURRENT_USER, Clef, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &key, &result) == ERROR_SUCCESS)\r
+       {\r
+               if ( RegSetValueEx(key, TAGLANGUAGE, 0, REG_DWORD, (LPBYTE)&LanguageCode, dwsize) == ERROR_SUCCESS)\r
+               {\r
+                       bOK=TRUE;\r
+               }\r
+               RegCloseKey(key);\r
+       }\r
+       return bOK;\r
+}\r
+#endif\r
+/*-----------------------------------------------------------------------------------*/ \r
+\r
diff --git a/scilab/modules/localization/src/loadsavelanguage.h b/scilab/modules/localization/src/loadsavelanguage.h
new file mode 100644 (file)
index 0000000..66aadc0
--- /dev/null
@@ -0,0 +1,15 @@
+/*-----------------------------------------------------------------------------------*/\r
+/* INRIA 2007 */\r
+/* Allan CORNET */\r
+/*-----------------------------------------------------------------------------------*/ \r
+#ifndef __LOADSAVELANGUAGE_H__\r
+#define __LOADSAVELANGUAGE_H__\r
+\r
+#include "machine.h"\r
+\r
+BOOL loadlanguagepref(void);\r
+BOOL savelanguagepref(void);\r
+\r
+#endif /* __LOADSAVELANGUAGE_H__ */\r
+/*-----------------------------------------------------------------------------------*/ \r
+\r
index 461f857..f87a171 100644 (file)
@@ -42,7 +42,7 @@
                        <Tool
                                Name="VCCLCompilerTool"
                                Optimization="0"
-                               AdditionalIncludeDirectories="../includes;../../core/includes;../../../libs/hashtable;../../../libs/MALLOC/includes;../../../libs/libxml2;../../../libs/libxml2/libxml"
+                               AdditionalIncludeDirectories="../src;../includes;../../core/includes;../../../libs/hashtable;../../../libs/MALLOC/includes;../../../libs/libxml2;../../../libs/libxml2/libxml"
                                PreprocessorDefinitions="FORDLL;_DLL;STRICT;_DEBUG;_CRT_SECURE_NO_DEPRECATE;EXPORT_LOCALIZATION_DLL"
                                BasicRuntimeChecks="0"
                                RuntimeLibrary="3"
                                Optimization="2"
                                InlineFunctionExpansion="1"
                                FavorSizeOrSpeed="1"
-                               AdditionalIncludeDirectories="../includes;../../core/includes;../../../libs/hashtable;../../../libs/MALLOC/includes;../../../libs/libxml2;../../../libs/libxml2/libxml"
+                               AdditionalIncludeDirectories="../src;../includes;../../core/includes;../../../libs/hashtable;../../../libs/MALLOC/includes;../../../libs/libxml2;../../../libs/libxml2/libxml"
                                PreprocessorDefinitions="FORDLL;EXPORT_LOCALIZATION_DLL;STRICT;NDEBUG;_CRT_SECURE_NO_DEPRECATE"
                                StringPooling="true"
                                RuntimeLibrary="2"
                                >
                        </File>
                        <File
+                               RelativePath=".\loadsavelanguage.c"
+                               >
+                       </File>
+                       <File
                                RelativePath=".\localization.c"
                                >
                        </File>
                                >
                        </File>
                        <File
+                               RelativePath=".\loadsavelanguage.h"
+                               >
+                       </File>
+                       <File
                                RelativePath="..\includes\localization.h"
                                >
                        </File>
index 2f63cc9..3f14785 100644 (file)
@@ -6,6 +6,7 @@
 #include "setgetlanguage.h"
 #include "MALLOC.h"
 #include "tableslanguages.h"
+#include "loadsavelanguage.h"
 /*-----------------------------------------------------------------------------------*/ 
 static char CURRENTLANGUAGESTRING[LengthAlphacode]=SCILABDEFAULTLANGUAGE;
 static int  CURRENTLANGUAGECODE=SCILABDEFAULTLANGUAGECODE;
@@ -53,6 +54,7 @@ BOOL setlanguage(char *lang)
                {
                        strcpy(CURRENTLANGUAGESTRING,LANGUAGETMP);
                        setlanguagecode(LANGUAGETMP);
+                       savelanguagepref();
                        bOK=TRUE;
                }
        }
@@ -74,6 +76,28 @@ int getcurrentlanguagecode(void)
        return CURRENTLANGUAGECODE;
 }
 /*-----------------------------------------------------------------------------------*/ 
+char *getlanguagefromcode(int code)
+{
+       char *RetLanguage=NULL;
+       int i=0;
+
+       for (i=0;i<NumberLanguages;i++)
+       {
+               if (LANGUAGE_COUNTRY_TAB[i].code == code)
+               {
+                       RetLanguage=(char*)MALLOC(sizeof(char)*(strlen(LANGUAGE_COUNTRY_TAB[i].alphacode)+1));
+                       strcpy(RetLanguage,LANGUAGE_COUNTRY_TAB[i].alphacode);
+                       return RetLanguage;
+               }
+       }
+       return RetLanguage;
+}
+/*-----------------------------------------------------------------------------------*/ 
+int getcodefromlanguage(char *language)
+{
+       return FindLanguageCode(language);
+}
+/*-----------------------------------------------------------------------------------*/ 
 static BOOL LanguageIsOK(char *lang)
 {
        BOOL bOK=FALSE;