Skip to content

Instantly share code, notes, and snippets.

@rxaviers
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rxaviers/dbd2e56840c5c7a508e8 to your computer and use it in GitHub Desktop.
Save rxaviers/dbd2e56840c5c7a508e8 to your computer and use it in GitHub Desktop.
Example using cldr-data + to filter CLDR files and fields

Consider we have the following application.

.
├── <my application files>
└── bower.json
// bower.json
{
  "dependencies": {
    "lib-foo": "1.2.3",
    "lib-bar": "4.5.6",
    "lib-baz": "7.8.9"
  }
}

// libFoo v1.2.3's bower.json
{
  "dependencies": {
    "cldr-data": ">23.1"
  }
}

// libBar v4.5.6's bower.json
{
  "dependencies": {
    "cldr-data": ">=25"
  }
}

// libBaz v7.8.9's bower.json
{
  "dependencies": {
    "cldr-data": ">23.1 <26"
  }
}
bower install

.
├── bower_components
│   ├── cldr-data
│   │   └── index.json
│   ├── libBar
│   ├── libBaz
│   └── libFoo
└── bower.json

Note that bower will resolve the cldr-data version. Considering the above dependencies, we'd end up with cldr-data@25.x. Therefore:

// bower_components/cldr-data/index.json
{
  "json": "http://www.unicode.org/Public/cldr/25/json.zip",
  "json-full": "http://www.unicode.org/Public/cldr/25/json_full.zip"
}

In order to populate bower_components/cldr-data, one can run: (or, obviously, place this on its .bowerrc post install hook and don't worry about it)

$ npm install cldr-data-downloader
$ ./node_modules/cldr-data-downloader/bin/download.js -i bower_components/cldr-data/index.json -o bower_components/cldr-data

.
├── bower_components
│   ├── cldr-data
│   │   ├── main
│   │   │   ├── ...
│   │   │   └── zh-Hant
│   │   ├── segments
│   │   │   ├── ...
│   │   │   └── ru
│   │   └── supplemental
│   ├── libBar
│   ├── libBaz
│   └── libFoo
└── bower.json

All the above has already been implemented. Feel free to try (a) bower install cldr-data#25 and then the (b) populate step. Obviously, we could improve it with your ideas.

Now, to the filter part, which we need to discuss further and to establish how to declare it. One initial idea for filtering the files is:

// bower_components/libFoo/cldr.json
{
  "locales": [ "en", "es", "zh" ], // The default would be including all locales.
  "jsons": [
    "main/ca-*", // The filter algorithm is smart enough to expand `main/ca-*` into `main/<locale>/ca-*`.
    "supplemental/likelySubtags"
  ]
}

Optionally, instead of creating a new file cldr.json we could re-use bower.json with a cldr key.

To have downloader to use the new filter, we could pass it as a parameter: cldr-data-downloader/bin/download.js -f bower_components/libFoo/cldr.json. The command could accept any number of -f parameters, which would be all merged.

About the fields filtering, one suggestion is using key-value pairs, where keys are json files and its value is the fields to be included (or excluded). For example:

// bower_components/libFoo/cldr.json
{
  "locales": [ "en", "es", "zh" ], // The default would be including all locales.
  "jsons": {
    "main/ca-*": [
      "main/*/dates/calendars/islamic/(months|days|dateTimeFormats)",
      "main/*/dates/calendars/islamic/eras"
    ],
    "main/numbers": [
      "main/*/numbers/currencies/currency"
    ]
  ]
}

We could allow a mix of full jsons using Array and fields filtered jsons using objects with:

// bower_components/libFoo/cldr.json
{
  "locales": [ "en", "es", "zh" ], // The default would be including all locales.
  "jsons": [
    "supplemental/likelySubtags",
    {
      "main/ca-*": [
        "main/*/dates/calendars/islamic/(months|days|dateTimeFormats)",
        "main/*/dates/calendars/islamic/eras"
      ],
      "main/numbers": [
        "main/*/numbers/currencies/currency"
      ]
    }
  ]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment