[Android] Talking Retrofit Localization, Part 2

Profile Picture
- Published on Feb 25, 2020馃審 Public

We attempt to add accept-language headers to our Retrofit apis... but are stumped. So hard to find a free API that supports localization. We tried a few different options, but not much luck.

Check out our code to add the accept-language header and http logging:

val contentType = "application/json".toMediaType()
        val retrofit = Retrofit.Builder()
            .baseUrl("https://pokeapi.co/api/v2/")
            .client(
                OkHttpClient.Builder()
                    .addNetworkInterceptor(object : Interceptor {
                        override fun intercept(chain: Interceptor.Chain): Response {
                            val builder = chain.request().newBuilder()
                            builder.addHeader("Accept-Language", "es-es")
                            val request = builder.build()
                            return chain.proceed(request)
                        }
                    })
                    .addInterceptor(logging)
                    .build()
            )
            .addConverterFactory(
                Json(configuration = JsonConfiguration.Stable.copy(strictMode = false)).asConverterFactory(
                    contentType
                )
            )
            .build()

        val service = retrofit.create(PokeService::class.java)