0

我有以下咖啡脚本代码,它读取文件系统并提取目录名称:

  fs.readdir "./locales", (err,locales) ->
    EXCLUDE = [ 'dev', 'README.md', 'config.json' ]
    results = __.reject locales, (value, index, list) ->
      return EXCLUDE.indexOf(value) != -1
    languages = {}
    locales = __.each results, (value, index, list) ->
      code =  value
      locale = value.split("-")[0]
      console.log locale
      console.log code
      language = cldr.extractLanguageDisplayNames(locale)[locale]
      console.log language
      languages.locale = language
      #languages.push[{locale: language}]
    console.log languages

控制台日志输出:

cs
cs-CZ
čeština
en
en-UK
English
fr
fr-FR
français
pt
pt-PT
português
sk
sk-SK
slovenčina

制作 json 文件的正确方法是什么:

{
  "cs": {
    "locale": "cs",
    "code": "cs-CZ"
  },
  "en": {
    "locale": "en",
    "code": "en-UK"
  },
  "fr": {
    "locale": "fr",
    "code": "fr-FR"
  },
  "pt": {
    "locale": "pt",
    "code": "pt-PT"
  },
  "sk": {
    "locale": "sk",
    "code": "sk-SK"
  }
}

我想languages返回上述对象并在全局命名空间中可用,以便我可以在我的应用程序中使用它。

4

1 回答 1

0

好的,我想通了:

  fs.readdir "./locales", (err,locales) ->
    EXCLUDE = [ 'dev', 'README.md', 'config.json' ]
    languages = {}
    results = __.reject locales, (value, index, list) ->
      return EXCLUDE.indexOf(value) != -1
    locales = __.each results, (value, index, list) ->
      locale = value.split("-")[0]
      language = cldr.extractLanguageDisplayNames(locale)[locale]
      languages[locale] = language
    console.log languages

返回

{ cs: 'čeština',
  en: 'English',
  fr: 'français',
  pt: 'português',
  sk: 'slovenčina' }
于 2013-08-14T14:46:34.037 回答