Guest User

Untitled

a guest
May 23rd, 2018
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. package common.network;
  2.  
  3. import java.io.IOException;
  4. import java.util.Locale;
  5.  
  6. import okhttp3.Interceptor;
  7. import okhttp3.Request;
  8. import okhttp3.Response;
  9.  
  10. /**
  11. * Created by oleynikd on 6/9/16.
  12. */
  13. public class AcceptLanguageHeaderInterceptor implements Interceptor {
  14. @Override
  15. public Response intercept(Chain chain) throws IOException {
  16. Request originalRequest = chain.request();
  17. Request requestWithHeaders = originalRequest.newBuilder()
  18. .header("Accept-Language", localeToBcp47Language(Locale.getDefault()))
  19. .build();
  20. return chain.proceed(requestWithHeaders);
  21. }
  22.  
  23. /*
  24. * From https://github.com/apache/cordova-plugin-globalization/blob/master/src/android/Globalization.java#L140
  25. * @Description: Returns a well-formed ITEF BCP 47 language tag representing
  26. * the locale identifier for the client's current locale
  27. *
  28. * @Return: String: The BCP 47 language tag for the current locale
  29. */
  30. private static String localeToBcp47Language(Locale loc) {
  31. final char SEP = '-'; // we will use a dash as per BCP 47
  32. String language = loc.getLanguage();
  33. String region = loc.getCountry();
  34. String variant = loc.getVariant();
  35.  
  36. // special case for Norwegian Nynorsk since "NY" cannot be a variant as per BCP 47
  37. // this goes before the string matching since "NY" wont pass the variant checks
  38. if( language.equals("no") && region.equals("NO") && variant.equals("NY")){
  39. language = "nn";
  40. region = "NO";
  41. variant = "";
  42. }
  43.  
  44. if( language.isEmpty() || !language.matches("\p{Alpha}{2,8}")){
  45. language = "und"; // Follow the Locale#toLanguageTag() implementation
  46. // which says to return "und" for Undetermined
  47. }else if(language.equals("iw")){
  48. language = "he"; // correct deprecated "Hebrew"
  49. }else if(language.equals("in")){
  50. language = "id"; // correct deprecated "Indonesian"
  51. }else if(language.equals("ji")){
  52. language = "yi"; // correct deprecated "Yiddish"
  53. }
  54.  
  55. // ensure valid country code, if not well formed, it's omitted
  56. if (!region.matches("\p{Alpha}{2}|\p{Digit}{3}")) {
  57. region = "";
  58. }
  59.  
  60. // variant subtags that begin with a letter must be at least 5 characters long
  61. if (!variant.matches("\p{Alnum}{5,8}|\p{Digit}\p{Alnum}{3}")) {
  62. variant = "";
  63. }
  64.  
  65. StringBuilder bcp47Tag = new StringBuilder(language);
  66. if (!region.isEmpty()) {
  67. bcp47Tag.append(SEP).append(region);
  68. }
  69. if (!variant.isEmpty()) {
  70. bcp47Tag.append(SEP).append(variant);
  71. }
  72.  
  73. return bcp47Tag.toString();
  74. }
  75. }
  76.  
  77. private static final OkHttpClient client = new OkHttpClient.Builder()
  78. .addInterceptor(new AcceptLanguageHeaderInterceptor())
  79. .build();
  80.  
  81. Retrofit ret = new Retrofit.Builder()
  82. .client(new OkHttpClient.Builder()
  83. .addNetworkInterceptor(new Interceptor() {
  84.  
  85. @Override
  86. public Response intercept(Chain chain) throws IOException {
  87. Request.Builder builder = chain.request().newBuilder();
  88.  
  89. builder.addHeader("Accept-Language", "Your value");
  90. Request request = builder.build();
  91. Response response = chain.proceed(request);
  92.  
  93. return response;
  94. }
  95. }).build())
  96. .build();
  97.  
  98. Locale.getDefault().getDisplayLanguage();
  99.  
  100. toLanguageTag
  101. Added in API level 21
  102. String toLanguageTag ()
  103. Returns a well-formed IETF BCP 47 language tag representing this locale.
  104.  
  105. private class AcceptLanguageInterceptor(val locale: Locale) : Interceptor {
  106.  
  107. @Throws(IOException::class)
  108. override fun intercept(chain: Interceptor.Chain): okhttp3.Response = chain.proceed(chain.request().newBuilder()
  109. .header("Accept-Language", locale.toLanguageTag())
  110. .build())
  111. }
Add Comment
Please, Sign In to add comment