88 "bytes"
99 "errors"
1010 "fmt"
11- "io/ioutil"
1211 "os"
1312 "os/user"
1413 "path/filepath"
@@ -26,6 +25,7 @@ import (
2625 notifications "github.com/linuxdeepin/go-dbus-factory/session/org.freedesktop.notifications"
2726 lastore "github.com/linuxdeepin/go-dbus-factory/system/org.deepin.dde.lastore1"
2827 localehelper "github.com/linuxdeepin/go-dbus-factory/system/org.deepin.dde.localehelper1"
28+
2929 gio "github.com/linuxdeepin/go-gir/gio-2.0"
3030 "github.com/linuxdeepin/go-lib/dbusutil"
3131 . "github.com/linuxdeepin/go-lib/gettext"
@@ -45,10 +45,9 @@ const (
4545
4646var (
4747 // for locale-helper
48- _ = Tr ("Authentication is required to switch language" )
49- localeConfigFile = filepath .Join (basedir .GetUserHomeDir (), userLocaleConfigFile )
50- localeConfigFileTmp = filepath .Join (basedir .GetUserHomeDir (), userLocaleConfigFileTmp )
51- localeRegionSections = []string {"LC_CTYPE" , "LC_NUMERIC" , "LC_MONETARY" , "LC_TIME" , "LC_PAPER" , "LC_NAME" , "LC_ADDRESS" , "LC_TELEPHONE" , "LC_MEASUREMENT" }
48+ _ = Tr ("Authentication is required to switch language" )
49+ localeConfigFile = filepath .Join (basedir .GetUserHomeDir (), userLocaleConfigFile )
50+ localeConfigFileTmp = filepath .Join (basedir .GetUserHomeDir (), userLocaleConfigFileTmp )
5251)
5352
5453const (
@@ -69,23 +68,22 @@ var (
6968 // Error: not found the file
7069 //
7170 // 错误:没有此文件
72- ErrFileNotExist = fmt .Errorf ("File not exist" )
71+ ErrFileNotExist = fmt .Errorf ("file not exist" )
7372 // Error: not found the locale
7473 //
7574 // 错误:无效的 Locale
76- ErrLocaleNotFound = fmt .Errorf ("Locale not found" )
75+ ErrLocaleNotFound = fmt .Errorf ("locale not found" )
7776 // Error: changing locale failure
7877 //
7978 // 错误:修改 locale 失败
80- ErrLocaleChangeFailed = fmt .Errorf ("Changing locale failed" )
79+ ErrLocaleChangeFailed = fmt .Errorf ("changing locale failed" )
8180)
8281
8382var (
8483 //save old language notifycation data
8584 notifyTxtStartWithInstall string
8685 notifyTxtStart string
8786 notifyTxtDone string
88- notifyTxtRegionSetDone string
8987)
9088
9189//go:generate dbusutil-gen -type LangSelector locale.go
@@ -141,7 +139,6 @@ func initNotifyTxt() {
141139 notifyTxtStartWithInstall = Tr ("Changing system language and installing the required language packages, please wait..." )
142140 notifyTxtStart = Tr ("Changing system language, please wait..." )
143141 notifyTxtDone = Tr ("System language changed, please log out and then log in" )
144- notifyTxtRegionSetDone = Tr ("System region changed, please log out and then log in" )
145142}
146143
147144func newLangSelector (service * dbusutil.Service ) (* LangSelector , error ) {
@@ -308,32 +305,7 @@ func writeUserLocale(locale string) error {
308305
309306func writeLocaleEnvFile (locale , originFilename string , destFilename string ) error {
310307 var content = generateLocaleEnvFile (locale , originFilename )
311- return ioutil .WriteFile (destFilename , content , 0644 )
312- }
313-
314- func writeLocaleRegionsEnvFile (locale string , originFilename string , destFilename string ) error {
315- var (
316- localeRegionFound bool
317- infos , _ = readEnvFile (originFilename )
318- buf bytes.Buffer
319- )
320-
321- for _ , info := range infos {
322- if strv .Strv (localeRegionSections ).Contains (info .key ) {
323- localeRegionFound = true
324- info .value = locale
325- }
326-
327- buf .WriteString (fmt .Sprintf ("%s=%s\n " , info .key , info .value ))
328- }
329-
330- if ! localeRegionFound {
331- for _ , format := range localeRegionSections {
332- buf .WriteString (fmt .Sprintf ("%s=%s\n " , format , locale ))
333- }
334- }
335-
336- return ioutil .WriteFile (destFilename , buf .Bytes (), 0644 )
308+ return os .WriteFile (destFilename , content , 0644 )
337309}
338310
339311func generateLocaleEnvFile (locale , filename string ) []byte {
@@ -383,7 +355,7 @@ func getLocaleFromFile(filename string) (string, error) {
383355}
384356
385357func readEnvFile (file string ) (envInfos , error ) {
386- content , err := ioutil .ReadFile (file )
358+ content , err := os .ReadFile (file )
387359 if err != nil {
388360 return nil , err
389361 }
@@ -673,107 +645,3 @@ func (lang *LangSelector) deleteLocale(locale string) error {
673645 }
674646 return nil
675647}
676-
677- func getCurrentLocaleRegion (path string ) (string , error ) {
678- var currentLocaleRegion string
679-
680- infos , err := readEnvFile (path )
681- if err != nil {
682- return currentLocaleRegion , err
683- }
684-
685- for _ , info := range infos {
686- if strv .Strv (localeRegionSections ).Contains (info .key ) {
687- currentLocaleRegion = info .value
688- break
689- }
690- }
691-
692- return currentLocaleRegion , nil
693- }
694-
695- // 获取当前用户的locale region, 如果没有设置过,使用系统的locale region
696- func (lang * LangSelector ) getLocaleRegion () (string , error ) {
697- l , err := getCurrentLocaleRegion (localeConfigFile )
698- if err != nil {
699- logger .Warning ("failed to get current locale region" , err )
700- return lang .CurrentLocale , nil
701- }
702-
703- return l , nil
704- }
705-
706- // 根据用户选择的区域(locale)去设置对应的LC_*环境变量
707- func (lang * LangSelector ) setLocaleRegion (locale string ) {
708- currentRegion , err := lang .getLocaleRegion ()
709- if err != nil {
710- logger .Warning ("failed to get locale region" , err )
711- return
712- }
713-
714- if currentRegion == locale {
715- logger .Debugf ("the same locale Region %v, not need to set" , locale )
716- return
717- }
718-
719- lang .PropsMu .Lock ()
720- lang .setPropLocaleState (LocaleStateChanging )
721- lang .PropsMu .Unlock ()
722-
723- networkEnabled , err := isNetworkEnable ()
724- if err != nil {
725- logger .Warning (err )
726- }
727-
728- // check locale language pkg installed
729- pkg , err := lang .getInstallLangSupportPackages (locale )
730- if err != nil {
731- logger .Debug ("failed to get language support packages" , err )
732- }
733-
734- // only language support packages not installed and network enabled should install
735- isInstalled := len (pkg ) != 0
736-
737- if ! isInstalled {
738- if networkEnabled {
739- sendNotify (localeIconStart , "" , notifyTxtStartWithInstall )
740- } else {
741- sendNotify (localeIconStart , "" , notifyTxtStart )
742- }
743-
744- // generate locale
745- err = lang .generateLocale (locale )
746- if err != nil {
747- logger .Warning ("failed to generate locale:" , err )
748- sendNotify (localeIconFailed , "" ,
749- Tr ("Failed to change locale region, please try later" ))
750- lang .PropsMu .Lock ()
751- lang .setPropLocaleState (LocaleStateChanged )
752- lang .PropsMu .Unlock ()
753- return
754- } else {
755- logger .Debug ("generate locale success" )
756- }
757- }
758-
759- // install language support packages
760- if ! isInstalled && networkEnabled {
761- err = lang .installLangSupportPackages (locale )
762- if err != nil {
763- logger .Warning ("failed to install packages:" , err )
764- } else {
765- logger .Debug ("install packages success" )
766- }
767- }
768-
769- err = writeLocaleRegionsEnvFile (locale , localeConfigFile , localeConfigFileTmp )
770- if err != nil {
771- logger .Warning ("failed to write user locale formats:" , err )
772- }
773-
774- sendNotify (localeIconFinished , "" , notifyTxtRegionSetDone )
775-
776- lang .PropsMu .Lock ()
777- lang .setPropLocaleState (LocaleStateChanged )
778- lang .PropsMu .Unlock ()
779- }
0 commit comments