Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

save language prefs #333

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion mac/Focalboard/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,13 @@ import WebKit
class ViewController:
NSViewController,
WKUIDelegate,
WKScriptMessageHandler,
WKNavigationDelegate {
@IBOutlet var webView: WKWebView!
private var refreshWebViewOnLoad = true

let messageHandler = "Desktop"

override func viewDidLoad() {
super.viewDidLoad()

Expand Down Expand Up @@ -78,12 +81,19 @@ class ViewController:
)
webView.configuration.userContentController.removeAllUserScripts()
webView.configuration.userContentController.addUserScript(script)
webView.configuration.userContentController.add(self, name: messageHandler)
}

private func loadHomepage() {
let lang = UserDefaults.standard.string(forKey: "Language")
var queryLang = ""
if lang != nil {
queryLang = "?lang=\(lang!)"
}

let appDelegate = NSApplication.shared.delegate as! AppDelegate
let port = appDelegate.serverPort
let url = URL(string: "http://localhost:\(port)/")!
let url = URL(string: "http://localhost:\(port)/\(queryLang)")!
let request = URLRequest(url: url)
refreshWebViewOnLoad = true
webView.load(request)
Expand Down Expand Up @@ -190,6 +200,41 @@ class ViewController:
})
}
}

func getJsonFromString(stringMessage: String) -> Dictionary<String, Any> {
let data: Data = stringMessage.data(using: .utf8)!
do {
let json = try JSONSerialization.jsonObject(with: data) as? [String: Any]
return json!
} catch _ {
NSLog("could not deserialize json")
return [String: Any]()
}
}

func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if message.name == messageHandler {
guard let params = message.body as? [String: Any],
let messageType = params["type"] as? String,
let stringArgs = params["args"] as? String else {
NSLog("Received a message but it was not in the expected format")
return
}

let messageArgs = getJsonFromString(stringMessage: stringArgs)
switch messageType {
case "setLanguage":
guard let lang = messageArgs["lang"] as? String else {
NSLog("Invalid set language parameters")
return
}
NSLog("Saving current language %@", lang)
UserDefaults.standard.set(lang, forKey: "Language")
default:
NSLog("Unknown message type %@", messageType)
}
}
}

// HACKHACK: Fix WebView initial rendering artifacts
private func refreshWebView() {
Expand All @@ -215,6 +260,7 @@ class ViewController:
}
return nil
}


@IBAction func navigateToHome(_ sender: NSObject) {
loadHomepage()
Expand Down
3 changes: 2 additions & 1 deletion webapp/src/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import {Utils} from './utils'
import CombinedProviders from './combinedProviders'

const App = React.memo((): JSX.Element => {
const [language, setLanguage] = useState(getCurrentLanguage())
const params = (new URL(window.location.toString())).searchParams
const [language, setLanguage] = useState(getCurrentLanguage(params.get('lang')))
const [user, setUser] = useState<IUser|undefined>(undefined)
const [initialLoad, setInitialLoad] = useState(false)

Expand Down
10 changes: 8 additions & 2 deletions webapp/src/i18n.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,20 @@ export function getMessages(lang: string): {[key: string]: string} {
return messages_en
}

export function getCurrentLanguage(): string {
let lang = localStorage.getItem('language')
export function getCurrentLanguage(initialLang: string|null): string {
let lang = initialLang
if (!lang) {
lang = localStorage.getItem('language')
}
if (!lang) {
lang = navigator.language.split(/[-_]/)[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if navigator.language is not one of the supported languages? Does it just default to English? I think it does, but should smoke test this.

}
return lang
}

export function storeLanguage(lang: string): void {
// webkit is only available on wkwebviews
// eslint-disable-next-line @typescript-eslint/no-explicit-any
(window as any).webkit?.messageHandlers?.Desktop?.postMessage({type: 'setLanguage', args: JSON.stringify({lang})})
localStorage.setItem('language', lang)
}