【发布时间】:2016-02-02 17:35:15
【问题描述】:
我通常将 JavaScript 用于 UI/交互目的,并且我正在尝试通过编写更复杂的 JS 来提高我的技能。
我编写了以下代码,试图根据 URL 和国家/地区代码更改我们网站的国家/地区选择器中的内容。
我知道这很糟糕,但我已经花了好几个小时,但我被困住了。如果有人可以提供可以帮助我前进的信息,我将不胜感激。
我的所有变量都未使用,那是因为我认为我可以在 if 语句中使用点表示法来调用它们,但事实并非如此。
这是我到目前为止所写的。尽量不要笑(或哭)。谢谢。
//Initialize Variables
var moduleTitle = document.querySelector(".amt-select-title").innerHTML;
var colTitle = document.querySelector(".amt-choose-country-col > h3").innerHTML;
var languageModTitle = document.querySelector(".amt-choose-language > h2").innerHTML;
var investorRelations = document.querySelector(".amt-utility-3 a").innerHTML;
var company = document.querySelector(".amt-utility-4 a").innerHTML;
var corporateResponsibility = document.querySelector(".amt-utility-5 a").innerHTML;
var careers = document.querySelector(".amt-utility-6 a").innerHTML;
var contactUs = document.querySelector(".amt-utility-7 a").innerHTML;
// This is my library of objects -- one for each language -- EN, ES, PT and DE
var ENcontent = {
moduleTitle : "English Module Title",
colTitle : "English Title",
languageModTitle : "Choose Language EN Title",
investorRelations : "Investor Relations",
company : "Company",
corporateResponsibility : "Corporate Responsbility",
careers : "Careers",
contactUs : "Contact Us"
};
var EScontent = {
moduleTitle : "Spanish Module Title",
colTitle : "Spanish Title",
languageModTitle : "Choose Language ES Title",
investorRelations : "Relaciones con Inversionistas",
company : "Empresa",
corporateResponsibility : "Responsabilidad Corporativa",
careers : "Oportunidades Laborales",
contactUs : "Contáctanos"
};
var PTcontent = {
moduleTitle : "Portuguese Module Title",
colTitle : "Portuguese Title",
languageModTitle : "Choose Language PT Title",
investorRelations : "Investidores",
company : "Empresa",
corporateResponsibility : "Responsabilidade Corporativa",
careers : "Carreiras",
contactUs : "Fale Conosco"
};
var DEcontent = {
moduleTitle : "German Module Title",
colTitle : "German Title",
languageModTitle : "Choose Language DE Title",
investorRelations : "Investor Relations",
company : "Unternehmen",
corporateResponsibility : "Corporate Responsbility",
careers : "Karriere",
contactUs : "Kontakt"
};
//Language Object
function Language(moduleTitle, colTitle, languageModTitle, investorRelations, company, corporateResponsibility, careers, contactUs) {
this.moduleTitle = moduleTitle,
this.colTitle = colTitle,
this.languageModTitle = languageModTitle,
this.investorRelations = investorRelations,
this.company = company,
this.corporateResponsibility = corporateResponsibility,
this.careers = careers,
this.contactUs = contactUs;
}
//Calling Language() function using arguments based on country -- What's a good way to do this?
if(window.location.href.indexOf("/en") > -1) {
Language(
ENcontent.moduleTitle,
ENcontent.colTitle,
ENcontent.languageModTitle,
ENcontent.investorRelations,
ENcontent.company,
ENcontent.corporateResponsibility,
ENcontent.careers,
ENcontent.contactUs);
}else if(window.location.href.indexOf("/es") > -1) {
Language(
EScontent.moduleTitle,
EScontent.colTitle,
EScontent.languageModTitle,
EScontent.investorRelations,
EScontent.company,
EScontent.corporateResponsibility,
EScontent.careers, EScontent.contactUs);
}else if(window.location.href.indexOf("/pt") > -1) {
Language(
PTcontent.moduleTitle,
PTcontent.colTitle,
PTcontent.languageModTitle,
PTcontent.investorRelations,
PTcontent.company,
PTcontent.corporateResponsibility,
PTcontent.careers,
PTcontent.contactUs);
}else if(window.location.href.indexOf("/de") > -1) {
Language(
DEcontent.moduleTitle,
DEcontent.colTitle,
DEcontent.languageModTitle,
DEcontent.investorRelations,
DEcontent.company,
DEcontent.corporateResponsibility,
DEcontent.careers,
DEcontent.contactUs);
}
【问题讨论】:
-
你真的应该使用现有的库来为你做国际化。一个是 fnando/i18n-js,它专注于 Ruby,但它也可以独立工作(参见 "vanilla JS" setup section)。另一个是i18njs.com,这可能会更简单一些。还有很多其他的,没必要在这里重新发明轮子。
-
这个问题可能适合Code Review,只要 (a) 您的代码按预期工作,(b) 您的代码是真实代码,而不是示例代码,并且 (c) 您的代码包含在问题的正文中。如果您希望通过同行评审来改进代码的各个方面,请将其发布在 Code Review 上。
-
@user3472810 当您说您“卡住”时,您是否遇到了具体问题?
标签: javascript jquery javascript-objects