wikimedia / banana-i18n

banana-i18n - Javascript Internationalization library
https://wikimedia.github.io/banana-i18n/
MIT License
80 stars 27 forks source link

Crash with "zh-classical" messages #38

Open nyurik opened 4 years ago

nyurik commented 4 years ago

This code crashes because it calls new Intl.PluralRules("zh-classical"), which is an error in Chrome.

try {
  const bb = new Banana('zh-classical', { messages: {} });
  console.error(bb.i18n('zh-classical -- {{PLURAL:$1|$1 aaa|$1 bbb}}', 10))
} catch (err) {
  console.error(err)
}

Error:

index.js:1 RangeError: Incorrect locale information provided
    at new PluralRules (<anonymous>)
    at r.getPluralForm (language.js:51)
    at r.convertPlural (language.js:36)
    at c.plural (emitter.js:134)
    at c.emit (emitter.js:55)
    at emitter.js:54
    at Array.map (<anonymous>)
    at c.emit (emitter.js:54)
    at i.parse (parser.js:13)
    at o.i18n (index.js:28)
Nikerabbit commented 4 years ago

I fixed a similar issue in https://phabricator.wikimedia.org/T228510

Nikerabbit commented 4 years ago

The standard language code for zh-classical is lzh.

nyurik commented 4 years ago

@Nikerabbit thanks, but the banana lib has an entry for it in https://github.com/wikimedia/banana-i18n/blob/6fc921139f42d39092a09964de55dc116232003e/src/languages/fallbacks.json#L156 -- shouldn't it automatically handle such things internally? If banana lib is targeting Wikipedia, it should IMO handle every "language-like" prefix Wikipedia has -- i.e. *.wikipedia.org

Nikerabbit commented 4 years ago

I recommend against using the domain code as language code. There are many exceptions where they do not match. The real language code (queryable via API, mw.config.values.wgContentLanguage, etc.) is better.

nyurik commented 4 years ago

@Nikerabbit you assume the code runs in MW, whereas I use it outside of it -- https://dibabel.toolforge.org/ -- and I would have to resolve the domain into the default language. That said, could you explain why there is a fallback file? If its there, I would assume it is supported. Otherwise its a bug to have stuff in it that doesn't work :)

nyurik commented 4 years ago

P.S. I just double checked -- https://meta.wikimedia.org/w/api.php?action=sitematrix&formatversion=2 -- zh-classical is the official language code:

        "313": {
            "code": "zh-classical",
            "name": "文言",
            "site": [
                {
                    "url": "https://zh-classical.wikipedia.org",
                    "dbname": "zh_classicalwiki",
                    "code": "wiki",
                    "sitename": "維基大典"
                }
            ],
            "dir": "ltr",
            "localname": "Classical Chinese"
        },
siddharthvp commented 3 years ago

This is necro-posting, but that's the sitecode, not language code. Going to https://zh-classical.wikipedia.org and hitting mw.config.get('wgContentLanguage') gives lzh.