├── MIT-LICENCE ├── README.md └── jquery.browserLanguage.js /MIT-LICENCE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Dan Singerman 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in 13 | all copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 21 | THE SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | **Note**: this is a hack only to be used if you do not have a back end available that can do this for you. In general you should not be making calls to third party hosted javascript files in your pages unless you have a very high level of trust in the host. Third party script calls could in theory run any arbitrary javascript on your site. 2 | 3 | This is a jQuery plugin to obtain the browser language setting 4 | 5 | The browser language can not (generally) be obtained via navigator.language (which is silly but there you go). 6 | 7 | The browser language is available in "Accept-language" HTTP header, but this is not directly accessible via javascript. 8 | 9 | The plugin utilises a service at http://ajaxhttpheaders.appspot.com which echoes back the HTTP request headers in JSONP. 10 | 11 | I made that as well, and intend to leave it running there in perpetuity (or as long as Google continue to host App Engine for free) 12 | 13 | The Accept-Language header as defined by http://www.w3.org/Protocols/rfc2616/rfc2616-sec3.html#sec3.10 uses an ISO 639-1 language code. The plugin converts this to an English Language description of the Language (e.g. 'Norwegian'), but also returns the complete value of the header if you need to do further processing. 14 | 15 | Example usage: 16 | 17 | ``` 18 | 23 | ``` 24 | 25 | ## Update 30th April 2015 26 | 27 | If you want to run your own version of the App Engine app, you can find it here: https://github.com/dansingerman/app-engine-headers 28 | -------------------------------------------------------------------------------- /jquery.browserLanguage.js: -------------------------------------------------------------------------------- 1 | /*! 2 | * jQuery-Browser-Language jQuery plugin v0.0.1 3 | * 4 | * Copyright 2010 by Dan Singerman 5 | * 6 | * This program is free software; you can redistribute it and/or modify it 7 | * under the terms of the GNU General Public License as published by the Free 8 | * Software Foundation; either version 2 of the License, or (at your option) 9 | * any later version. 10 | * 11 | * This program is distributed in the hope that it will be useful, but WITHOUT 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for 14 | * more details. 15 | * 16 | * You should have received a copy of the GNU General Public License along with 17 | * this program; if not, write to the Free Software Foundation, Inc., 51 18 | * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 19 | */ 20 | 21 | (function($){ 22 | 23 | $.browserLanguage = function(callback){ 24 | var language; 25 | $.ajax({ 26 | url: "//ajaxhttpheaders.appspot.com", 27 | dataType: 'jsonp', 28 | success: function(headers) { 29 | language = headers['Accept-Language'].substring(0,2); 30 | callback(languageLookup[language], headers['Accept-Language']); 31 | } 32 | }); 33 | } 34 | 35 | /* 36 | Language list from http://en.wikipedia.org/wiki/ISO_639-1_language_matrix 37 | */ 38 | 39 | var languageLookup = { 40 | "ab": "Abkhazian", 41 | "af": "Afrikaans", 42 | "an": "Aragonese", 43 | "ar": "Arabic", 44 | "as": "Assamese", 45 | "az": "Azerbaijani", 46 | "be": "Belarusian", 47 | "bg": "Bulgarian", 48 | "bn": "Bengali", 49 | "bo": "Tibetan", 50 | "br": "Breton", 51 | "bs": "Bosnian", 52 | "ca": "Catalan / Valencian", 53 | "ce": "Chechen", 54 | "co": "Corsican", 55 | "cs": "Czech", 56 | "cu": "Church Slavic", 57 | "cy": "Welsh", 58 | "da": "Danish", 59 | "de": "German", 60 | "el": "Greek", 61 | "en": "English", 62 | "eo": "Esperanto", 63 | "es": "Spanish / Castilian", 64 | "et": "Estonian", 65 | "eu": "Basque", 66 | "fa": "Persian", 67 | "fi": "Finnish", 68 | "fj": "Fijian", 69 | "fo": "Faroese", 70 | "fr": "French", 71 | "fy": "Western Frisian", 72 | "ga": "Irish", 73 | "gd": "Gaelic / Scottish Gaelic", 74 | "gl": "Galician", 75 | "gv": "Manx", 76 | "he": "Hebrew", 77 | "hi": "Hindi", 78 | "hr": "Croatian", 79 | "ht": "Haitian; Haitian Creole", 80 | "hu": "Hungarian", 81 | "hy": "Armenian", 82 | "id": "Indonesian", 83 | "is": "Icelandic", 84 | "it": "Italian", 85 | "ja": "Japanese", 86 | "jv": "Javanese", 87 | "ka": "Georgian", 88 | "kg": "Kongo", 89 | "ko": "Korean", 90 | "ku": "Kurdish", 91 | "kw": "Cornish", 92 | "ky": "Kirghiz", 93 | "la": "Latin", 94 | "lb": "Luxembourgish Letzeburgesch", 95 | "li": "Limburgan Limburger Limburgish", 96 | "ln": "Lingala", 97 | "lt": "Lithuanian", 98 | "lv": "Latvian", 99 | "mg": "Malagasy", 100 | "mk": "Macedonian", 101 | "mn": "Mongolian", 102 | "mo": "Moldavian", 103 | "ms": "Malay", 104 | "mt": "Maltese", 105 | "my": "Burmese", 106 | "nb": "Norwegian (Bokmål)", 107 | "ne": "Nepali", 108 | "nl": "Dutch", 109 | "nn": "Norwegian (Nynorsk)", 110 | "no": "Norwegian", 111 | "oc": "Occitan (post 1500); Provençal", 112 | "pl": "Polish", 113 | "pt": "Portuguese", 114 | "rm": "Raeto-Romance", 115 | "ro": "Romanian", 116 | "ru": "Russian", 117 | "sc": "Sardinian", 118 | "se": "Northern Sami", 119 | "sk": "Slovak", 120 | "sl": "Slovenian", 121 | "so": "Somali", 122 | "sq": "Albanian", 123 | "sr": "Serbian", 124 | "sv": "Swedish", 125 | "sw": "Swahili", 126 | "tk": "Turkmen", 127 | "tr": "Turkish", 128 | "ty": "Tahitian", 129 | "uk": "Ukrainian", 130 | "ur": "Urdu", 131 | "uz": "Uzbek", 132 | "vi": "Vietnamese", 133 | "vo": "Volapuk", 134 | "yi": "Yiddish", 135 | "zh": "Chinese" 136 | } 137 | 138 | })(jQuery); 139 | --------------------------------------------------------------------------------