Skip to content

Commit

Permalink
Fix localized titles not returned by graphql because of missing 'Acce…
Browse files Browse the repository at this point in the history
…pt-Language' header

Also, map locale codes to correct presentation, e.g. from 'en-us' (returned by graphql) to 'en_us' (returned by json:api and internal app presentation).
  • Loading branch information
Drumber committed Dec 21, 2024
1 parent 843723f commit 3a8797e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,11 @@ package io.github.drumber.kitsune.data.mapper.graphql
import io.github.drumber.kitsune.data.common.Titles
import io.github.drumber.kitsune.data.source.graphql.fragment.TitlesFragment

fun TitlesFragment.toTitles(): Titles? = localized as? Map<String, String>
fun TitlesFragment.toTitles(): Titles? {
var localized = (localized as? Map<String, String>)?.mapKeys { it.key.replace('-', '_') }
if (!romanized.isNullOrBlank()) {
// add romanized title to titles map for backwards compatibility
localized = (localized ?: emptyMap()) + mapOf("en_jp" to romanized)
}
return localized
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import io.github.drumber.kitsune.constants.Kitsu
import io.github.drumber.kitsune.util.json.AlgoliaFacetValueDeserializer
import io.github.drumber.kitsune.util.json.AlgoliaNumericValueDeserializer
import io.github.drumber.kitsune.util.json.IgnoreParcelablePropertyMixin
import io.github.drumber.kitsune.util.network.AcceptLanguageHeaderInterceptor
import io.github.drumber.kitsune.util.network.AuthenticationInterceptor
import io.github.drumber.kitsune.util.network.AuthenticationInterceptorImpl
import io.github.drumber.kitsune.util.network.UserAgentInterceptor
Expand All @@ -43,6 +44,7 @@ val networkModule = module {

fun createHttpClientBuilder(addLoggingInterceptor: Boolean = true) = OkHttpClient.Builder()
.addInterceptor(createUserAgentInterceptor())
.addInterceptor(AcceptLanguageHeaderInterceptor())
.apply {
if (addLoggingInterceptor) {
addNetworkInterceptor(createHttpLoggingInterceptor())
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package io.github.drumber.kitsune.util.network

import android.os.LocaleList
import okhttp3.Interceptor
import okhttp3.Response

class AcceptLanguageHeaderInterceptor : Interceptor {

// use default system languages for now
private val acceptLanguage = LocaleList.getDefault().toLanguageTags()

override fun intercept(chain: Interceptor.Chain): Response {
val request = chain.request().newBuilder()
.header("Accept-Language", acceptLanguage)
.build()
return chain.proceed(request)
}
}

0 comments on commit 3a8797e

Please sign in to comment.