Regular expression for a language tag (as defined by BCP47)

前端 未结 4 696
情书的邮戳
情书的邮戳 2021-02-05 18:52

I need a regular expression for a language tag as defined by BCP 47.

I know that the full BNF syntax is available at http://www.rfc-editor.org/rfc/bcp/bcp47.txt and th

4条回答
  •  孤独总比滥情好
    2021-02-05 19:18

    If using a CLDR-based function set, like PHP's intl extension, you can check if a locale exists in the intl database using a function like:

    
    

    It takes about half a millisecond to load and search the data, so it won't be too much of a performance hit.

    Of course, it will only find those in the database of the CLDR version supplied with the PHP version used, but will be updated with each subsequent PHP release.

提交回复
热议问题