【发布时间】:2018-03-14 14:15:36
【问题描述】:
我正在用 nodeJS 编写一个应用程序 - 一个 Facebook Messenger 应用程序。
我的应用程序包含一个名为 strings.js 的模块,它基本上用于存储常见的字符串。任何时候我需要打印问候语或从 strings.js 文件中提取的任何内容。 Strings.js 看起来像这样:
/*
* This module exports common strings.
* */
'use strict';
module.exports = {
// Quick replies for user to choose age range
ageReplies:[
{
//Option 1 CODE
},
{
//Option 2 CODE
},
{
//Option 3 CODE
}],
// Quick replies for user to choose gender
genderReplies:[
{
//Option 1 CODE
},
{
//Option 2 CODE
},
{
//Option 3 CODE
}],
// 'greetingButtons' is used to create the buttons sent to a registered
// user during the greeting
greetingButtons: [
{
//BUTTON 1 CODE
},
{
//BUTTON 2 CODE
},
{
//BUTTON 3 CODE
}],
userAgeString: "How old are you?",
userGenderString: "What's your gender?",
userInitialGreetingString: "Hello there! I don't believe we've spoken before. You'll need to create a user profile, so let's do that now.",
userNamePromptString: "What would you like me to call you?",
userPrefNamePromptString: "Ok, what would you like me to call you instead? Type it below:",
userWelcomeGreetingString: "Welcome back! I am ready and willing to serve, as always!",
};
前两个“对象”(如果我们可以这样称呼的话)“ageReplies”和“GenderReplies”用于在 Facebook Messenger 中询问用户性别和年龄时(在创建用户个人资料期间)生成快速回复。
接下来是“greetingButtons”,用于创建按钮,这些按钮会发送给先前注册的用户,并带有“您接下来想做什么?”消息。
最后我有一些字符串用于问候用户,提示他们输入性别和年龄,等等。随着我添加它们,它们会更多。
我一直在考虑将这些单独的字符串放在一个对象中,例如如下:
stringData: {
userAgeString: "How old are you?",
userGenderString: "What's your gender?",
userInitialGreetingString: "Hello there! I don't believe we've spoken before. You'll need to create a user profile, so let's do that now.",
userNamePromptString: "What would you like me to call you?",
userPrefNamePromptString: "Ok, what would you like me to call you instead? Type it below:",
userWelcomeGreetingString: "Welcome back! I am ready and willing to serve, as always!",
},
这样做有什么好处吗?有什么意义吗?
另一个问题是,如果我确实使用了这样的对象,是否可以将我的按钮和快速回复放在同一个对象中,例如如下:
stringData: {
ageReplies:[ /* Age reply code */ ],
genderReplies: [ /* Gender reply code */ ],
greetingButtons: [ /* Button code */ ],
userAgeString: "How old are you?",
userGenderString: "What's your gender?",
userInitialGreetingString: "Hello there! I don't believe we've spoken before. You'll need to create a user profile, so let's do that now.",
userNamePromptString: "What would you like me to call you?",
userPrefNamePromptString: "Ok, what would you like me to call you instead? Type it below:",
userWelcomeGreetingString: "Welcome back! I am ready and willing to serve, as always!",
},
这行得通吗?
编辑:澄清一下,我不是说在我的主代码中有一个对象。我正在谈论的对象将在 strings.js 中
【问题讨论】:
-
从本地化的角度来看,我会说坚持使用 Strings.js,与在代码中创建对象相比,从一个文件更新和维护并引用该文件要容易得多。
-
澄清一下:我的意思是让我的字符串作为 strings.js 中的一个对象,而不是让它们作为 strings.js 中的单独变量