Back to index

lightning-sunbird  0.9+nobinonly
nsMacLocale.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) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsISupports.h"
00039 #include "nscore.h"
00040 #include "nsString.h"
00041 #include "nsReadableUtils.h"
00042 #include "nsILocale.h"
00043 #include "nsMacLocale.h"
00044 #include "nsLocaleCID.h"
00045 #include "prprf.h"
00046 #include <Script.h>
00047 #include "nsReadableUtils.h"
00048 
00049 struct iso_lang_map
00050 {
00051   char*  iso_code;
00052   short  mac_lang_code;
00053   short  mac_script_code;
00054 
00055 };
00056 typedef struct iso_lang_map iso_lang_map;
00057 
00058 const iso_lang_map lang_list[] = {
00059   { "sq", langAlbanian, smRoman },
00060   { "am", langAmharic, smEthiopic  },
00061   { "ar", langArabic, smArabic },
00062   { "hy", langArmenian, smArmenian},
00063   { "as", langAssamese, smBengali },
00064   { "ay", langAymara, smRoman},
00065   { "eu", langBasque, smRoman},
00066   { "bn", langBengali, smBengali },
00067   { "dz", langDzongkha, smTibetan },
00068   { "br", langBreton, smRoman },
00069   { "bg", langBulgarian, smCyrillic },
00070   { "my", langBurmese, smBurmese },
00071   { "km", langKhmer, smKhmer },
00072   { "ca", langCatalan, smRoman },
00073   { "zh", langTradChinese, smTradChinese },
00074   { "hr", langCroatian, smRoman },
00075   { "cs", langCzech, smCentralEuroRoman },
00076   { "da", langDanish, smRoman },
00077   { "nl", langDutch, smRoman },
00078   { "en", langEnglish, smRoman },
00079   { "eo", langEsperanto, smRoman },
00080   { "et", langEstonian, smCentralEuroRoman},
00081   { "fo", langFaeroese, smRoman },
00082   { "fa", langFarsi, smArabic },
00083   { "fi", langFinnish, smRoman },
00084   { "fr", langFrench, smRoman },
00085   { "ka", langGeorgian, smGeorgian },
00086   { "de", langGerman, smRoman },
00087   { "el", langGreek, smGreek },
00088   { "gn", langGuarani, smRoman },
00089   { "gu", langGujarati, smGujarati },
00090   { "he", langHebrew, smHebrew },
00091   { "iw", langHebrew, smHebrew },
00092   { "hu", langHungarian, smCentralEuroRoman }, 
00093   { "is", langIcelandic, smRoman },
00094   { "in", langIndonesian, smRoman },
00095   { "id", langIndonesian,  smRoman },
00096   { "iu", langInuktitut, smEthiopic },
00097   { "ga", langIrish, smRoman }, 
00098   { "hi", langHindi, smDevanagari },
00099   { "it", langItalian, smRoman },
00100   { "ja", langJapanese, smJapanese },
00101   { "jw", langJavaneseRom, smRoman },
00102   { "kn", langKannada, smKannada },
00103   { "ks", langKashmiri, smArabic },
00104   { "kk", langKazakh, smCyrillic },
00105   { "ky", langKirghiz, smCyrillic },
00106   { "ko", langKorean, smKorean },
00107   { "ku", langKurdish, smArabic },
00108   { "lo", langLao, smLao },
00109   { "la", langLatin, smRoman },
00110   { "lv", langLatvian, smCentralEuroRoman },
00111   { "lt", langLithuanian, smCentralEuroRoman },
00112   { "mk", langMacedonian, smCyrillic },
00113   { "mg", langMalagasy, smRoman },
00114   { "ml", langMalayalam, smMalayalam },
00115   { "mt", langMaltese, smRoman },
00116   { "mr", langMarathi, smDevanagari },
00117   { "mo", langMoldavian, smCyrillic },
00118   { "ne", langNepali, smDevanagari },
00119   { "no", langNorwegian, smRoman },
00120   { "or", langOriya, smOriya },
00121   { "om", langOromo, smEthiopic },
00122   { "ps", langPashto, smArabic },
00123   { "pl", langPolish, smCentralEuroRoman },
00124   { "pt", langPortuguese, smRoman },
00125   { "pa", langPunjabi, smGurmukhi },
00126   { "ro", langRomanian, smRoman },
00127   { "ru", langRussian, smCyrillic },
00128   { "sa", langSanskrit, smDevanagari },
00129   { "sr", langSerbian, smCyrillic },
00130   { "sd", langSindhi, smArabic },
00131   { "si", langSinhalese, smSinhalese },
00132   { "sk", langSlovak, smCentralEuroRoman },
00133   { "sl", langSlovenian, smRoman },
00134   { "so", langSomali, smRoman },
00135   { "es", langSpanish, smRoman },
00136   { "su", langSundaneseRom, smRoman },
00137   { "sw", langSwahili, smRoman },
00138   { "sv", langSwedish, smRoman }, 
00139   { "tl", langTagalog, smRoman },
00140   { "tg", langTajiki, smCyrillic },
00141   { "ta", langTamil, smTamil },
00142   { "tt", langTatar, smCyrillic },
00143   { "te", langTelugu, smTelugu },
00144   { "th", langThai, smThai },
00145   { "bo", langTibetan, smTibetan },
00146   { "ti", langTigrinya, smEthiopic },
00147   { "tr", langTurkish, smRoman },
00148   { "tk", langTurkmen, smCyrillic },
00149   { "ug", langUighur, smCyrillic },
00150   { "uk", langUkrainian, smCyrillic },
00151   { "ur", langUrdu, smArabic },
00152   { "uz", langUzbek, smCyrillic },
00153   { "vi", langVietnamese, smVietnamese },
00154   { "cy", langWelsh, smRoman },
00155   { "ji", langYiddish, smHebrew },
00156   { "yi", langYiddish, smHebrew },
00157   { nsnull, 0, 0}
00158 };
00159 
00160 struct iso_country_map
00161 {
00162   char*  iso_code;
00163   short  mac_region_code;
00164 };
00165 
00166 typedef struct iso_country_map iso_country_map;
00167 
00168 const iso_country_map country_list[] = {
00169   { "US", verUS},
00170   { "EG", verArabic},
00171   { "DZ", verArabic},
00172   { "AU", verAustralia},
00173   { "BE", verFrBelgium },
00174   { "CA", verEngCanada },
00175   { "CN", verChina },
00176   { "HR", verYugoCroatian },
00177   { "CY", verCyprus },
00178   { "DK", verDenmark },
00179   { "EE", verEstonia },
00180   { "FI", verFinland },
00181   { "FR", verFrance },
00182   { "DE", verGermany },
00183   { "EL", verGreece },
00184   { "HU", verHungary },
00185   { "IS", verIceland },
00186   { "IN", verIndiaHindi},
00187   { "IR", verIran },
00188   { "IQ", verArabic },
00189   { "IE", verIreland },
00190   { "IL", verIsrael },
00191   { "IT", verItaly },
00192   { "JP", verJapan },
00193   { "KP", verKorea },
00194   { "LV", verLatvia },
00195   { "LY", verArabic },
00196   { "LT", verLithuania },
00197   { "LU", verFrBelgiumLux },
00198   { "MT", verMalta },
00199   { "MA", verArabic },
00200   { "NL", verNetherlands },
00201   { "NO", verNorway },
00202   { "PK", verPakistan },
00203   { "PL", verPoland },
00204   { "PT", verPortugal },
00205   { "RU", verRussia },
00206   { "SA", verArabic },
00207   { "ES", verSpain },
00208   { "SE", verSweden },
00209   { "CH", verFrSwiss },
00210   { "TW", verTaiwan},
00211   { "TH", verThailand },
00212   { "TN", verArabic},
00213   { "TR", verTurkey },
00214   { "GB", verBritain },
00215   { nsnull, 0 }
00216 };
00217   
00218 
00219 /* nsMacLocale ISupports */
00220 NS_IMPL_ISUPPORTS1(nsMacLocale,nsIMacLocale)
00221 
00222 nsMacLocale::nsMacLocale(void)
00223 {
00224 }
00225 
00226 nsMacLocale::~nsMacLocale(void)
00227 {
00228 
00229 }
00230 
00231 NS_IMETHODIMP 
00232 nsMacLocale::GetPlatformLocale(const nsAString& locale, short* scriptCode, short* langCode, short* regionCode)
00233 {
00234   char  country_code[3];
00235   char  lang_code[3];
00236   char  region_code[3];
00237   bool  validCountryFound;
00238   int  i;
00239   
00240   if (!ParseLocaleString(NS_LossyConvertUTF16toASCII(locale).get(), lang_code, country_code, region_code, '-')) {
00241     *scriptCode = smRoman;
00242     *langCode = langEnglish;
00243     *regionCode = verUS;
00244     return NS_ERROR_FAILURE;
00245   }
00246   
00247   if (country_code[0]!=0) 
00248   {
00249     validCountryFound=false;
00250     for(i=0;country_list[i].iso_code;i++) {
00251       if (strcmp(country_list[i].iso_code,country_code)==0) {
00252         *regionCode = country_list[i].mac_region_code;
00253         validCountryFound=true;
00254         break;
00255       }
00256     }
00257     if (!validCountryFound) {
00258       *scriptCode = smRoman;
00259       *langCode = langEnglish;
00260       *regionCode = verUS;
00261       return NS_ERROR_FAILURE;
00262     }
00263   }
00264     
00265   for(i=0;lang_list[i].iso_code;i++) {
00266     if (strcmp(lang_list[i].iso_code, lang_code)==0) {
00267       *scriptCode = lang_list[i].mac_script_code;
00268       *langCode = lang_list[i].mac_lang_code;
00269       return NS_OK;
00270     }
00271   }
00272   return NS_ERROR_FAILURE;
00273 }
00274   
00275 NS_IMETHODIMP
00276 nsMacLocale::GetXPLocale(short scriptCode, short langCode, short regionCode, nsAString& locale)
00277 {
00278 
00279   int i;
00280   bool validResultFound = false;
00281 
00282   locale.Truncate();
00283   
00284   //
00285   // parse language
00286   //
00287   for(i=0;lang_list[i].iso_code;i++) {
00288     if (langCode==lang_list[i].mac_lang_code && scriptCode==lang_list[i].mac_script_code) {
00289       CopyASCIItoUTF16(nsDependentCString(lang_list[i].iso_code), locale);
00290       validResultFound = true;
00291       break;
00292     }
00293   }
00294   
00295   if (!validResultFound) {
00296     return NS_ERROR_FAILURE;
00297   }
00298   
00299   //
00300   // parse region
00301   //
00302   for(i=0;country_list[i].iso_code;i++) {
00303     if (regionCode==country_list[i].mac_region_code) {
00304       locale.Append(PRUnichar('-'));
00305       AppendASCIItoUTF16(country_list[i].iso_code, locale);
00306       validResultFound = true;
00307       break;
00308     }
00309   }
00310   
00311   if (validResultFound) {
00312     return NS_OK;
00313   }
00314   
00315   return NS_ERROR_FAILURE;
00316 }
00317 
00318 //
00319 // returns PR_FALSE/PR_TRUE depending on if it was of the form LL-CC-RR
00320 PRBool
00321 nsMacLocale::ParseLocaleString(const char* locale_string, char* language, char* country, char* region, char separator)
00322 {
00323   size_t    len;
00324 
00325   len = strlen(locale_string);
00326   if (len==0 || (len!=2 && len!=5 && len!=8))
00327     return PR_FALSE;
00328   
00329   if (len==2) {
00330     language[0]=locale_string[0];
00331     language[1]=locale_string[1];
00332     language[2]=0;
00333     country[0]=0;
00334     region[0]=0;
00335   } else if (len==5) {
00336     language[0]=locale_string[0];
00337     language[1]=locale_string[1];
00338     language[2]=0;
00339     country[0]=locale_string[3];
00340     country[1]=locale_string[4];
00341     country[2]=0;
00342     region[0]=0;
00343     if (locale_string[2]!=separator) return PR_FALSE;
00344   } else if (len==8) {
00345     language[0]=locale_string[0];
00346     language[1]=locale_string[1];
00347     language[2]=0;
00348     country[0]=locale_string[3];
00349     country[1]=locale_string[4];
00350     country[2]=0;
00351     region[0]=locale_string[6];
00352     region[1]=locale_string[7];
00353     region[2]=0;
00354     if (locale_string[2]!=separator || locale_string[5]!=separator) return PR_FALSE;
00355   } else {
00356     return PR_FALSE;
00357   }
00358 
00359   return PR_TRUE;
00360 }