Ben Murdoch | 85b7179 | 2012-04-11 18:30:58 +0100 | [diff] [blame^] | 1 | // Copyright 2011 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #ifndef V8_EXTENSIONS_EXPERIMENTAL_LANGUAGE_MATCHER_H_ |
| 29 | #define V8_EXTENSIONS_EXPERIMENTAL_LANGUAGE_MATCHER_H_ |
| 30 | |
| 31 | #include "include/v8.h" |
| 32 | |
| 33 | #include "unicode/uloc.h" |
| 34 | |
| 35 | namespace v8 { |
| 36 | namespace internal { |
| 37 | |
| 38 | struct LocaleIDMatch { |
| 39 | LocaleIDMatch(); |
| 40 | |
| 41 | LocaleIDMatch& operator=(const LocaleIDMatch& rhs); |
| 42 | |
| 43 | // Bcp47 locale id - "de-Latn-DE-u-co-phonebk". |
| 44 | char bcp47_id[ULOC_FULLNAME_CAPACITY]; |
| 45 | |
| 46 | // ICU locale id - "de_Latn_DE@collation=phonebk". |
| 47 | char icu_id[ULOC_FULLNAME_CAPACITY]; |
| 48 | |
| 49 | // Score for this locale. |
| 50 | int score; |
| 51 | }; |
| 52 | |
| 53 | class LanguageMatcher { |
| 54 | public: |
| 55 | // Default locale. |
| 56 | static const char* const kDefaultLocale; |
| 57 | |
| 58 | // Finds best supported locale for a given a list of locale identifiers. |
| 59 | // It preserves the extension for the locale id. |
| 60 | static void GetBestMatchForPriorityList( |
| 61 | v8::Handle<v8::Array> locale_list, LocaleIDMatch* result); |
| 62 | |
| 63 | // Finds best supported locale for a single locale identifier. |
| 64 | // It preserves the extension for the locale id. |
| 65 | static void GetBestMatchForString( |
| 66 | v8::Handle<v8::String> locale_id, LocaleIDMatch* result); |
| 67 | |
| 68 | private: |
| 69 | // If langauge subtags match add this amount to the score. |
| 70 | static const unsigned int kLanguageWeight; |
| 71 | |
| 72 | // If script subtags match add this amount to the score. |
| 73 | static const unsigned int kScriptWeight; |
| 74 | |
| 75 | // If region subtags match add this amount to the score. |
| 76 | static const unsigned int kRegionWeight; |
| 77 | |
| 78 | // LocaleID match score has to be over this number to accept the match. |
| 79 | static const unsigned int kThreshold; |
| 80 | |
| 81 | // For breaking ties in priority queue. |
| 82 | static const unsigned int kPositionBonus; |
| 83 | |
| 84 | LanguageMatcher(); |
| 85 | |
| 86 | // Compares locale_id to the supported list of locales and returns best |
| 87 | // match. |
| 88 | // Returns false if it fails to convert locale id from ICU to BCP47 format. |
| 89 | static bool CompareToSupportedLocaleIDList(v8::Handle<v8::String> locale_id, |
| 90 | LocaleIDMatch* result); |
| 91 | }; |
| 92 | |
| 93 | } } // namespace v8::internal |
| 94 | |
| 95 | #endif // V8_EXTENSIONS_EXPERIMENTAL_LANGUAGE_MATCHER_H_ |