在Angular中使用TypeScript生成具有动态值的常量标签吗?

huangapple go评论72阅读模式
英文:

Generate constant labels with dynamic values in angular with typescript?

问题

我必须将应用程序中的所有标签转换为标签常量。

某些区域的HTML具有动态内容,例如 This label has '1' dynamic values,其中的 '1' 可根据组件或不同的应用程序状态而更改。

我需要在我的labelConstants中添加2个不同的值,如下所示:

label_1: 'This label has \'',
label_2: '\' dynamic values'

并生成以下字符串:

let string = LabelConst.label_1 + '1' + LabelConst.label_2;

我考虑的一个解决方法是在标签常量中添加一个值,如下所示:

label_1: 'This label has '%s' dynamic values'

然后在生成标签值时,使用一个公共函数来替换标签常量中的 %s 值,例如在我的TypeScript代码中:

let requiredLabel = insertDynamicValues(LabelConst.label_1, '3');

其中函数 insertDynamicValues() 将替换标签常量中的 %s 并返回 This label has '3' dynamic values

是否有更高效/更好的方法来做这件事(不使用外部库)?

英文:

I have to convert all the labels in my application to label constants.

The HTML in some areas have dynamic content like this This label has '1' dynamic values where the '1' is subject to change according to the component or a different application state.

I would have to add 2 different values in my labelConstants like this

label_1: 'This label has \'',

label_2: '\' dynamic values'

and generate the string like this

let string = LabelConst.label_1 + '1' + LabelConst.label_2;

An idea I had for solving this was to add a value in the label constant like this

label_1: 'This label has '%s' dynamic values'

and then while generating the value for the labels use a common function to replace the %s value like this in my typescript code,

let requiredLabel = insertDynamicValues(LabelConst.label_1, '3');

where the function insertDynamicValues() would replace %s in the label constant and return This label has '3' dynamic values.

Is there a more efficient/better way to do this (without external libraries)?

答案1

得分: 1

我有这种方法:

'标签有%0的动态值'

和TypeScript代码:

public static replacePlaceholder(text: string, words: string[], placeholder: string = '%'): string {
  const replaceValue = (result: string, value: string, i: number): string => {
    return result.replace(`${placeholder}${i}`, String(value));
  };

  return words.reduce(replaceValue, text);
}

这样,我可以在一个字符串中使用多个占位符。示例:

`我们有%0个苹果和%1个橙子`
英文:

I have this approach:

'The label has %0 dynamic value'

and TypeScript code:

  public static replacePlaceholder(text: string, words: string[], placeholder: string = '%'): string {
const replaceValue = (result: string, value: string, i: number): string => {
  return result.replace(`${placeholder}${i}`, String(value));
};

return words.reduce(replaceValue, text);

}

That way I can use multiple placeholders in one string. Example:

`We have %0 apples and %1 oranges`

答案2

得分: 0

你可以使用类似于mustache(https://www.npmjs.com/package/mustache)或handlebars(https://www.npmjs.com/package/handlebars)这样的库来实现,尽管我不确定这是否对你的用例来说有点过于复杂。这基本取决于标签应该有多复杂。

英文:

You could use a library like mustache (https://www.npmjs.com/package/mustache) or handlebars (https://www.npmjs.com/package/handlebars) for this, although I am not sure if this is an overkill for your use case. This basically depends on how complex the labels should be.

huangapple
  • 本文由 发表于 2023年7月6日 12:31:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76625510.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定