Skip to content
This repository has been archived by the owner on May 6, 2024. It is now read-only.

iOS - Update web view cookie with in-app selected language #1333

Closed
wants to merge 28 commits into from
Closed
Changes from 1 commit
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
d09b027
Update web view cookie with in-app selected language
mumer92 Jan 28, 2020
19a70cf
get preferred localizations from Bundle
mumer92 Jan 28, 2020
5bdfe91
make host name generic for cookie
mumer92 Jan 28, 2020
915a62b
extract root domain from host name
mumer92 Jan 29, 2020
3a7bec3
move language cookie to extension
mumer92 Jan 30, 2020
b85f53b
fix github comments, move wekwebview extension to seperate file, upda…
mumer92 Jan 31, 2020
9a97c4d
fix github comments
mumer92 Feb 4, 2020
86ab8b8
delete file
mumer92 Feb 4, 2020
49e0dd3
fix github comments
mumer92 Feb 4, 2020
5b2a243
Merge branch 'master' into umer/LEARNER-7498
mumer92 Feb 4, 2020
df7bec3
Update web view cookie with in-app selected language
mumer92 Jan 28, 2020
9a9176d
get preferred localizations from Bundle
mumer92 Jan 28, 2020
4906220
make host name generic for cookie
mumer92 Jan 28, 2020
8e9d4ea
extract root domain from host name
mumer92 Jan 29, 2020
ff1748f
move language cookie to extension
mumer92 Jan 30, 2020
6370b3b
fix github comments, move wekwebview extension to seperate file, upda…
mumer92 Jan 31, 2020
ba8bc26
fix github comments
mumer92 Feb 4, 2020
d680f2f
delete file
mumer92 Feb 4, 2020
ef87091
fix github comments
mumer92 Feb 4, 2020
fa7fdf6
Merge branch 'umer/LEARNER-7498' of github.com:edx/edx-app-ios into u…
mumer92 Feb 4, 2020
b35657f
update language cookie when app language is changed
mumer92 Feb 6, 2020
2de8811
check for device langue and cookie language
mumer92 Feb 6, 2020
faa823b
get language code
mumer92 Feb 7, 2020
4aa2d4b
cleanup
mumer92 Feb 7, 2020
bf6b1e3
add return statement
mumer92 Feb 7, 2020
ab260ca
set cache
mumer92 Feb 7, 2020
07e3ac6
compare cookie value if it exists
mumer92 Feb 13, 2020
afcf622
update cookie on language default case
mumer92 Feb 13, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
29 changes: 21 additions & 8 deletions Source/WKWebView+LanguageCookie.swift
Expand Up @@ -8,36 +8,49 @@

import Foundation

let SelectedLanguageCookieValue = "SelectedLanguageCookieValue"

extension WKWebView {
private var languageCookieName: String {
return "prod-edx-language-preference"
}

private var defaultLanguage: String {
guard let language = NSLocale.preferredLanguages.first,
Bundle.main.preferredLocalizations.contains(language) else { return "en" }
return language
}

private var storedLanguageCookieValue: String {
set {
UserDefaults.standard.set(newValue, forKey: SelectedLanguageCookieValue)
Copy link
Contributor

Choose a reason for hiding this comment

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

We can not read the language code from the existing cookie? My thinking is that there are two cases.

  1. Cookie exists
    If cookie exists then check if the cookie language value is not equal to the current app selected language then update the cookie with language otherwise do nothing.

  2. Cookie Not exist
    If cookie do not exist then create the new cookie with the app selected language the set it.

I don't think there is a need to use userDefaults whats your thoughts on it?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please have a look into updated logic.

UserDefaults.standard.synchronize()
}
get {
return UserDefaults.standard.value(forKey: SelectedLanguageCookieValue) as? String ?? ""
}
}

func loadRequest(_ request: URLRequest) {
var request = request
if #available(iOS 11.0, *) {
guard let domain = request.url?.rootDomain,
let languageCookie = HTTPCookie(properties: [
.domain: ".\(domain)",
.path: "/",
.name: languageCookieName,
.value: defaultLanguage,
.expires: NSDate(timeIntervalSinceNow: 3600000)
.domain: ".\(domain)",
.path: "/",
.name: languageCookieName,
.value: defaultLanguage,
.expires: NSDate(timeIntervalSinceNow: 3600000)
])
else {
load(request)
return
}

getCookie(with: languageCookieName) { [weak self] cookie in
if cookie == nil {
if cookie == nil || self?.storedLanguageCookieValue != self?.defaultLanguage {
self?.configuration.websiteDataStore.httpCookieStore.setCookie(languageCookie) {
self?.storedLanguageCookieValue = self?.defaultLanguage ?? ""
self?.load(request)
}
}
Expand Down