├── .gitattributes ├── .github └── workflows │ └── build.yml ├── .gitignore ├── .npmrc ├── LICENSE ├── README.md ├── package.json └── spec.emu /.gitattributes: -------------------------------------------------------------------------------- 1 | index.html -diff merge=ours 2 | spec.js -diff merge=ours 3 | spec.css -diff merge=ours 4 | -------------------------------------------------------------------------------- /.github/workflows/build.yml: -------------------------------------------------------------------------------- 1 | name: Deploy spec 2 | 3 | on: 4 | push: 5 | paths: 6 | - spec.emu 7 | jobs: 8 | build: 9 | runs-on: ubuntu-latest 10 | 11 | steps: 12 | - uses: actions/checkout@v2 13 | - uses: actions/setup-node@v1 14 | with: 15 | node-version: '16.x' 16 | - run: npm install 17 | - run: npm run build 18 | - name: commit changes 19 | uses: elstudio/actions-js-build/commit@v3 20 | with: 21 | commitMessage: "fixup: [spec] `npm run build`" 22 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | 6 | # Runtime data 7 | pids 8 | *.pid 9 | *.seed 10 | 11 | # Directory for instrumented libs generated by jscoverage/JSCover 12 | lib-cov 13 | 14 | # Coverage directory used by tools like istanbul 15 | coverage 16 | 17 | # nyc test coverage 18 | .nyc_output 19 | 20 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 21 | .grunt 22 | 23 | # node-waf configuration 24 | .lock-wscript 25 | 26 | # Compiled binary addons (http://nodejs.org/api/addons.html) 27 | build/Release 28 | 29 | # Dependency directories 30 | node_modules 31 | jspm_packages 32 | 33 | # Optional npm cache directory 34 | .npm 35 | 36 | # Optional REPL history 37 | .node_repl_history 38 | 39 | # Only apps should have lockfiles 40 | yarn.lock 41 | package-lock.json 42 | npm-shrinkwrap.json 43 | pnpm-lock.yaml 44 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 ECMA TC39 and contributors 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Intl.MessageFormat.parseResource() 2 | 3 | This is a follow-on proposal to the [Intl.MessageFormat proposal](https://github.com/tc39/proposal-intl-messageformat), 4 | adding support for handling entire resources (aka bundles) of messages, 5 | in addition to supporting individual messages. 6 | 7 | ## Status 8 | 9 | Champion: Eemeli Aro (Mozilla/OpenJS Foundation) 10 | 11 | ### Stage: 1 12 | 13 | ## Motivation and Use Cases 14 | 15 | In most use cases, systems that have a need to format messages need to format more than one such message. 16 | For example, a dialog box may include a title, description, and one or more buttons with labels. 17 | These messages need to be handled together as a cohesive unit, 18 | both when editing or translating, as well as when formatting. 19 | 20 | To enable this, a message resource syntax is being developed in parallel with the MessageFormat 2.0 specification. 21 | This proposal adds a static method `Intl.MessageFormat.parseResource()` that allows for parsing such resources. 22 | Such a method would allow for MF2 messages to be stored and transmitted in a purpose-built container, 23 | rather than needing to be separately parsed for use in JavaScript environments. 24 | 25 | ## API Description 26 | 27 | As a baseline, this proposal presumes the existence of `Intl.MessageFormat` as described in its proposal, 28 | and extends it. 29 | 30 | ### MessageFormat.parseResource(resource, locales?, options?) 31 | 32 | This static method parses a string representation of an MF2 resource, 33 | constructing a Map with a corresponding structure of `MessageFormat` instances for each of the resource's messages. 34 | Its `locales` and `options` arguments are used to construct each such instance. 35 | 36 | A `MessageResource` is a Map representing a collection of related messages for a single locale. 37 | Messages can be organized in a flat structure, or in hierarchy, using paths. 38 | Conceptually, it is similar to a file containing a set of messages, 39 | but there are no constrains implied on the underlying implementation. 40 | 41 | ```ts 42 | type MessageResource = Map; 43 | 44 | class Intl.MessageFormat { 45 | static parseResource( 46 | resource: string, 47 | locales?: string | string[], 48 | options?: MessageFormatOptions 49 | ): MessageResource; 50 | 51 | ... 52 | } 53 | ``` 54 | 55 | ## Example 56 | 57 | Given an MF2 resource as follows: 58 | 59 | ```ini 60 | # Note! MF2 syntax is under development; this may still change 61 | 62 | greeting = {Hello {$place}!} 63 | 64 | new_notifications = 65 | match {$count} 66 | when 0 {You have no new notifications} 67 | when one {You have {$count} new notification} 68 | when * {You have {$count} new notifications} 69 | ``` 70 | 71 | This could be used in code like this: 72 | 73 | ```js 74 | const source = ... // string source of resource as above 75 | const res = Intl.MessageFormat.parseResource(source, ['en']); 76 | 77 | const greeting = res.get('greeting').resolveMessage({ place: 'world' }); 78 | greeting.toString(); // 'Hello world!' 79 | 80 | const notifications = res.get('new_notifications').resolveMessage({ count: 1 }); 81 | notifications.toString(); // 'You have 1 new notification' 82 | ``` 83 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "proposal-intl-message-resource", 3 | "description": "A TC39 proposal for Intl.MessageFormat.parseResource()", 4 | "private": true, 5 | "scripts": { 6 | "start": "npm run build-loose -- --watch", 7 | "build": "npm run build-loose -- --strict", 8 | "build-loose": "ecmarkup --load-biblio @tc39/ecma262-biblio --verbose spec.emu index.html --lint-spec" 9 | }, 10 | "homepage": "https://github.com/eemeli/proposal-intl-message-resource", 11 | "repository": { 12 | "type": "git", 13 | "url": "git+https://github.com/eemeli/proposal-intl-message-resource.git" 14 | }, 15 | "license": "MIT", 16 | "devDependencies": { 17 | "@tc39/ecma262-biblio": "^2.1.2390", 18 | "ecmarkup": "^14.1.1" 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /spec.emu: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 |
 7 | title: Proposal Title Goes Here
 8 | stage: -1
 9 | contributors: Your Name(s) Here
10 | 
11 | 12 | 13 |

This is an emu-clause

14 |

This is an algorithm:

15 | 16 | 1. Let _proposal_ be *undefined*. 17 | 1. If IsAccepted(_proposal_), then 18 | 1. Let _stage_ be *0*. 19 | 1. Else, 20 | 1. Let _stage_ be *-1*. 21 | 1. Return ? ToString(_proposal_). 22 | 23 |
24 | --------------------------------------------------------------------------------