Generate Yii translation message files

前端 未结 3 1737
温柔的废话
温柔的废话 2021-01-31 21:37

I am interested to know is there a script or otherway available to collect and generate Yii translation messages in a controller/project

Eg. If I have the following code

3条回答
  •  失恋的感觉
    2021-01-31 21:58

    That sounds like a job for grep and a regular expression. Search for this:

    Yii::t\s*\(\s*('(?:[^']|(?<=\\)')*'|"(?:[^"]|(?<=\\)")*")\s*,\s*('(?:[^']|(?<=\\)')*'|"(?:[^"]|(?<=\\)")*")\s*\)
    

    Since the above is unfortunately unreadable, I 'll break it down a bit:

    Yii::t\s*\(\s*##PATTERN##\s*,\s*##PATTERN##\s*\)
    

    This obviously matches a call to Yii::t taking care to account for whitespace. The secret sauce is ##PATTERN##, which is repeated twice. ##PATTERN## is

    ('(?:[^']|(?<=\\)')*'|"(?:[^"]|(?<=\\)")*")
    

    The above matches either '([^']|(?<=\\)')*' (a singly quoted string) or "([^"]|(?<=\\)")*" (a doubly quoted string). Non-capturing groups (?:) have been used to disregard interim results that are of no interest.

    After matching with this regex, capturing group #1 will hold the translation file name (e.g. 'blog') and group #2 will hold the string name (e.g. 'Your name').

提交回复
热议问题