Home>

There is Two Language to choose from: Russian and English .

How to implement so that, for example, the user has chosen the Russian language, the entire interface changed into Russian, if he chooses English, then everything changed into English.

How can I implement (the application is created on Pyqt5 -Graphic)?

main.py

from pyqt5 import qtcore
From Pyqt5 Import Qtgui
from pyqt5 import qtwidgets
Import Window_1
Import sys.
Import Ctypes.
Import OS.
Class Mainwindow (qtwidgets.qmainwindow):
    Def __init __ (Self):
        Qtwidgets.qmainwindow .__ INIT __ (SELF)
        Creating an object for building a menu (in another class)
        Self.Menubar= qtwidgets.qmenubar ()
        # Creating a file to save the native
        If os.name== "NT": # Verification on the name of the system
            self.config_file_name= 'settings \\ Config.ini'
        self.centralwidget= qtwidgets.qwidget () # create a central widget
        Self.SetCentralWidget (self.centralwidget)
        self.mainlayout= qtwidgets.qvboxLayout (self.centralwidget)
        # Settings
        self.setfixedsize (640, 600)
        Self.setWindowTitle ("B U G")
        self.setwindowicon (qtgui.qicon ("settings \\ bug.ico"))
        # Making an assembly
        self.widget_main= Self.create_page ()
        self.mainlayout.addwidget (Self.Widget_Main)
        Self.Create_Menubar ()
        Self.Create_Toolbar ()
        # For string condition
        self.Label_1= Qtwidgets.QLabel ("V 1.0")
        self.Label_1.SetStylesheet ("Font-Size: 18px; Color: Black;")
        self.status_1.addpermanentwidget (Self.Label_1)
        Self.Status_1.ShowMessage ("Welcome!", Msecs= 4000)
        self.setstatusbar (Self.status_1)
    DEF CREATE_PAGE (SELF):
        self.widget_page= qtwidgets.qwidget ()
        Creating Obektov
        self.button_1= qtwidgets.qpushbutton ("Go to searching for telephone \ nankers and emails")
        Self.formbox= qtwidgets.qgridlayout ()
        # Adding to Box and window
        self.formbox.addwidget (Self.Button_1)
        self.formbox.setspacing (20)
        self.widget_page.setlayout (Self.formBox)
        # Sensors and events
        self.button_1.clicalked.connect (self.clicked_button_1)
        # Make the main comonent in general
        Return Self.widget_page #
    DEF CREATE_MENUBAR (SELF):
        Creating a menu panel and string status
        self.umenu= qtwidgets.qmenubar ()
        self.status_1= qtwidgets.qstatusbar ()
        Creating a menu categories and functionality in it
        Self.Menu_Options= qtwidgets.QMENU ("Options")
        self.Menu_View= qtwidgets.QMENU ('View')
        Self.Menu_Help= Qtwidgets.QMENU ("Help")
        self.sub_menu_in_options_settings_1= qtwidgets.QMENU ("Settings")
        # Settings Statution Line
        self.options_settings_status= qtwidgets.Qaction ("Enable Status Panel")
        self.options_settings_status.setCheckable (True)
        Self.Options_settings_status.setChecked (True)
        self.sub_menu_in_options_settings_1.addaction (self.options_settings_status)
        Self.Menu_Options.addmenu (self.sub_menu_in_options_settings_1)
        Search Settings
        self.sub_menu_options_search= qtwidgets.QMENU ("Search Settings")
        self.action_search_options_1= qtwidgets.qaction ("Search for all")
        self.action_search_options_1.setcheckable (True)self.action_search_options_1.setchecked (true) self.action_search_options_2= qtwidgets.Qaction ("phone numbers")
        self.action_search_options_2.setcheckable (TRUE)
        self.action_search_options_3= qtwidgets.qaction ("Email and Account")
        self.action_search_options_3.setcheckable (True)
        self.group_action_1= qtwidgets.qactiongroup (Self.sub_menu_Options_Search)
        self.group_action_1.addaction (self.action_search_options_1) # Adding to GroupBox
        self.group_action_1.addaction (self.action_search_options_2)
        Self.group_action_1.Addaction (self.action_search_options_3)
        self.sub_menu_options_search.addaction (self.action_search_options_1) # add to invested
        self.sub_menu_options_search.addaction (self.action_search_options_2) # Menu
        Self.sub_menu_Options_Search.addaction (self.action_search_options_3)
        self.umenu_options.addmenu (self.sub_menu_options_search) # Adding on the Options menu
        # language settings
        self.delimiter_2= qtwidgets.qaction ()
        Self.Delimiter_2.Setseparator (True)
        Self.Menu_Options.addaction (self.delimiter_2)
        Self.Language_Menu= Qtwidgets.QMENU ("Language")
        Self.Language_Russian= Qtwidgets.Qaction ("Rusky (RU)")
        Self.Language_Russian.setCheckable (True)
        Self.Language_Russian.SetChecked (True)
        self.Language_ENGLISH= qtwidgets.Qaction ("English (EN)")
        Self.Language_english.setCheckable (True)
        self.group_action_6= qtwidgets.QActionGroup (Self.Language_Menu)
        self.group_action_6.addaction (Self.Language_ENGLISH)
        Self.group_action_6.addaction (Self.Language_Russian)
        Self.Language_Menu.Addaction (self.Language_english)
        Self.Language_Menu.Addaction (Self.Language_Russian)
        Self.Menu_Options.Addmenu (Self.Language_Menu)
        # Setup Tools
        self.sub_menu_view_themes= qtwidgets.qmenu ("Color Scheme")
        self.view_theme_1= qtwidgets.qaction ("Dark-blue topic")
        self.view_theme_1.setcheckable (TRUE)
        self.view_theme_2= qtwidgets.qaction ("Light gray theme")
        self.view_theme_2.setcheckable (True)
        self.view_theme_2.setchecked (True)
        self.view_theme_3= qtwidgets.qaction ("Dark brown theme")
        self.view_theme_3.setcheckable (TRUE)
        self.group_action_5= qtwidgets.QActionGroup (self.sub_menu_view_themes)
        self.group_action_5.addaction (self.view_theme_1)
        Self.group_action_5.addaction (self.view_theme_2)
        Self.group_action_5.addaction (self.view_theme_3)
        self.sub_menu_view_themes.addaction (self.view_theme_1)
        self.sub_menu_view_themes.addaction (self.view_theme_2)
        self.sub_menu_view_themes.addaction (self.view_theme_3)
        self.Menu_View.addmenu (self.sub_menu_view_themes)
        # Add a separate menu to the main menu
        Self.Menu.addmenu (Self.Menu_Options)
        Self.Menu.addmenu (Self.Menu_View)
        Self.Menu.addmenu (Self.Menu_Help)
        # Sensors
        self.options_settings_status.triggered.connect (Self.ToggleMenu)
        # Add to the window
        Self.SetMenubar (Self.Menu)
    DEF CREATE_TOOLBAR (SELF):
        self.toolbar= qtwidgets.Qtoolbar ()
        self.action_back= qtwidgets.Qtion ("Back")
        self.toolbar.addaction (Self.action_back)
        self.addtoolbar (Self.Toolbar)
    # Function for sensors
    Def ToggleMenu (Self, State):
        If state:
            self.statusbar (). Show ()
        ELSE:
            self.statusbar (). Hide ()
    Def Clicked_Button_1 (Self):
        Widget= Self.centralwidget.layout (). Itemat (0) .widget ()
        self.centralwidget.layout (). RemoveWidget (Widget)
        widget.hide ()
        Self.Window_1= window_1.page_find_numbers (Self)
        self.action_back.triggered.connect (self.clicked_action_back)
        self.mainlayout.addwidget (Self.Window_1)Def clicked_action_back (Self): Widget= Self.centralwidget.layout (). Itemat (0) .widget ()
        self.centralwidget.layout (). RemoveWidget (Widget)
        widget.hide ()
        self.widget_main= Self.create_page ()
        self.mainlayout.addwidget (Self.Widget_Main)
if __name__== '__main__':
    Myappid= 'MyCompany.myProduct.SubProduct.Version'
    ctypes.windll.shell32.SetcurrentProcessexplicitAppusermodelid (MYAPPID)
    App= Qtwidgets.Qapplication (SYS.Argv)
    App.SetWindowicon (QtGui.QiCon ("Settings \\ Bug.ico"))
    Mainwindow= Mainwindow ()
    Mainwindow.Show ()
    sys.exit (app.exec_ ())

window_1.py

from pyqt5 import qtcore
from pyqt5 import qtwidgets
Import OS.
Import Re.
Class page_find_numbers (qtwidgets.qwidget):
    Def __init __ (Self, Parent= None):
        Super () .__ INIT __ (Parent)
        Self.Parent= Parent.
        # Lists to store found data in text
        self.email_text= []
        self.telephon_text= []
        # Objects
        self.button_clear= qtwidgets.qpushbutton ("Clear")
        self.button_find= qtwidgets.qpushbutton ("Run")
        self.button_save= qtwidgets.qpushbutton ("Save \ NB TXT-File")
        self.Label_1= Qtwidgets.QLabel ("Insert the text here in which you want \ NNICT phone numbers and electronic \ NCs.")
        self.Label_2= Qtwidgets.QLabel ("Result:")
        self.textedit_1= qtwidgets.qtextedit ()
        self.textedit_2= qtwidgets.qtextedit ()
        self.textedit_2.settextinteractionFlags (qtcore.qt.textBrowserinteraction)
        self.gridbox= qtwidgets.qgridlayout ()
        # Adding to Box
        self.gridbox.addwidget (Self.Label_1, 0, 0)
        self.gridbox.addwidget (Self.Label_2, 2, 0)
        self.gridbox.addwidget (Self.Button_Find, 0, 1)
        self.gridbox.addwidget (Self.Button_Clear, 1, 1)
        self.gridbox.addwidget (self.button_save, 4, 1, alignment= qtcore.qt.aligntop)
        self.gridbox.addwidget (self.textedit_1, 1, 0)
        self.gridbox.addwidget (self.textedit_2, 4, 0)
        Self.gridbox.setverticalSpacing (18)
        self.setlayout (Self.gridbox)
        # Event sensors
        self.button_clear.clicked.connect (self.textedit_1.clear)
        self.button_clear.clicked.connect (self.textedit_2.clear)
        self.button_find.clicalked.connect (Self.toseeknomemail)
        self.button_save.clicked.connect (self.tosavenomemail)
    DEF ToseeknomeMail (Self):
        PlainText= Self.TextEdit_1.ToplainText ()
        Self.Action_Options_1= Self.Parent.Action_Search_Options_1
        self.action_Options_2= self.Parent.action_Search_Options_2.
        self.action_options_3= self.parent.action_search_options_3
        Self.status_1= self.parent.status_1
        Self.Options_Settings_status= self.parent.options_settings_status
        IF LEN (PlainText) >
 0:
            # Template for phone numbers
            inserted_text_1= rep.inpile (R '\ W? \ D? \ D \ s? \ d \ d \ d \ d? \ s? \ d \ d \ d \ s? -? \ D \ D \ s? -? \ d \ d ')
            # Chark for ACC and Email
            Email_AKK= RE.COMPILE (R '[A-ZA-Z0-9] {1.50} @ [a -z] {2,6} \. [a -z] {2,4}')
            # Collects data
            self.email_text= email_akk.findall (PlainText)
            self.telephon_text_1.findall (PlainText)
            # If the array was telephone numbers or acc and mail
            If Len (Self.Email_Text) >
 0 or Len (Self.telephon_Text) >
 0:
                # Checking the set action mode
                # Find all (action)
                If self.action_options_1.ischecked ()== True:
                    text_1= '' # for telephone numbers
                    text_2= '' # for post and accountsConvert to normal form for nomer in self.telephon_text:
                        TEXT_1 += NOMER + "\ N"
                    For email in self.email_text:
                        Text_2 += email + "\ n"
                    # Check for what will be displayed in the text
                    # Only phone numbers
                    If Len (text_1) >
 0 and len (text_2)== 0:
                        self.textedit_2.settext ("phone numbers: \ n" + text_1)
                    # Only mail
                    ELIF LEN (text_1)== 0 and Len (text_2) >
 0:
                        self.textedit_2.settext ("Email: \ n" + text_2)
                    # together
                    Elif Len (Text_1) >
 0 and len (text_2) >
 0:
                        self.textedit_2.settext ("Telephone numbers: \ n" + text_1 \
                                + "\ NElectronic Mail: \ N" + Text_2)
                    # Panel Condition
                    Self.Status_1.ShowMessage ("Telephone numbers -" + str (Len (Self.telephon_Text)) \
                           + "; Emails -" + STR (LEN (Self.Email_Text)), msecs= 3500)
                # Search phone numbers
                elif self.action_options_2.ischecked ()== True:
                    TEXT_1= ''
                    For Nomer in Self.telephon_Text:
                        TEXT_1 += NOMER + "\ N"
                    # If the data turned out to be
                    If Len (text_1) >
 0:
                        self.textedit_2.settext ("phone numbers: \ n" + text_1)
                    ELIF LEN (text_1)== 0:
                        self.textedit_2.settext ("Data Not detected!")
                    # Status panel
                    self.status_1.showMessage ("Found: phone numbers -" + Str (Len (Self.telephon_Text)), msecs= 3500)
                # Search Accounts and Mail
                elif self.action_options_3.ischecked ()== True:
                    TEXT_2= ''
                    For email in self.email_text:
                        Text_2 += email + "\ n"
                    # If the data turned out to be
                    IF LEN (text_2) >
 0:
                        self.textedit_2.settext ("Email: \ n" + text_2)
                    ELIF LEN (Text_2)== 0:
                        self.textedit_2.settext ("Data Not detected!")
                    # Status panel
                    Self.Status_1.ShowMessage ("Found: Email and Acc -" + Str (Len (Self.Email_Text)), msecs= 3500)
            # If there are no numbers in the list
            Elif Len (Self.Email_Text)== 0 and Len (self.telephon_text)== 0:
                self.textedit_2.settext ("Data Not detected!")
        # If the first text is not detected to search for
        ELIF LEN (PlainText)== 0:
            self.textedit_2.settext ("Enter the data!")
    DEF TOSAVENOMEREMAIL (SELF):
        If Len (Self.Email_Text) >
 0 or Len (Self.telephon_Text) >
 0:
            # Voice check
            if os.path.exists ("C: /Nomera_Email")== False:
                # Create folders
                OS.MKDIR ('C: /Nomera_Email')
                # Checking the set of action
                # Search for everything
                If self.action_options_1.ischecked ()== True:
                    telephon_nomer_email= open ('C: /Nomera_Email/telephonnue_nomera_email.txt', 'W')
                    # -records rooms and acc, and mailIf Len (Self.telephon_Text) > 0: # number check
                        telephon_nomer_email.write ("phone numbers: \ n")
                        For Tel_nomer in Self.telephon_Text:
                            telephon_nomer_mail.write (tel_nomer + "\ n")
                        telephon_nomer_email.write ("")
                    If Len (Self.Email_Text) >
 0: # Check on ACC or Mail
                        telephon_nomer_email.write ("\ Nakcounts, emails: \ n")
                        For email in self.email_text:
                            telephon_nomer_email.write (email + '\ n')
                        telephon_nomer_email.write ("")
                    telephon_nomer_email.close ()
                # If donated to the search for phone numbers
                elif self.action_options_2.ischecked ()== True:
                    # Record phone numbers
                    If Len (Self.telephon_Text) >
 0:
                        TELEPHON_NOMER= OPEN ('C: /Nomera_Email/telephonnue_nomera.txt', 'W')
                        telephon_nomer.write ("phone numbers: \ n")
                        For Tel_nomer in Self.telephon_Text:
                            telephon_nomer.write (tel_nomer + "\ n")
                        telephon_nomer.write (")
                        Telephon_nomer.Close ()
                # Search for post and accounts
                elif self.action_options_3.ischecked ()== True:
                    # Recording Accounts
                    If Len (Self.Email_Text) >
 0:
                        Email_File= Open ('C: /Nomera_email/email.txt', 'W')
                        email_file.write ("\ Nakcounts, emails: \ n")
                        For email in self.email_text:
                            email_file.write (email + '\ n')
                        email_file.write ("")
                        email_file.close ()
            # If the path exists
            ELIF OS.Path.Exists ("C: /Nomera_Email")== True:
                # Checking on the dality of the search mode
                If self.action_options_1.ischecked ()== True:
                    telephon_nomer_email= open ('C: /Nomera_Email/telephonnue_nomera_email.txt', 'A')
                    # Record numbers
                    If Len (Self.telephon_Text) >
 0:
                        telephon_nomer_email.write ("\ NTEphone numbers: \ n")
                        For tel_nomer_1 in self.telephon_text:
                            telephon_nomer_mail.write (tel_nomer_1 + "\ n")
                        telephon_nomer_email.write ("")
                    # Recording Accounts
                    If Len (Self.Email_Text) >
 0:
                        telephon_nomer_email.write ("\ Nakcounts and Electronic Mail: \ n")
                        For email_1 in self.email_text:
                            telephon_nomer_email.write (email_1 + '\ n')
                        telephon_nomer_email.write ("")
                    telephon_nomer_email.close ()
                elif self.action_options_2.ischecked ()== True:
                     If Len (Self.telephon_Text) >
 0:
                        TELEPHON_NOMER= OPEN ('C: /Nomera_Email/telephonnue_nomera.txt', 'A')
                        Telephon_nomer.Write ("\ NTEphone numbers: \ n")
                        For tel_nomer_1 in self.telephon_text:
                            telephon_nomer.write (tel_nomer_1 + "\ n")
                        telephon_nomer.write (")
                        Telephon_nomer.Close ()
                elif self.action_options_3.ischecked ()== True:
                    If Len (Self.Email_Text) >
 0:
                        Email_File= Open ('C: /Nomera_Email/email.txt', 'A')
                        email_file.write ("\ Nakcounts and emails: \ n")For email_1 in self.email_text: email_file.write (email_1 + '\ n')
                        email_file.write ("")
                        email_file.close ()
            # Display information after saving
            self.textedit_2.settext ("You saved in the TXT file, \ Nna way -C: \\ Nomera_email")
        # If data for saving is not detected
        Elif Len (Self.Email_Text)== 0 and Len (self.telephon_text)== 0:
            self.textedit_2.settext ("Data for saving to \ NTXT file is not detected!")
# Numbers and acc to check search
# Schoashgommashto (+5 555) 555 55 55 aprardfb 89209103333
# Vaiapp 342423 PIA 8 888 888 88-88 dfbdfg [email protected]

@Aleksandr Give your example you want to internationalize.

S. Nick2021-05-04 23:16:37

For this, there is a doc.qt.io/qt-5/qtranslator.html and doc.qt.io/qt-5/qcoreApplication.html#installtranslator. The translations themselves are performed through the QT-SHO utility Linguist

gil9red2021-05-04 23:16:37

Please advise good articles or how it does not work

Aleksandr2021-05-04 23:16:37