【问题标题】:Internationalisation with asp.net, c# and VSCode使用 asp.net、c# 和 VSCode 进行国际化
【发布时间】:2015-10-11 22:36:16
【问题描述】:

我想使用 VS Code 在我的 asp.net 项目中创建一个国际化文件。我刚刚找到了如何使用常见的VS版本,只需创建一个.resx文件,并使用特定的接口插入变量和值。

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

【问题讨论】:

    标签: c# asp.net visual-studio-code


    【解决方案1】:

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

    资源文件是 .resx,这是一个使用 Visual Studio Community 制作的入门文件,您可以使用:

    <?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>
    

    【讨论】:

    • 谢谢!这正是我想要的!
    【解决方案2】:

    Resx 文件只会在 VS Code 中部分发挥作用。

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

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

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

    作为 .resx 的替代方案,我一直致力于创建 .resj 文件格式。此功能完全在 MSBuild 中运行,因此在 VSCode 中应该与在 Visual Studio 中一样好。该项目是open sourced here。我已将其打包为 nuget 包 "Elemental.JsonResource"(目前仅预发布)。如果您将该包添加到您的项目中,您应该能够使用 .resj 文件。目前只支持字符串资源,希望以后能增加对其他资源类型的支持。它不会为您的项目添加运行时依赖项,它仅在构建时运行。

    【讨论】:

      猜你喜欢
      • 2023-03-13
      • 1970-01-01
      • 1970-01-01
      • 2018-02-05
      • 2019-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多