Noelia 11fcd82b82 fix(Language): App crashes when language is changed
Enabled restart info popup after language is changed --> Workaround to temporary resolve the crash we have when language is changed (`startupModule` is null and some qml bindings are still calling this dead pointer) so, change language will not retranslate and instead, also on mac and win, the app will need a restart to apply the new language.

Disabled retranslations for all os on the corresponding service.

Will temporary fix #7709
2022-10-07 18:45:41 +02:00

73 lines
2.1 KiB
Nim

import NimQml
import json_serialization, chronicles, os, strformat, re
import ../../../app/global/global_singleton
import ../../../app/core/eventemitter
import ../../../app/core/signals/types
logScope:
topics = "language-service"
const SIGNAL_LANGUAGE_UPDATE* = "languageUpdated"
type
LanguageUpdatedArgs* = ref object of Args
language*: string
type
Service* = ref object of RootObj
events: EventEmitter
i18nPath: string
shouldRetranslate: bool
languages: seq[string] # list of locale names for translation purposes
proc delete*(self: Service) =
discard
proc newService*(events: EventEmitter): Service =
result = Service()
result.events = events
result.shouldRetranslate = false #not defined(linux)
proc obtainLanguages(dir: string): seq[string] =
let localeRe = re".*qml_(.*).qm"
for file in walkFiles dir & "/*.qm":
if file =~ localeRe:
result.add(matches[0])
proc currentLanguage*(): string =
singletonInstance.localAppSettings.getLanguage()
proc languages*(self: Service): seq[string] =
self.languages
proc init*(self: Service) =
try:
self.i18nPath = ""
if defined(development):
self.i18nPath = joinPath(getAppDir(), "i18n")
elif (defined(windows)):
self.i18nPath = joinPath(getAppDir(), "../resources/i18n")
elif (defined(macosx)):
self.i18nPath = joinPath(getAppDir(), "../i18n")
elif (defined(linux)):
self.i18nPath = joinPath(getAppDir(), "../i18n")
self.languages = obtainLanguages(self.i18nPath)
let language = currentLanguage()
singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{language}.qm"), self.shouldRetranslate)
except Exception as e:
let errDesription = e.msg
error "error: ", errDesription
return
proc setLanguage*(self: Service, language: string) =
if (language == singletonInstance.localAppSettings.getLanguage()):
return
singletonInstance.localAppSettings.setLanguage(language)
singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{language}.qm"), self.shouldRetranslate)
self.events.emit(SIGNAL_LANGUAGE_UPDATE, LanguageUpdatedArgs(language: language))