diff --git a/src/app/modules/main/profile_section/language/controller.nim b/src/app/modules/main/profile_section/language/controller.nim index 5a27caed47..a71dca1d8d 100644 --- a/src/app/modules/main/profile_section/language/controller.nim +++ b/src/app/modules/main/profile_section/language/controller.nim @@ -1,26 +1,34 @@ import io_interface import ../../../../../app_service/service/language/service as language_service - +import ../../../../../app/core/eventemitter type Controller* = ref object of RootObj delegate: io_interface.AccessInterface + events: EventEmitter languageService: language_service.Service proc init*(self: Controller) = - discard + self.events.on(SIGNAL_LOCALE_UPDATE) do(e: Args): + let args = LocaleUpdatedArgs(e) + self.delegate.onCurrentLocaleChanged(args.locale) -proc newController*(delegate: io_interface.AccessInterface, languageService: language_service.Service): +proc newController*(delegate: io_interface.AccessInterface, + events: EventEmitter, languageService: language_service.Service): Controller = result = Controller() result.delegate = delegate + result.events = events result.languageService = languageService proc delete*(self: Controller) = discard -proc changeLanguage*(self: Controller, locale: string) = +proc changeLocale*(self: Controller, locale: string) = self.languageService.setLanguage(locale) proc getLocales*(self: Controller): seq[string] = self.languageService.locales() + +proc getCurrentLocale*(self: Controller): string = + language_service.currentLocale() diff --git a/src/app/modules/main/profile_section/language/io_interface.nim b/src/app/modules/main/profile_section/language/io_interface.nim index 25a27e65e9..9fff544aba 100644 --- a/src/app/modules/main/profile_section/language/io_interface.nim +++ b/src/app/modules/main/profile_section/language/io_interface.nim @@ -16,7 +16,7 @@ method isLoaded*(self: AccessInterface): bool {.base.} = method getModuleAsVariant*(self: AccessInterface): QVariant {.base.} = raise newException(ValueError, "No implementation available") -method changeLanguage*(self: AccessInterface, locale: string) {.base.} = +method changeLocale*(self: AccessInterface, locale: string) {.base.} = raise newException(ValueError, "No implementation available") method setIsDDMMYYDateFormat*(self: AccessInterface, isDDMMYYDateFormat: bool) {.slot.} = @@ -25,6 +25,10 @@ method setIsDDMMYYDateFormat*(self: AccessInterface, isDDMMYYDateFormat: bool) { method setIs24hTimeFormat*(self: AccessInterface, is24hTimeFormat: bool) {.slot.} = raise newException(ValueError, "No implementation available") +method onCurrentLocaleChanged*(self: AccessInterface, locale: string) {.base.} = + raise newException(ValueError, "No implementation available") + + # View Delegate Interface # Delegate for the view must be declared here due to use of QtObject and multi # inheritance, which is not well supported in Nim. diff --git a/src/app/modules/main/profile_section/language/module.nim b/src/app/modules/main/profile_section/language/module.nim index 9b00b5e184..69bdd1844b 100644 --- a/src/app/modules/main/profile_section/language/module.nim +++ b/src/app/modules/main/profile_section/language/module.nim @@ -3,6 +3,7 @@ import NimQml, tables, sequtils, sugar, chronicles import io_interface, view, controller, item, model, locale_table import ../io_interface as delegate_interface import ../../../../global/global_singleton +import ../../../../../app/core/eventemitter import ../../../../../app_service/service/language/service as language_service @@ -17,12 +18,12 @@ type moduleLoaded: bool proc newModule*(delegate: delegate_interface.AccessInterface, - languageService: language_service.Service): Module = + events: EventEmitter, languageService: language_service.Service): Module = result = Module() result.delegate = delegate result.view = newView(result) result.viewVariant = newQVariant(result.view) - result.controller = controller.newController(result, languageService) + result.controller = controller.newController(result, events, languageService) result.moduleLoaded = false proc populateLanguageModel(self: Module) = @@ -54,14 +55,19 @@ method isLoaded*(self: Module): bool = method viewDidLoad*(self: Module) = self.populateLanguageModel() + self.view.setLocale(self.controller.getCurrentLocale()) + self.moduleLoaded = true self.delegate.languageModuleDidLoad() method getModuleAsVariant*(self: Module): QVariant = return self.viewVariant -method changeLanguage*(self: Module, locale: string) = - self.controller.changeLanguage(locale) +method changeLocale*(self: Module, locale: string) = + self.controller.changeLocale(locale) + +method onCurrentLocaleChanged*(self: Module, locale: string) = + self.view.setLocale(locale) method setIsDDMMYYDateFormat*(self: Module, isDDMMYYDateFormat: bool) = if(isDDMMYYDateFormat != singletonInstance.localAccountSensitiveSettings.getIsDDMMYYDateFormat()): @@ -70,3 +76,5 @@ method setIsDDMMYYDateFormat*(self: Module, isDDMMYYDateFormat: bool) = method setIs24hTimeFormat*(self: Module, is24hTimeFormat: bool) = if(is24hTimeFormat != singletonInstance.localAccountSensitiveSettings.getIs24hTimeFormat()): singletonInstance.localAccountSensitiveSettings.setIs24hTimeFormat(is24hTimeFormat) + + diff --git a/src/app/modules/main/profile_section/language/view.nim b/src/app/modules/main/profile_section/language/view.nim index 07f81b76e2..723506498b 100644 --- a/src/app/modules/main/profile_section/language/view.nim +++ b/src/app/modules/main/profile_section/language/view.nim @@ -8,6 +8,7 @@ QtObject: delegate: io_interface.AccessInterface model: Model modelVariant: QVariant + currentLocale: string proc delete*(self: View) = self.QObject.delete @@ -33,11 +34,25 @@ QtObject: QtProperty[QVariant] model: read = getModel - proc changeLocale*(self: View, locale: string) {.slot.} = - self.delegate.changeLanguage(locale) - proc setIsDDMMYYDateFormat*(self: View, isDDMMYYDateFormat: bool) {.slot.} = self.delegate.setIsDDMMYYDateFormat(isDDMMYYDateFormat) proc setIs24hTimeFormat*(self: View, is24hTimeFormat: bool) {.slot.} = self.delegate.setIs24hTimeFormat(is24hTimeFormat) + + proc changeLocale*(self: View, locale: string) {.slot.} = + self.delegate.changeLocale(locale) + + proc getLocale*(self: View): string {.slot.} = + self.currentLocale + + proc localeChanged*(self: View) {.signal.} + + QtProperty[string] currentLocale: + read = getLocale + notify = localeChanged + + proc setLocale*(self: View, locale: string) = + if locale != self.currentLocale: + self.currentLocale = locale + self.localeChanged() diff --git a/src/app/modules/main/profile_section/module.nim b/src/app/modules/main/profile_section/module.nim index 6c6a1f4f59..3664e91a8c 100644 --- a/src/app/modules/main/profile_section/module.nim +++ b/src/app/modules/main/profile_section/module.nim @@ -85,7 +85,7 @@ proc newModule*(delegate: delegate_interface.AccessInterface, result.profileModule = profile_module.newModule(result, profileService) result.contactsModule = contacts_module.newModule(result, events, contactsService, chatService) - result.languageModule = language_module.newModule(result, languageService) + result.languageModule = language_module.newModule(result, events, languageService) result.privacyModule = privacy_module.newModule(result, events, settingsService, privacyService, generalService) result.aboutModule = about_module.newModule(result, events, aboutService) result.advancedModule = advanced_module.newModule(result, events, settingsService, stickersService, nodeConfigurationService) diff --git a/src/app_service/service/language/service.nim b/src/app_service/service/language/service.nim index 58108e9ea7..c682c378e0 100644 --- a/src/app_service/service/language/service.nim +++ b/src/app_service/service/language/service.nim @@ -1,12 +1,21 @@ import NimQml import json, json_serialization, sequtils, chronicles, os, strformat, re import ../../../app/global/global_singleton +import ../../../app/core/eventemitter +import ../../../app/core/signals/types logScope: topics = "language-service" +const SIGNAL_LOCALE_UPDATE* = "localeUpdated" + +type + LocaleUpdatedArgs* = ref object of Args + locale*: string + type Service* = ref object of RootObj + events: EventEmitter i18nPath: string shouldRetranslate: bool locales: seq[string] @@ -24,6 +33,12 @@ proc obtainLocales(dir: string): seq[string] = if file =~ localeRe: result.add(matches[0]) +proc currentLocale*(): string = + singletonInstance.localAppSettings.getLocale() + +proc locales*(self: Service): seq[string] = + self.locales + proc init*(self: Service) = try: self.i18nPath = "" @@ -38,7 +53,7 @@ proc init*(self: Service) = self.locales = obtainLocales(self.i18nPath) - let locale = singletonInstance.localAppSettings.getLocale() + let locale = currentLocale() singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{locale}.qm"), self.shouldRetranslate) except Exception as e: @@ -53,5 +68,4 @@ proc setLanguage*(self: Service, locale: string) = singletonInstance.localAppSettings.setLocale(locale) singletonInstance.engine.setTranslationPackage(joinPath(self.i18nPath, fmt"qml_{locale}.qm"), self.shouldRetranslate) -proc locales*(self: Service): seq[string] = - self.locales + self.events.emit(SIGNAL_LOCALE_UPDATE, LocaleUpdatedArgs(locale: locale))