Back to index

lightning-sunbird  0.9+nobinonly
nsAcceptLang.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2000
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Tao Cheng <tao@netscape>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "prmem.h"
00040 #include "nsIMemory.h"
00041 #include "nsIServiceManager.h"
00042 #include "nsID.h"
00043 #include "nsString.h"
00044 #include "nsReadableUtils.h"
00045 #include "nsUnicharUtils.h"
00046 #include "nsIStringBundle.h"
00047 #include "nsAcceptLang.h"
00048 
00049 /* define CID & IID */
00050 static NS_DEFINE_CID(kStringBundleServiceCID, NS_STRINGBUNDLESERVICE_CID);
00051 
00053 
00054 NS_IMPL_ISUPPORTS1(nsAcceptLang, nsIAcceptLang)
00055 
00056 nsAcceptLang::nsAcceptLang()
00057 {
00058   /* member initializers and constructor code */
00059 }
00060 
00061 nsAcceptLang::~nsAcceptLang()
00062 {
00063   /* destructor code */
00064 }
00065 
00066 // NS_IMPL_THREADSAFE_ISUPPORTS1(nsAcceptLang, nsIAcceptLang)
00067 
00068 /* wstring getAcceptLangFromLocale (in wstring aLocale); */
00069 NS_IMETHODIMP
00070 nsAcceptLang::GetAcceptLangFromLocale(const PRUnichar *aLocale, PRUnichar **_retval)
00071 {
00072   nsDependentString lc_name(aLocale);
00073   if (lc_name.Length() <=0) {
00074 #ifdef DEBUG
00075     printf("nsAcceptLang::GetAcceptLangFromLocale: aLocale is empty!");
00076 #endif
00077     // TODO: don't return; instead, use system locale: lc_name=...
00078     return NS_ERROR_FAILURE;
00079   }
00080 
00081   nsresult res;
00082 
00083        nsCOMPtr<nsIStringBundleService> sBundleService = 
00084                 do_GetService(kStringBundleServiceCID, &res);
00085        if (NS_FAILED(res) || (nsnull == sBundleService)) {
00086     return NS_ERROR_FAILURE;
00087   }
00088 
00089   nsCOMPtr<nsIStringBundle> bundle;
00090 #if 1
00091   res = sBundleService->CreateBundle("resource://gre/res/language.properties",
00092                                      getter_AddRefs(bundle));
00093 #else
00094   res = sBundleService->CreateBundle("chrome://global/locale/languageNames.properties",
00095                                      getter_AddRefs(bundle));
00096 #endif
00097   PRUnichar *ptrv = nsnull;
00098   nsAutoString  lc_tmp(aLocale);
00099   NS_NAMED_LITERAL_STRING(sAccept, ".accept");
00100   NS_NAMED_LITERAL_STRING(sTrue, "true");
00101 
00102   ToLowerCase(lc_tmp);
00103   lc_tmp.Append(sAccept);
00104   if (NS_OK == (res = bundle->GetStringFromName(lc_tmp.get(), &ptrv))) {
00105     if (sTrue.Equals(ptrv)) {
00106       // valid name already
00107       *_retval = ToNewUnicode(lc_name);
00108       return res;
00109     }
00110   }
00111 
00112   /* not in languageNames.properties; lang only?
00113    */
00114   PRInt32  dash = lc_tmp.FindChar('-');
00115   nsAutoString lang;
00116   nsAutoString country;
00117   if (dash > 0) {
00118     /* lang-country
00119      */
00120     PRInt32 count = 0;
00121     count = lc_tmp.Left(lang, dash);
00122     count = lc_tmp.Right(country, (lc_tmp.Length()-dash-1));
00123     /* ja-JP -> ja*/
00124   }
00125   else {
00126     /* ja ?? en-JP 
00127        en-JP ->ja (how about product locale or syste, locale ???)
00128        ja-EN ->ja
00129     */
00130     lang = lc_name;
00131   }
00132   
00133   // lang always in lower case; don't convert
00134   *_retval = ToNewUnicode(lang);
00135   lang.Append(sAccept);
00136   if (NS_OK == (res = bundle->GetStringFromName(lang.get(), &ptrv))) {
00137 
00138     if (sTrue.Equals(ptrv)) {
00139       /* lang is accepted */
00140       return res;
00141     }
00142   }
00143 
00144   /* unsupported lang */
00145   *_retval = nsnull;
00146   return NS_ERROR_FAILURE;
00147 }
00148 
00149 /* wstring getLocaleFromAcceptLang (in wstring aName); */
00150 NS_IMETHODIMP
00151 nsAcceptLang::GetLocaleFromAcceptLang(const PRUnichar *aName, PRUnichar **_retval)
00152 {
00153   nsresult res = NS_OK;
00154 
00155   if (!aName) {
00156     /* shall we use system locale instead */
00157     *_retval = nsnull;
00158     return NS_ERROR_FAILURE;
00159   }
00160   
00161   nsAutoString acceptLang(aName);
00162 
00163   /* TODO: need to parse accept lang since "en; q=0.3, ja,... 
00164    */
00165 
00166   /* always return lang-country 
00167    */
00168   PRInt32   dash = acceptLang.FindChar('-');
00169   if (dash > 0) {
00170     /* lang-country already */
00171     *_retval = ToNewUnicode(acceptLang);
00172     return res;
00173   }
00174   /* lang only 
00175    */
00176   nsCOMPtr<nsIStringBundleService> sBundleService = 
00177            do_GetService(kStringBundleServiceCID, &res);
00178   if (NS_FAILED(res) || (nsnull == sBundleService)) {
00179 #ifdef DEBUG
00180     printf("\n** nsAcceptLang::GetLocaleFromAcceptLang: failed to get nsIStringBundleService!! **\n");
00181 #endif
00182     return NS_ERROR_FAILURE;
00183   }
00184   
00185   nsIStringBundle *bundle = nsnull;
00186   /* shall we put the file in res/ instead ? */
00187   res = sBundleService->CreateBundle("chrome://global/locale/accept2locale.properties",
00188                                      &bundle);
00189     
00190   PRUnichar *ptrv = nsnull;
00191   if (NS_OK == (res = bundle->GetStringFromName(acceptLang.get(), &ptrv))) {
00192     
00193     // valid name already
00194     *_retval = ToNewUnicode(nsDependentString(ptrv));
00195   }
00196   else {
00197     /* shall we use system locale instead ? */
00198   }
00199   
00200   /* ja -> ja-JP
00201    * en-JP -> ?
00202    * default -> system locale, product locale, or good guess from aName?
00203    */
00204   return res;
00205 }
00206 
00207 /* wstring acceptLang2List (in wstring aName, in wstring aList); */
00208 NS_IMETHODIMP 
00209 nsAcceptLang::AcceptLang2List(const PRUnichar *aName, const PRUnichar *aList, PRUnichar **_retval)
00210 {
00211   nsresult rv = NS_OK;
00212 
00213   /* if aName is not in aList: append it
00214    */
00215 
00216   /* if aName is in aList: bring it to the front and assign higher value
00217    */
00218 
00219   return rv;                                                     
00220 }