Attachment #462663: Patch 7: Change navigator.language to use Accept-Language for bug #55366

View | Details | Raw Unified | Return to bug 55366
Collapse All | Expand All

(-)a/dom/base/nsGlobalWindow.cpp (-10 / +50 lines)
Line     Link Here 
 Lines 25-40    Link Here 
25
 *   Brendan Eich <brendan@mozilla.org>
25
 *   Brendan Eich <brendan@mozilla.org>
26
 *   David Hyatt (hyatt@netscape.com)
26
 *   David Hyatt (hyatt@netscape.com)
27
 *   Dan Rosen <dr@netscape.com>
27
 *   Dan Rosen <dr@netscape.com>
28
 *   Vidur Apparao <vidur@netscape.com>
28
 *   Vidur Apparao <vidur@netscape.com>
29
 *   Johnny Stenback <jst@netscape.com>
29
 *   Johnny Stenback <jst@netscape.com>
30
 *   Mark Hammond <mhammond@skippinet.com.au>
30
 *   Mark Hammond <mhammond@skippinet.com.au>
31
 *   Ryan Jones <sciguyryan@gmail.com>
31
 *   Ryan Jones <sciguyryan@gmail.com>
32
 *   Jeff Walden <jwalden+code@mit.edu>
32
 *   Jeff Walden <jwalden+code@mit.edu>
33
 *   Ben Bucksch <ben.bucksch  beonex.com>
33
 *
34
 *
34
 * Alternatively, the contents of this file may be used under the terms of
35
 * Alternatively, the contents of this file may be used under the terms of
35
 * either of the GNU General Public License Version 2 or later (the "GPL"),
36
 * either of the GNU General Public License Version 2 or later (the "GPL"),
36
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
37
 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
37
 * in which case the provisions of the GPL or the LGPL are applicable instead
38
 * in which case the provisions of the GPL or the LGPL are applicable instead
38
 * of those above. If you wish to allow use of your version of this file only
39
 * of those above. If you wish to allow use of your version of this file only
39
 * under the terms of either the GPL or the LGPL, and not to allow others to
40
 * under the terms of either the GPL or the LGPL, and not to allow others to
40
 * use your version of this file under the terms of the MPL, indicate your
41
 * use your version of this file under the terms of the MPL, indicate your
 Lines 62-77    Link Here 
62
#include "nsXPIDLString.h"
63
#include "nsXPIDLString.h"
63
#include "nsJSUtils.h"
64
#include "nsJSUtils.h"
64
#include "prmem.h"
65
#include "prmem.h"
65
#include "jsapi.h"              // for JSAutoRequest
66
#include "jsapi.h"              // for JSAutoRequest
66
#include "jsdbgapi.h"           // for JS_ClearWatchPointsForObject
67
#include "jsdbgapi.h"           // for JS_ClearWatchPointsForObject
67
#include "nsReadableUtils.h"
68
#include "nsReadableUtils.h"
68
#include "nsDOMClassInfo.h"
69
#include "nsDOMClassInfo.h"
69
#include "nsContentUtils.h"
70
#include "nsContentUtils.h"
71
#include "nsCharSeparatedTokenizer.h" // for Accept-Language parsing
72
#include "nsUnicharUtils.h"
70
73
71
// Other Classes
74
// Other Classes
72
#include "nsIEventListenerManager.h"
75
#include "nsIEventListenerManager.h"
73
#include "nsEscape.h"
76
#include "nsEscape.h"
74
#include "nsStyleCoord.h"
77
#include "nsStyleCoord.h"
75
#include "nsMimeTypeArray.h"
78
#include "nsMimeTypeArray.h"
76
#include "nsNetUtil.h"
79
#include "nsNetUtil.h"
77
#include "nsICachingChannel.h"
80
#include "nsICachingChannel.h"
 Lines 9854-9882   nsNavigator::GetAppName(nsAString& aAppN Link Here 
9854
      return NS_OK;
9857
      return NS_OK;
9855
    }
9858
    }
9856
  }
9859
  }
9857
9860
9858
  aAppName.AssignLiteral("Netscape");
9861
  aAppName.AssignLiteral("Netscape");
9859
  return NS_OK;
9862
  return NS_OK;
9860
}
9863
}
9861
9864
9865
/**
9866
 * JS property navigator.language, exposed to web content.
9867
 * Take first value from Accept-Languages (HTTP header), which is
9868
 * the "content language" freely set by the user in the Pref window.
9869
 *
9870
 * Do not use UI language (chosen app locale) here.
9871
 * See RFC 2616, Section 15.1.4 "Privacy Issues Connected to Accept Headers"
9872
 *
9873
 * "en", "en-US" and "i-cherokee" and "" are valid.
9874
 * Fallback in case of invalid pref should be "" (empty string), to
9875
 * let site do fallback, e.g. to site's local language.
9876
 */
9862
NS_IMETHODIMP
9877
NS_IMETHODIMP
9863
nsNavigator::GetLanguage(nsAString& aLanguage)
9878
nsNavigator::GetLanguage(nsAString& aLanguage)
9864
{
9879
{
9865
  nsresult rv;
9880
  // e.g. "de-de, en-us,en"
9866
  nsCOMPtr<nsIHttpProtocolHandler>
9881
  const nsAdoptingString& acceptLang =
9867
    service(do_GetService(NS_NETWORK_PROTOCOL_CONTRACTID_PREFIX "http", &rv));
9882
      nsContentUtils::GetLocalizedStringPref("intl.accept_languages");
9868
  if (NS_SUCCEEDED(rv)) {
9883
  // take everything before the first "," or ";", without trailing space
9869
    nsCAutoString lang;
9884
  nsCharSeparatedTokenizer langTokenizer(acceptLang, ',');
9870
    rv = service->GetLanguage(lang);
9885
  const nsSubstring &firstLangPart = langTokenizer.nextToken();
9871
    CopyASCIItoUTF16(lang, aLanguage);
9886
  nsCharSeparatedTokenizer qTokenizer(firstLangPart, ';');
9872
  }
9887
  aLanguage.Assign(qTokenizer.nextToken());
9873
9888
9874
  return rv;
9889
  // checks and fixups
9890
  // replace "_" with "-", to avoid POSIX/Windows "en_US" notation
9891
  if (aLanguage.Length() > 2 && aLanguage[2] == PRUnichar('_'))
9892
    aLanguage.Replace(2, 1, PRUnichar('-')); // TODO replace all
9893
  // use uppercase for country part, e.g. "en-US", not "en-us", see BCP47
9894
  // only uppercase 2-letter country codes, not "zh-Hant", "de-DE-x-goethe"
9895
  if (aLanguage.Length() > 2)
9896
  {
9897
    nsCharSeparatedTokenizer localeTokenizer(aLanguage, '-');
9898
    PRInt32 pos = 0;
9899
    bool first = true;
9900
    while (localeTokenizer.hasMoreTokens())
9901
    {
9902
      const nsSubstring &code = localeTokenizer.nextToken();
9903
      if (code.Length() == 2 && !first)
9904
      {
9905
        nsAutoString upper(code);
9906
        ::ToUpperCase(upper);
9907
        aLanguage.Replace(pos, code.Length(), upper);
9908
      }
9909
      pos += code.Length() + 1; // 1 is the separator
9910
      if (first)
9911
        first = false;
9912
    }
9913
  }
9914
  return NS_OK;
9875
}
9915
}
9876
9916
9877
NS_IMETHODIMP
9917
NS_IMETHODIMP
9878
nsNavigator::GetPlatform(nsAString& aPlatform)
9918
nsNavigator::GetPlatform(nsAString& aPlatform)
9879
{
9919
{
9880
  if (!nsContentUtils::IsCallerTrustedForRead()) {
9920
  if (!nsContentUtils::IsCallerTrustedForRead()) {
9881
    const nsAdoptingCString& override =
9921
    const nsAdoptingCString& override =
9882
      nsContentUtils::GetCharPref("general.platform.override");
9922
      nsContentUtils::GetCharPref("general.platform.override");

Return to bug 55366