C# Discord Bot json 本地化

问题描述 投票:0回答:1

我想要一个关于 Discord 机器人 json 本地化的示例。对于 C#,有一种方法可以使用 .resx 文件来实现,但 json 对我来说更适合(可选)。我没有找到任何模板...

我还想要一件事,命令名称、描述等的全球本地化。但我也想将这个本地化用于机器人消息。

有一个 json 本地化模式,但我不知道如何使用它..

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "title": "Discord.NET Json Localization Schema",
  "description": "Json schema to be used with the Default Json Localization Manager of D.NET",
  "type": [
    "object",
    "string"
  ],
  "$defs": {
    "slash": {
      "type": "object",
      "properties": {
        "name": {
          "description": "Localized name of the Slash Command Group/Slash Command/Slash Command Parameter",
          "type": "string",
          "pattern": "^[\\w-]{1,32}$"
        },
        "description": {
          "description": "Localized description of the Slash Command Group/Slash Command/Slash Command Parameter",
          "type": "string",
          "minLength": 1,
          "maxLength": 100
        }
      },
      "additionalProperties": {
        "anyOf": [
          {
            "$ref": "#/$defs/slash"
          },
          {
            "$ref": "#/$defs/choice"
          }
        ]
      },
      "required": [
        "name",
        "description"
      ]
    },
    "context": {
      "type": "object",
      "properties": {
        "name": {
          "description": "Localized name of the Context Command",
          "type": "string",
          "minLength": 1,
          "maxLength": 32
        }
      },
      "required": [
        "name"
      ],
      "additionalProperties": false
    },
    "choice": {
      "type": "object",
      "properties": {
        "name": {
          "description": "Localized name of the Slash Command Parameter Choice",
          "type": "string",
          "minLength": 1,
          "maxLength": 32
        }
      },
      "required": [
        "name"
      ],
      "additionalProperties": false
    }
  },
  "additionalProperties": {
    "anyOf": [
      {
        "$ref": "#/$defs/slash"
      },
      {
        "$ref": "#/$defs/context"
      }
    ]
  }
}

有一个 .resx 使用示例; CommandLocales.resx file image

program.cs配置

private static readonly InteractionServiceConfig _interactionServiceConfig = new()
{
    LocalizationManager = new ResxLocalizationManager("Pleasure.Resources.CommandLocales", Assembly.GetEntryAssembly(),
    new CultureInfo("en-US"), new CultureInfo("tr"))
};
c# discord localization bots
1个回答
0
投票

我对消息进行了 json 本地化。

{
  "en-US": {
    "welcome": "Welcome to the server!"
  },
  "tr": {
    "welcome": "Sunucuya hoş geldiniz!"
  }
}

命令代码:

var json = File.ReadAllText("locales.json");

var stringReader = new StringReader(json);
var jsonReader = new JsonTextReader(stringReader);

JsonSerializer serialize = new JsonSerializer();
var localization = serialize.Deserialize<Dictionary<string, Dictionary<string, string>>>(jsonReader);

var serverLocale = Context.Guild.PreferredLocale;

localization[serverLocale]["welcome"]

但我仍在尝试命令名称、描述等...

© www.soinside.com 2019 - 2024. All rights reserved.