1 /* 2 * Copyright 2003, Axel Dörfler, axeld@pinc-software.de. 3 * Copyright 2010-2011, Oliver Tappe, zooey@hirschkaefer.de. 4 * Copyright 2012, John Scipione, jscipione@gmail.com 5 * All rights reserved. Distributed under the terms of the MIT License. 6 */ 7 8 9 #include <Autolock.h> 10 #include <Catalog.h> 11 #include <Locale.h> 12 #include <LocaleRoster.h> 13 14 15 BLocale::BLocale(const BLanguage* language, 16 const BFormattingConventions* conventions) 17 { 18 if (conventions != NULL) 19 fConventions = *conventions; 20 else 21 BLocale::Default()->GetFormattingConventions(&fConventions); 22 23 if (language != NULL) 24 fLanguage = *language; 25 else 26 BLocale::Default()->GetLanguage(&fLanguage); 27 } 28 29 30 BLocale::BLocale(const BLocale& other) 31 : 32 fConventions(other.fConventions), 33 fLanguage(other.fLanguage) 34 { 35 } 36 37 38 /*static*/ const BLocale* 39 BLocale::Default() 40 { 41 return BLocaleRoster::Default()->GetDefaultLocale(); 42 } 43 44 45 BLocale& 46 BLocale::operator=(const BLocale& other) 47 { 48 if (this == &other) 49 return *this; 50 51 BAutolock lock(fLock); 52 BAutolock otherLock(other.fLock); 53 if (!lock.IsLocked() || !otherLock.IsLocked()) 54 return *this; 55 56 fConventions = other.fConventions; 57 fLanguage = other.fLanguage; 58 59 return *this; 60 } 61 62 63 BLocale::~BLocale() 64 { 65 } 66 67 68 status_t 69 BLocale::GetCollator(BCollator* collator) const 70 { 71 if (!collator) 72 return B_BAD_VALUE; 73 74 BAutolock lock(fLock); 75 if (!lock.IsLocked()) 76 return B_ERROR; 77 78 *collator = fCollator; 79 80 return B_OK; 81 } 82 83 84 status_t 85 BLocale::GetLanguage(BLanguage* language) const 86 { 87 if (!language) 88 return B_BAD_VALUE; 89 90 BAutolock lock(fLock); 91 if (!lock.IsLocked()) 92 return B_ERROR; 93 94 *language = fLanguage; 95 96 return B_OK; 97 } 98 99 100 status_t 101 BLocale::GetFormattingConventions(BFormattingConventions* conventions) const 102 { 103 if (!conventions) 104 return B_BAD_VALUE; 105 106 BAutolock lock(fLock); 107 if (!lock.IsLocked()) 108 return B_ERROR; 109 110 *conventions = fConventions; 111 112 return B_OK; 113 } 114 115 116 const char * 117 BLocale::GetString(uint32 id) const 118 { 119 // Note: this code assumes a certain order of the string bases 120 121 BAutolock lock(fLock); 122 if (!lock.IsLocked()) 123 return ""; 124 125 if (id >= B_OTHER_STRINGS_BASE) { 126 if (id == B_CODESET) 127 return "UTF-8"; 128 129 return ""; 130 } 131 return fLanguage.GetString(id); 132 } 133 134 135 void 136 BLocale::SetFormattingConventions(const BFormattingConventions& conventions) 137 { 138 BAutolock lock(fLock); 139 if (!lock.IsLocked()) 140 return; 141 142 fConventions = conventions; 143 } 144 145 146 void 147 BLocale::SetCollator(const BCollator& newCollator) 148 { 149 BAutolock lock(fLock); 150 if (!lock.IsLocked()) 151 return; 152 153 fCollator = newCollator; 154 } 155 156 157 void 158 BLocale::SetLanguage(const BLanguage& newLanguage) 159 { 160 BAutolock lock(fLock); 161 if (!lock.IsLocked()) 162 return; 163 164 fLanguage = newLanguage; 165 } 166