search

Home  >  Q&A  >  body text

How about Symfony2 Chinese support?

Originally I wanted to use ZendFramework2, but ZF2’s support for Chinese is too poor. Chinese characters often appear garbled. I would like to know if Symfony2 often causes garbled Chinese characters?

黄舟黄舟2738 days ago424

reply all(1)I'll reply

  • 阿神

    阿神2017-05-16 16:48:19

    The Chinese support of Symfony 2 is very good. There is actually nothing special about it. If you can follow best practices, there will be no problems.

    The first is PHP’s charset, SF2 uses UTF-8 by default:

    • The default configuration of the 2.0 branch (/app/config/config.yml) is UTF-8.
    • The 2.1 branch has simply removed this configuration item. If you do not plan to use UTF-8, you need to overload the getCharset() method in AppKernel to specify it.

    Then there are the character sets of various data sources, such as relational databases, NoSQL, external data files, etc.:

    • If you use third-party libraries such as Doctrine or Propel, there are corresponding parameters. Doctrine defaults to UTF-8.
    • If you do your own data interface encapsulation, take MySQL as an example. When building a table, you need to confirm that the database character set is UTF-8. You also need to specify the character set when creating a connection. External files such as csv, etc. need to be converted by yourself before operating; newer NoSQL generally uses UTF-8 by default, so don't worry about it.

    View layer:

    • If you write all language items in the template, you need to ensure that the encoding of the template file (can also be regarded as an external data file) is correct.
    • If you output HTML or other markup language content, you also need to ensure that the correct character set (such as HTML's charset) is also specified semantically.
    • If you use the Translations module that comes with SF2 (you can use yaml, xml or gettext), you need to specify the correct locale.

    To summarize, make sure that all code files and running parameters are in the same encoding (UTF-8).

    As for ensuring that the code files have the correct character set, VCS hooks can be used to handle it well. For example, when submitting the file, check the character set of the file (pre-commit). If it is incorrect, reject the submission.

    reply
    0
  • Cancelreply