WebStorm 2021.2 Help

Encoding编码

To display and edit files correctly, WebStorm needs to know which encoding to use. 为了正确显示和编辑文件,WebStorm需要知道使用哪种编码。In general, source code files are mostly in UTF-8. 一般来说,源代码文件大多采用UTF-8格式。This is the recommended encoding unless you have some other requirements.除非您有其他要求,否则这是推荐的编码。

To determine the encoding of a file, WebStorm uses the following steps:为了确定文件的编码,WebStorm使用以下步骤:

  • If the byte order mark (BOM) is present, WebStorm will use the corresponding Unicode encoding regardless of all other settings. 如果存在字节顺序标记(BOM),WebStorm将使用相应的Unicode编码,而不考虑所有其他设置。For more information, see Byte order mark.有关更多信息,请参阅字节顺序标记

  • If the file declares the encoding explicitly, WebStorm will use the specified encoding. 如果文件显式声明编码,WebStorm将使用指定的编码。For example, this can apply to XML or HTML files. 例如,这可以应用于XML或HTML文件。The explicit declaration also overrides all other settings, but you can change it in the editor.显式声明也会覆盖所有其他设置,但您可以在编辑器中对其进行更改。

  • If there is no BOM and no explicit encoding declaration in the file, WebStorm will use the encoding configured for the file or directory in the file encoding settings. 如果文件中没有BOM表和明确的编码声明,WebStorm将使用文件编码设置中为文件或目录配置的编码。If encoding is not configured for the file or directory, WebStorm will use the encoding of the parent directory. 如果没有为文件或目录配置编码,WebStorm将使用父目录的编码。If the parent directory encoding is also not configured, WebStorm will fall back to the Project Encoding, and if there is no project, to Global Encoding.如果未配置父目录编码,WebStorm将退回到“项目编码”,如果没有项目编码,则退回到“全局编码”。

Change encoding of a file that contains explicit encoding更改包含显式编码的文件的编码

  1. Open the desired file in the editor.在编辑器中打开所需的文件。

  2. Change explicit encoding information. Use error highlighting to recognize wrong encoding and press Ctrl+Space to have a list of available encodings displayed:更改显式编码信息。使用错误突出显示可识别错误编码,并按Ctrl+Space可显示可用编码的列表:

    Explicit encoding

Change the encoding used to view a file更改用于查看文件的编码

If WebStorm displays characters in a file incorrectly, it probably couldn't detect the file encoding. 如果WebStorm错误地显示文件中的字符,它可能无法检测到文件编码。In this case, you need to specify the correct encoding to use for viewing and editing this file.在这种情况下,您需要指定用于查看和编辑此文件的正确编码。

  • With the file open in the editor, either select File | File Properties | File Encoding from the main menu or click the File Encoding widget on the status bar, and select the correct encoding of the file.在编辑器中打开文件后,从主菜单中选择“文件”|“文件属性”|“文件编码”,或单击状态栏上的“文件编码”小部件,然后选择文件的正确编码。

    Changing encoding on the Status bar

    Encodings marked with The triangle warning icon or The round error icon might change the file contents. 标记为The triangle warning iconThe round error icon可能更改文件内容的编码。In this case, WebStorm opens a dialog where you can choose what to do with the file:在这种情况下,WebStorm会打开一个对话框,您可以在其中选择如何处理文件:

    • Reload: load the file in the editor from disk and apply encoding changes to the editor only. 重载:从磁盘加载编辑器中的文件,并仅对编辑器应用编码更改。You will see the content changes related to the chosen encoding, but the actual file will not change.您将看到与所选编码相关的内容更改,但实际文件不会更改。

    • Convert: overwrite the file with the chosen encoding.转换:使用所选编码覆盖文件。

This will add an association for the file to the file encoding settings. 这会将文件的关联添加到文件编码设置中。WebStorm will use the specified encoding to view and edit this file.WebStorm将使用指定的编码来查看和编辑此文件。

Configure default encoding for properties files为属性文件配置默认编码

WebStorm applies system default encoding to .properties files. WebStorm将系统默认编码应用于.properties文件。To use other encodings, feed them as escape sequences and Unicode. 要使用其他编码,请将其作为转义序列和Unicode提供。Alternatively, define the default encoding for .properties files on the project level and use different API that can read .properties files in the encoding you have defined.或者,在项目级别定义.properties文件的默认编码,并使用不同的API来读取已定义编码中的.properties文件。

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Editor | File Encodings.在“设置/首选项”对话框中,按住Ctrl+Alt+S键,选择“编辑器”|“文件编码”。

  2. Do one of the following:执行以下操作之一:

    • To have the special mode turned on when symbols are stored in a properties file as escape sequences but displayed as normal letters, check the option Transparent native-to-ascii conversion. 当符号作为转义序列存储在属性文件中但显示为普通字母时,要启用特殊模式,请选中“透明本机到ascii转换”选项。This option is helpful when the properties files are encoded in ISO 8859-1. 当属性文件以ISO 8859-1编码时,此选项非常有用。It is recommended to use this approach, if you don't have any special reasons to change encoding.如果您没有任何特殊原因更改编码,建议使用这种方法。

    • In the Default encoding for properties files list, select the encoding that will be used for all properties files in project.在“特性文件的默认编码”列表中,选择将用于项目中所有特性文件的编码。

Configure file encoding settings配置文件编码设置

In the Settings/Preferences dialog Ctrl+Alt+S, select Editor | File Encodings.在“设置/首选项”对话框Ctrl+Alt+S中,选择“编辑器”|“文件编码”。

WebStorm uses these settings to view and edit files for which it was not able to detect the encoding and also uses the specified encodings for new files.WebStorm使用这些设置来查看和编辑无法检测编码的文件,并且还将指定的编码用于新文件。

Global Encoding全局编码

Select the encoding to use when other encoding options don't apply.选择其他编码选项不适用时要使用的编码。

For example, WebStorm will use this encoding for files that are not part of any project or when you check out sources from a version control system.例如,WebStorm将对不属于任何项目的文件或从版本控制系统签出源文件时使用此编码。

Project Encoding项目编码Select the encoding to use for files that are not listed in the table.选择用于表中未列出的文件的编码。
Path路径Specify the path to the files or directories for which you want to configure the encoding.指定要为其配置编码的文件或目录的路径。
Encoding编码

Select the encoding to use for the specified files and directories.选择用于指定文件和目录的编码。

If this selector is disabled, the file probably has a BOM or declares the encoding explicitly. 如果禁用此选择器,则文件可能具有BOM表或显式声明编码。In this case, you can't configure the encoding to use for this file.在这种情况下,您无法配置用于此文件的编码。

The encoding selected for a directory applies to all files and subdirectories within it.为目录选择的编码适用于其中的所有文件和子目录。

Default encoding for properties files属性文件的默认编码

Select the encoding for properties files in your project.选择项目中“属性文件”的编码。

Transparent native-to-ascii conversion透明本机到ascii转换

Show national characters (those not defined in ISO 8859-1) in place of the corresponding escape sequences.显示国家字符(ISO 8859-1中未定义的字符)以代替相应的转义序列。

Create UTF-8 files创建UTF-8文件

Select how WebStorm should create UTF-8 files:选择WebStorm应如何创建UTF-8文件:

  • with BOM带物料清单

  • without BOM无物料清单

  • with BOM on Window and without BOM otherwise在窗口上显示BOM表,否则不显示BOM表

By default, WebStorm creates UTF-8 files without the BOM because some software is not compatible with the BOM, and it may be a problem when interpreting scripts. 默认情况下,WebStorm创建不带BOM的UTF-8文件,因为某些软件与BOM不兼容,在解释脚本时可能会出现问题。However, in some cases, you may want to have the BOM in your UTF-8 files.但是,在某些情况下,您可能希望在UTF-8文件中包含BOM表。

Select console output encoding选择控制台输出编码

By default, WebStorm uses the system encoding to view console output.默认情况下,WebStorm使用系统编码来查看控制台输出。

  1. In the Settings/Preferences dialog Ctrl+Alt+S, select Editor | General | Console.在“设置/首选项”对话框Ctrl+Alt+S中,选择“编辑器”|“常规”|“控制台”。

  2. Select the default encoding from the Default Encoding list.从“默认编码”列表中选择默认编码。

  3. Click OK to apply the changes.单击“确定”应用更改。

Last modified: 15 June 2021