我可以“匹配”吗? 2 个数组中的值,然后使用后续的“匹配”;价值?

发布于 2024-12-13 14:50:21 字数 1638 浏览 4 评论 0原文

我正在尝试实现以下目标,但凭借我的中级 JavaScript 技能,我不确定这是否可行。

这部分与 这个问题

现在我有 2 个数组

a) 有各种语言(例如“en-GB”、“fr”、“de”等)
b)具有基于上述浏览器语言的URL后缀(例如“fr/”,“de/”,“uk/”)

我想要实现的是:

1)用户点击页面,浏览器检测哪个浏览器它正在使用数组 (a)
2) 根据浏览器基于 (a) 的内容,然后搜索 (b),如果它们匹配,例如,如果语言是“fr”,它将使用 (b) 中数组中的后缀“fr/” .
3) 然后它将将此后缀添加到顶级域(始终保持不变)

这是否有可能实现(我确信是)?可以纯粹通过 JavaScript(或 JQuery)来完成吗?我该怎么做呢?

这是我到目前为止的一些代码:

var IAB_Array = new Array("de-at","nl-be","fr-be","da","de","hu","en-ie","ga","es","fr","it","nl","no","pl","en","en-GB","en-US","en-gb","en-us"); //language array
var IAB_suffix = new Array("at/","be-nl/","be-fr","den/","de/","hu/","ie/","es/","fr/","it/","nl/","nor/","pl/","uk/"); //URL suffix Array
var IAB_lang = "en-GB"; //default language
var IAB_link = "http://www.mysitegoeshere/";

if(navigator.browserLanguage) IAB_lang = navigator.browserLanguage; //change lang if detection supported
if(window.navigator.language) IAB_lang = window.navigator.language; //change lang if detection supported

function IAB_Lang_detect () { //execute search
for (var i=0;i<IAB_Array.length;i++) {
    if(IAB_Array[i]==IAB_lang) {
        document.write(IAB_Array[i]); //output matched array value
        }
}
return false;
}

var IAB_URL = "<a href="+IAB_link+IAB_suffix[1]+">"+IAB_link+IAB_suffix[1]+"</a>"; //this is the resulting URL

document.write(IAB_URL);
IAB_Lang_detect ();

我希望有人可以提供帮助,因为我有点困惑!更重要的是匹配两个数组中的值,然后选择我遇到问题的正确后缀。

谢谢

I'm trying to achieve the following though with my intermediate JavaScript skills I'm not sure if this is possible.

This is related in part to this question.

Now I have 2 arrays

a) Has the various language in (e.g. "en-GB", "fr", "de" etc)
b) Has a suffix of a URL based on the browser language above (e.g. "fr/","de/","uk/")

What I am trying to achieve is:

1) User hits a page, browser detects which browser it is using from the array (a)
2) Depending on what the browser is based on (a), it then searches through (b) and if they match, e.g. if the language is "fr" it will use the suffix "fr/" from the array in (b).
3) It will then add this suffix to a top level domain (which is always constant)

Is this even possible to achieve (I'm sure it is)? Can it be done purely via JavaScript (or JQuery)? How would I go about doing this?

Here's some of the code I have so far:

var IAB_Array = new Array("de-at","nl-be","fr-be","da","de","hu","en-ie","ga","es","fr","it","nl","no","pl","en","en-GB","en-US","en-gb","en-us"); //language array
var IAB_suffix = new Array("at/","be-nl/","be-fr","den/","de/","hu/","ie/","es/","fr/","it/","nl/","nor/","pl/","uk/"); //URL suffix Array
var IAB_lang = "en-GB"; //default language
var IAB_link = "http://www.mysitegoeshere/";

if(navigator.browserLanguage) IAB_lang = navigator.browserLanguage; //change lang if detection supported
if(window.navigator.language) IAB_lang = window.navigator.language; //change lang if detection supported

function IAB_Lang_detect () { //execute search
for (var i=0;i<IAB_Array.length;i++) {
    if(IAB_Array[i]==IAB_lang) {
        document.write(IAB_Array[i]); //output matched array value
        }
}
return false;
}

var IAB_URL = "<a href="+IAB_link+IAB_suffix[1]+">"+IAB_link+IAB_suffix[1]+"</a>"; //this is the resulting URL

document.write(IAB_URL);
IAB_Lang_detect ();

I hope someone can help as I'm a little confused! It's more so the matching the values from the 2 arrays and then subsequently selecting the correct suffix that I'm having trouble with.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不打扰别人 2024-12-20 14:50:22

我会以不同的方式做并使用一个对象:

var IAB_Object = { "it-It": "it/", "en-Gb": "en/" ....}

if(IAB_Object.hasOwnProperty(IAB_lang)){
   //you have a match, the suffix is
   var suffix = IAB_Object[IAB_lang];
}else{
   //you don't have a match use a standard language
}     

I'd do it differently and use an object:

var IAB_Object = { "it-It": "it/", "en-Gb": "en/" ....}

if(IAB_Object.hasOwnProperty(IAB_lang)){
   //you have a match, the suffix is
   var suffix = IAB_Object[IAB_lang];
}else{
   //you don't have a match use a standard language
}     
余生再见 2024-12-20 14:50:22

我可能根本不会为此使用数组。您可以使用一个对象:

var IABInfo = {
    "de-at": "at/",
    "ln-be": "be-nl/",
    // ...and so on
};

然后直接索引到该对象:

var value = IABInfo[IABLang]; // Where IABLang contains a string, like "de-at"

所以:

var suffix = IABInfo[IABLang];
if (suffix) { // Did we have it?
    document.write(suffix);
}

这是可行的,因为所有 JavaScript 对象都是自由格式的键/值映射。下面是一个更简单的示例:

var lifeTheUniverseAndEverything = {
    answer: 42,
    question: "?"
};

您可以使用带有文字的点符号或带有字符串的方括号 ([]) 符号来查找属性。因此,所有这四个输出完全相同的内容:

// 1. Dotted notation with a literal:
console.log("The answer is " + lifeTheUniverseAndEverything.answer);

// 2. Bracketed notation with a string
console.log("The answer is " + lifeTheUniverseAndEverything["answer"]);

// 3. The string needn't be a literal, it can come from a variable...
var name = "answer";
console.log("The answer is " + lifeTheUniverseAndEverything[name]);

// 4. ...or indeed any expression:
console.log("The answer is " + lifeTheUniverseAndEverything["a" + "n" + "swer"]);

因此,通过将 IAB 信息制作为对象文字中的映射,您可以更轻松地查找内容:只需使用带有所需语言代码的括号表示法即可。

I probably wouldn't use arrays for this at all. You can use an object:

var IABInfo = {
    "de-at": "at/",
    "ln-be": "be-nl/",
    // ...and so on
};

Then index directly into that object:

var value = IABInfo[IABLang]; // Where IABLang contains a string, like "de-at"

So:

var suffix = IABInfo[IABLang];
if (suffix) { // Did we have it?
    document.write(suffix);
}

This works because all JavaScript objects are free-form key/value maps. Here's a simpler example:

var lifeTheUniverseAndEverything = {
    answer: 42,
    question: "?"
};

You can look up a property either using dotted notation with a literal, or by using square bracket ([]) notation with a string. So all four of these output exactly the same thing:

// 1. Dotted notation with a literal:
console.log("The answer is " + lifeTheUniverseAndEverything.answer);

// 2. Bracketed notation with a string
console.log("The answer is " + lifeTheUniverseAndEverything["answer"]);

// 3. The string needn't be a literal, it can come from a variable...
var name = "answer";
console.log("The answer is " + lifeTheUniverseAndEverything[name]);

// 4. ...or indeed any expression:
console.log("The answer is " + lifeTheUniverseAndEverything["a" + "n" + "swer"]);

So by making your IAB info a map in an object literal, you can make it much easier to look things up: Just use bracketed notation with the desired language code.

花伊自在美 2024-12-20 14:50:21
(function () {
    "use strict";
    var lang_map = {
        "de-at": "at/",
        "nl-be": "be-nl/",
        "fr-be": "be-fr",
        "da": "den/",
        "de": "de/",
        "hu": "hu/",
        "en-ie": "ie/",
        "ga": "ie/",
        "es": "es/",
        "fr": "fr/",
        "it": "it/",
        "nl": "nl/",
        "no": "nor/",
        "pl": "pl/",
        "en": "uk/",
        "en-GB": "uk/",
        "en-US": "uk/",
        "en-gb": "uk/",
        "en-us": "uk/"
    },
    lang = (navigator && navigator.browserLanguage) || (window.navigator && window.navigator.language) || "en-GB";
    window.location = "http://www.mysitegoeshere/" + lang_map[lang];
}());
(function () {
    "use strict";
    var lang_map = {
        "de-at": "at/",
        "nl-be": "be-nl/",
        "fr-be": "be-fr",
        "da": "den/",
        "de": "de/",
        "hu": "hu/",
        "en-ie": "ie/",
        "ga": "ie/",
        "es": "es/",
        "fr": "fr/",
        "it": "it/",
        "nl": "nl/",
        "no": "nor/",
        "pl": "pl/",
        "en": "uk/",
        "en-GB": "uk/",
        "en-US": "uk/",
        "en-gb": "uk/",
        "en-us": "uk/"
    },
    lang = (navigator && navigator.browserLanguage) || (window.navigator && window.navigator.language) || "en-GB";
    window.location = "http://www.mysitegoeshere/" + lang_map[lang];
}());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文