261 self.__formsBrowser = \ |
262 self.__formsBrowser = \ |
262 e5App().getObject("ProjectBrowser").getProjectBrowser("forms") |
263 e5App().getObject("ProjectBrowser").getProjectBrowser("forms") |
263 self.__formsBrowser.addHookMethodAndMenuEntry("newForm", |
264 self.__formsBrowser.addHookMethodAndMenuEntry("newForm", |
264 self.newForm, self.trUtf8("New template...")) |
265 self.newForm, self.trUtf8("New template...")) |
265 |
266 |
266 ## self.__e5project.projectLanguageAddedByCode.connect( |
267 if self.__e5project.getProjectLanguage() == "Python2": |
267 ## self.__projectLanguageAdded) |
268 # Babel and lingua are not yet available for Python 3 |
268 ## self.__translationsBrowser = \ |
269 self.__e5project.projectLanguageAddedByCode.connect( |
269 ## e5App().getObject("ProjectBrowser").getProjectBrowser("translations") |
270 self.__projectLanguageAdded) |
270 ## self.__translationsBrowser.addHookMethodAndMenuEntry("extractMessages", |
271 self.__translationsBrowser = \ |
271 ## self.extractMessages, self.trUtf8("Extract messages")) |
272 e5App().getObject("ProjectBrowser").getProjectBrowser("translations") |
272 ## self.__translationsBrowser.addHookMethodAndMenuEntry("releaseAll", |
273 self.__translationsBrowser.addHookMethodAndMenuEntry("extractMessages", |
273 ## self.compileCatalogs, self.trUtf8("Compile all catalogs")) |
274 self.extractMessages, self.trUtf8("Extract Messages")) |
274 ## self.__translationsBrowser.addHookMethodAndMenuEntry("releaseSelected", |
275 self.__translationsBrowser.addHookMethodAndMenuEntry("releaseAll", |
275 ## self.compileSelectedCatalogs, |
276 self.compileCatalogs, self.trUtf8("Compile All Catalogs")) |
276 ## self.trUtf8("Compile selected catalogs")) |
277 self.__translationsBrowser.addHookMethodAndMenuEntry("releaseSelected", |
277 ## self.__translationsBrowser.addHookMethodAndMenuEntry("generateAll", |
278 self.compileSelectedCatalogs, |
278 ## self.updateCatalogs, self.trUtf8("Update all catalogs")) |
279 self.trUtf8("Compile Selected Catalogs")) |
279 ## self.__translationsBrowser.addHookMethodAndMenuEntry("generateSelected", |
280 self.__translationsBrowser.addHookMethodAndMenuEntry("generateAll", |
280 ## self.updateSelectedCatalogs, self.trUtf8("Update selected catalogs")) |
281 self.updateCatalogs, self.trUtf8("Update All Catalogs")) |
281 |
282 self.__translationsBrowser.addHookMethodAndMenuEntry("generateSelected", |
282 self.__hooksInstalled = True |
283 self.updateSelectedCatalogs, self.trUtf8("Update Selected Catalogs")) |
|
284 |
|
285 self.__hooksInstalled = True |
283 |
286 |
284 def projectClosedHooks(self): |
287 def projectClosedHooks(self): |
285 """ |
288 """ |
286 Public method to remove our hook methods. |
289 Public method to remove our hook methods. |
287 """ |
290 """ |
288 if self.__hooksInstalled: |
291 if self.__hooksInstalled: |
289 self.__formsBrowser.removeHookMethod("newForm") |
292 self.__formsBrowser.removeHookMethod("newForm") |
290 self.__formsBrowser = None |
293 self.__formsBrowser = None |
291 |
294 |
292 ## self.__e5project.projectLanguageAddedByCode.disconnect( |
295 self.__e5project.projectLanguageAddedByCode.disconnect( |
293 ## self.__projectLanguageAdded) |
296 self.__projectLanguageAdded) |
294 ## self.__translationsBrowser.removeHookMethod("extractMessages") |
297 self.__translationsBrowser.removeHookMethod("extractMessages") |
295 ## self.__translationsBrowser.removeHookMethod("releaseAll") |
298 self.__translationsBrowser.removeHookMethod("releaseAll") |
296 ## self.__translationsBrowser.removeHookMethod("releaseSelected") |
299 self.__translationsBrowser.removeHookMethod("releaseSelected") |
297 ## self.__translationsBrowser.removeHookMethod("generateAll") |
300 self.__translationsBrowser.removeHookMethod("generateAll") |
298 ## self.__translationsBrowser.removeHookMethod("generateSelected") |
301 self.__translationsBrowser.removeHookMethod("generateSelected") |
299 ## self.__translationsBrowser = None |
302 self.__translationsBrowser = None |
300 |
303 |
301 self.__hooksInstalled = False |
304 self.__hooksInstalled = False |
302 |
305 |
303 def newForm(self, path): |
306 def newForm(self, path): |
304 """ |
307 """ |
305 Public method to create a new form. |
308 Public method to create a new form. |
306 |
309 |
792 """ |
798 """ |
793 Private slot to show the helpviewer with the Pylons documentation. |
799 Private slot to show the helpviewer with the Pylons documentation. |
794 """ |
800 """ |
795 page = self.__plugin.getPreferences("PyramidDocUrl") |
801 page = self.__plugin.getPreferences("PyramidDocUrl") |
796 self.__ui.launchHelpViewer(page) |
802 self.__ui.launchHelpViewer(page) |
|
803 |
|
804 ################################################################## |
|
805 ## slots below implement translation functions |
|
806 ################################################################## |
|
807 |
|
808 def __getLocale(self, filename): |
|
809 """ |
|
810 Private method to extract the locale out of a file name. |
|
811 |
|
812 @param filename name of the file used for extraction (string) |
|
813 @return extracted locale (string) or None |
|
814 """ |
|
815 if self.__e5project.pdata["TRANSLATIONPATTERN"]: |
|
816 pattern = self.__e5project.pdata["TRANSLATIONPATTERN"][0]\ |
|
817 .replace("%language%", "(.*?)") |
|
818 match = re.search(pattern, filename) |
|
819 if match is not None: |
|
820 loc = match.group(1) |
|
821 return loc |
|
822 else: |
|
823 loc = None |
|
824 else: |
|
825 loc = None |
|
826 |
|
827 return loc |
|
828 |
|
829 def __normalizeList(self, filenames): |
|
830 """ |
|
831 Private method to normalize a list of file names. |
|
832 |
|
833 @param filenames list of file names to normalize (list of string) |
|
834 @return normalized file names (list of string) |
|
835 """ |
|
836 nfilenames = [] |
|
837 for filename in filenames: |
|
838 if filename.endswith(".mo"): |
|
839 filename = filename.replace(".mo", ".po") |
|
840 if filename not in nfilenames: |
|
841 nfilenames.append(filename) |
|
842 |
|
843 return nfilenames |
|
844 |
|
845 def __projectFilteredList(self, filenames): |
|
846 """ |
|
847 Private method to filter a list of file names by Pyramid project. |
|
848 |
|
849 @param filenames list of file names to be filtered (list of string) |
|
850 @return file names belonging to the current site (list of string) |
|
851 """ |
|
852 project = self.__project() |
|
853 nfilenames = [] |
|
854 for filename in filenames: |
|
855 if filename.startswith(project + os.sep): |
|
856 nfilenames.append(filename) |
|
857 |
|
858 return nfilenames |
|
859 |
|
860 def extractMessages(self): |
|
861 """ |
|
862 Public method to extract the messages catalog template file. |
|
863 """ |
|
864 title = self.trUtf8("Extract messages") |
|
865 try: |
|
866 projectPath = self.__projectPath() |
|
867 except PyramidNoProjectSelectedException: |
|
868 E5MessageBox.warning(self.__ui, |
|
869 title, |
|
870 self.trUtf8('No current Pyramid project selected or no Pyramid project' |
|
871 ' created yet. Aborting...')) |
|
872 return |
|
873 |
|
874 cmd = self.getPythonCommand() |
|
875 args = [] |
|
876 args.append("setup.py") |
|
877 args.append("extract_messages") |
|
878 |
|
879 dia = PyramidDialog(title, |
|
880 msgSuccess = \ |
|
881 self.trUtf8("\nMessages extracted successfully.")) |
|
882 res = dia.startProcess(cmd, args, projectPath) |
|
883 if res: |
|
884 dia.exec_() |
|
885 |
|
886 config = configparser.ConfigParser() |
|
887 config.read(os.path.join(projectPath, "setup.cfg")) |
|
888 potFile = config.get("extract_messages", "output_file") |
|
889 if potFile: |
|
890 self.__e5project.appendFile(os.path.join(projectPath, potFile)) |
|
891 else: |
|
892 try: |
|
893 lowerProject = self.__project().lower() |
|
894 except PyramidNoProjectSelectedException: |
|
895 E5MessageBox.warning(self.__ui, |
|
896 title, |
|
897 self.trUtf8('No current Pyramid project selected or no Pyramid' |
|
898 ' project created yet. Aborting...')) |
|
899 return |
|
900 |
|
901 self.__e5project.appendFile(os.path.join( |
|
902 projectPath, lowerProject, "locale", "%s.pot" % lowerProject)) |
|
903 |
|
904 def __projectLanguageAdded(self, code): |
|
905 """ |
|
906 Private slot handling the addition of a new language. |
|
907 |
|
908 @param code language code of the new language (string) |
|
909 """ |
|
910 title = self.trUtf8("Initializing message catalog for '%1'").arg(code) |
|
911 try: |
|
912 projectPath = self.__projectPath() |
|
913 except PyramidNoProjectSelectedException: |
|
914 E5MessageBox.warning(self.__ui, |
|
915 title, |
|
916 self.trUtf8('No current Pyramid project selected or no Pyramid project' |
|
917 ' created yet. Aborting...')) |
|
918 return |
|
919 |
|
920 cmd = self.getPythonCommand() |
|
921 args = [] |
|
922 args.append("setup.py") |
|
923 args.append("init_catalog") |
|
924 args.append("-l") |
|
925 args.append(code) |
|
926 |
|
927 dia = PyramidDialog(title, |
|
928 msgSuccess = \ |
|
929 self.trUtf8("\nMessage catalog initialized successfully.")) |
|
930 res = dia.startProcess(cmd, args, projectPath) |
|
931 if res: |
|
932 dia.exec_() |
|
933 |
|
934 langFile = self.__e4project.pdata["TRANSLATIONPATTERN"][0]\ |
|
935 .replace("%language%", code) |
|
936 self.__e5project.appendFile(langFile) |
|
937 |
|
938 def compileCatalogs(self, filenames): |
|
939 """ |
|
940 Public method to compile the message catalogs. |
|
941 |
|
942 @param filenames list of filenames (not used) |
|
943 """ |
|
944 title = self.trUtf8("Compiling message catalogs") |
|
945 try: |
|
946 projectPath = self.__projectPath() |
|
947 except PyramidNoProjectSelectedException: |
|
948 E5MessageBox.warning(self.__ui, |
|
949 title, |
|
950 self.trUtf8('No current Pyramid project selected or no Pyramid project' |
|
951 ' created yet. Aborting...')) |
|
952 return |
|
953 |
|
954 cmd = self.getPythonCommand() |
|
955 args = [] |
|
956 args.append("setup.py") |
|
957 args.append("compile_catalog") |
|
958 |
|
959 dia = PyramidDialog(title, |
|
960 msgSuccess = \ |
|
961 self.trUtf8("\nMessage catalogs compiled successfully.")) |
|
962 res = dia.startProcess(cmd, args, projectPath) |
|
963 if res: |
|
964 dia.exec_() |
|
965 |
|
966 for entry in os.walk(projectPath): |
|
967 for fileName in entry[2]: |
|
968 fullName = os.path.join(entry[0], fileName) |
|
969 if fullName.endswith('.mo'): |
|
970 self.__e5project.appendFile(fullName) |
|
971 |
|
972 def compileSelectedCatalogs(self, filenames): |
|
973 """ |
|
974 Public method to update the message catalogs. |
|
975 |
|
976 @param filenames list of file names (list of string) |
|
977 """ |
|
978 title = self.trUtf8("Compiling message catalogs") |
|
979 try: |
|
980 projectPath = self.__projectPath() |
|
981 except PyramidNoProjectSelectedException: |
|
982 E5MessageBox.warning(self.__ui, |
|
983 title, |
|
984 self.trUtf8('No current Pyramid project selected or no Pyramid project' |
|
985 ' created yet. Aborting...')) |
|
986 return |
|
987 |
|
988 argsLists = [] |
|
989 |
|
990 for filename in self.__normalizeList(self.__projectFilteredList(filenames)): |
|
991 locale = self.__getLocale(filename) |
|
992 if locale: |
|
993 args = [] |
|
994 args.append(self.getPythonCommand()) |
|
995 args.append("setup.py") |
|
996 args.append("compile_catalog") |
|
997 args.append("-l") |
|
998 args.append(locale) |
|
999 argsLists.append(args) |
|
1000 |
|
1001 if len(argsLists) == 0: |
|
1002 E5MessageBox.warning(self.__ui, |
|
1003 title, |
|
1004 self.trUtf8('No locales detected. Aborting...')) |
|
1005 return |
|
1006 |
|
1007 dia = PyramidDialog(title, |
|
1008 msgSuccess = \ |
|
1009 self.trUtf8("\nMessage catalogs compiled successfully.")) |
|
1010 res = dia.startBatchProcesses(argsLists, projectPath) |
|
1011 if res: |
|
1012 dia.exec_() |
|
1013 |
|
1014 for entry in os.walk(self.__sitePath()): |
|
1015 for fileName in entry[2]: |
|
1016 fullName = os.path.join(entry[0], fileName) |
|
1017 if fullName.endswith('.mo'): |
|
1018 self.__e5project.appendFile(fullName) |
|
1019 |
|
1020 def updateCatalogs(self, filenames): |
|
1021 """ |
|
1022 Public method to update the message catalogs. |
|
1023 |
|
1024 @param filenames list of filenames (not used) |
|
1025 """ |
|
1026 title = self.trUtf8("Updating message catalogs") |
|
1027 try: |
|
1028 projectPath = self.__projectPath() |
|
1029 except PyramidNoProjectSelectedException: |
|
1030 E5MessageBox.warning(self.__ui, |
|
1031 title, |
|
1032 self.trUtf8('No current Pyramid project selected or no Pyramid project' |
|
1033 ' created yet. Aborting...')) |
|
1034 return |
|
1035 |
|
1036 cmd = self.getPythonCommand() |
|
1037 args = [] |
|
1038 args.append("setup.py") |
|
1039 args.append("update_catalog") |
|
1040 |
|
1041 dia = PyramidDialog(title, |
|
1042 msgSuccess = \ |
|
1043 self.trUtf8("\nMessage catalogs updated successfully.")) |
|
1044 res = dia.startProcess(cmd, args, projectPath) |
|
1045 if res: |
|
1046 dia.exec_() |
|
1047 |
|
1048 def updateSelectedCatalogs(self, filenames): |
|
1049 """ |
|
1050 Public method to update the message catalogs. |
|
1051 |
|
1052 @param filenames list of filenames |
|
1053 """ |
|
1054 title = self.trUtf8("Updating message catalogs") |
|
1055 try: |
|
1056 projectPath = self.__projectPath() |
|
1057 except PyramidNoProjectSelectedException: |
|
1058 E5MessageBox.warning(self.__ui, |
|
1059 title, |
|
1060 self.trUtf8('No current Pyramid project selected or no Pyramid project' |
|
1061 ' created yet. Aborting...')) |
|
1062 return |
|
1063 |
|
1064 argsLists = [] |
|
1065 |
|
1066 for filename in self.__normalizeList(self.__projectFilteredList(filenames)): |
|
1067 locale = self.__getLocale(filename) |
|
1068 if locale: |
|
1069 args = [] |
|
1070 args.append(self.getPythonCommand()) |
|
1071 args.append("setup.py") |
|
1072 args.append("update_catalog") |
|
1073 args.append("-l") |
|
1074 args.append(locale) |
|
1075 argsLists.append(args) |
|
1076 |
|
1077 if len(argsLists) == 0: |
|
1078 E5MessageBox.warning(self.__ui, |
|
1079 title, |
|
1080 self.trUtf8('No locales detected. Aborting...')) |
|
1081 return |
|
1082 |
|
1083 dia = PyramidDialog(title, |
|
1084 msgSuccess = \ |
|
1085 self.trUtf8("\nMessage catalogs updated successfully.")) |
|
1086 res = dia.startBatchProcesses(argsLists, projectPath) |
|
1087 if res: |
|
1088 dia.exec_() |