使用asp.net、c#和VSCode实现国际化

使用asp.net、c#和VSCode实现国际化,c#,asp.net,visual-studio-code,C#,Asp.net,Visual Studio Code,我想在我的asp.net项目中使用VS代码创建一个国际化文件。我刚刚发现了如何使用一个通用的VS版本来实现它,只需创建一个.resx文件,并使用一个特定的接口来插入变量和值 如何使用Visual Studio代码创建国际化文件?Visual Studio代码不是IDE。这是一个代码编辑器。因此,您不能像使用完整的VisualStudio菜单那样添加新的项目类型。相反,您可以添加文本文件,为它们指定适当的扩展名,并相应地填写它们 资源文件是.resx,下面是一个使用Visual Studio社区制

我想在我的asp.net项目中使用VS代码创建一个国际化文件。我刚刚发现了如何使用一个通用的VS版本来实现它,只需创建一个.resx文件,并使用一个特定的接口来插入变量和值


如何使用Visual Studio代码创建国际化文件?

Visual Studio代码不是IDE。这是一个代码编辑器。因此,您不能像使用完整的VisualStudio菜单那样添加新的项目类型。相反,您可以添加文本文件,为它们指定适当的扩展名,并相应地填写它们

资源文件是.resx,下面是一个使用Visual Studio社区制作的入门文件,您可以使用:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <!-- 
    Microsoft ResX Schema 

    Version 2.0

    The primary goals of this format is to allow a simple XML format 
    that is mostly human readable. The generation and parsing of the 
    various data types are done through the TypeConverter classes 
    associated with the data types.

    Example:

    ... ado.net/XML headers & schema ...
    <resheader name="resmimetype">text/microsoft-resx</resheader>
    <resheader name="version">2.0</resheader>
    <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
    <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
    <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
    <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data>
    <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
        <value>[base64 mime encoded serialized .NET Framework object]</value>
    </data>
    <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
        <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
        <comment>This is a comment</comment>
    </data>

    There are any number of "resheader" rows that contain simple 
    name/value pairs.

    Each data row contains a name, and value. The row also contains a 
    type or mimetype. Type corresponds to a .NET class that support 
    text/value conversion through the TypeConverter architecture. 
    Classes that don't support this are serialized and stored with the 
    mimetype set.

    The mimetype is used for serialized objects, and tells the 
    ResXResourceReader how to depersist the object. This is currently not 
    extensible. For a given mimetype the value must be set accordingly:

    Note - application/x-microsoft.net.object.binary.base64 is the format 
    that the ResXResourceWriter will generate, however the reader can 
    read any of the formats listed below.

    mimetype: application/x-microsoft.net.object.binary.base64
    value   : The object must be serialized with 
            : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.soap.base64
    value   : The object must be serialized with 
            : System.Runtime.Serialization.Formatters.Soap.SoapFormatter
            : and then encoded with base64 encoding.

    mimetype: application/x-microsoft.net.object.bytearray.base64
    value   : The object must be serialized into a byte array 
            : using a System.ComponentModel.TypeConverter
            : and then encoded with base64 encoding.
    -->
  <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    <xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
    <xsd:element name="root" msdata:IsDataSet="true">
      <xsd:complexType>
        <xsd:choice maxOccurs="unbounded">
          <xsd:element name="metadata">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" />
              </xsd:sequence>
              <xsd:attribute name="name" use="required" type="xsd:string" />
              <xsd:attribute name="type" type="xsd:string" />
              <xsd:attribute name="mimetype" type="xsd:string" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="assembly">
            <xsd:complexType>
              <xsd:attribute name="alias" type="xsd:string" />
              <xsd:attribute name="name" type="xsd:string" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="data">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
                <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
              <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
              <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
              <xsd:attribute ref="xml:space" />
            </xsd:complexType>
          </xsd:element>
          <xsd:element name="resheader">
            <xsd:complexType>
              <xsd:sequence>
                <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
              </xsd:sequence>
              <xsd:attribute name="name" type="xsd:string" use="required" />
            </xsd:complexType>
          </xsd:element>
        </xsd:choice>
      </xsd:complexType>
    </xsd:element>
  </xsd:schema>
  <resheader name="resmimetype">
    <value>text/microsoft-resx</value>
  </resheader>
  <resheader name="version">
    <value>2.0</value>
  </resheader>
  <resheader name="reader">
    <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <resheader name="writer">
    <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
  </resheader>
  <data name="MyString" xml:space="preserve">
    <value>My Value</value>
    <comment>My Comment</comment>
  </data>
</root>

text/microsoft resx
2
System.Resources.ResXResourceReader,System.Windows.Forms,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089
System.Resources.ResXResourceWriter,System.Windows.Forms,版本=4.0.0.0,区域性=neutral,PublicKeyToken=b77a5c561934e089
我的价值
我的评论

Resx文件在VS代码中只起部分作用

通常,resx文件会生成两个工件:程序集中的嵌入式资源和一个代码文件,其中包含按名称访问资源的api

嵌入的资源是从MSBuild任务创建的,因此只要您使用MSBuild进行构建,就会对其进行处理,这对于任何.NET项目来说都是非常标准的

但是,生成的代码文件是由VisualStudio中的“自定义工具”创建的,因此如果使用VSCode,您将无法免费获得该文件。这个生成的代码文件可以手工编写,但与.resx保持同步并没有多大乐趣


作为.resx的替代方案,我一直在努力创建一个.resj文件格式。这完全在MSBuild中起作用,因此它在VSCode中的作用应该与在VisualStudio中的作用一样好。该项目正在进行中。我已经把它打包成一个nuget包(目前只是预发行版)。如果将该包添加到项目中,则应该能够使用.resj文件。目前,它只支持字符串资源,但我希望在将来添加对其他资源类型的支持。它不会将运行时依赖项添加到项目中,它只在构建时运行

谢谢大家!!这正是我要找的!!