Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JS package with pattern parsers & serializers #46

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions .github/workflows/js.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: JS

on:
push:
branches:
- main
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- run: npm ci
working-directory: ./js
- run: npm test -- --coverage
working-directory: ./js
- run: npm run build
working-directory: ./js

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- run: npm ci
working-directory: ./js
- run: npm run lint
working-directory: ./js
- run: npx prettier --check .
working-directory: ./js
3 changes: 3 additions & 0 deletions js/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/coverage/
/dist/
/node_modules/
92 changes: 92 additions & 0 deletions js/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# @mozilla/l10n

This is a library of JavaScript tools and utilities for working with localization files,
primarily built for internal use at Mozilla.

It's being bootstrapped to fulfil the needs of the [Pontoon](https://pontoon.mozilla.org/) frontend,
and to match the behaviour of the Python [moz.l10n](https://pypi.org/project/moz.l10n/) library.

The data structures used by the library are defined by a
[JSON Schema](https://github.com/mozilla/moz-l10n/blob/main/moz/l10n/message/schema.json).

The library currently supports the following message pattern formats:

- `android`: Android string resources
- `fluent`: Fluent (without internal selectors)
- `mf2`: MessageFormat 2.0
- `plain`: Patterns without placeholders (used in multiple resource formats)
- `webext`: WebExtensions (messages.json)
- `xliff`: XLIFF 1.2, including XCode customizations

The tooling for `android` and `xliff` depends on `DOMParser`, `XMLSerializer`, and related classes
which are available in browser environments.
The parser for `fluent` depends on the `@fluent/syntax` package,
and the parser for `mf2` depends on the `messageformat` package.

## API

### parsePattern()

```js
import { ParseError, parsePattern } from '@mozilla/l10n'
```

```js
function parsePattern(
format: 'android' | 'fluent' | 'mf2' | 'plain' | 'webext' | 'xliff',
src: string,
baseMsg?: Message,
onError?: (error: ParseError) => void
): Pattern

class ParseError extends Error {
range?: [number, number] // Set for fluent, mf2, and webext errors
}
```

Parse the string representation of a single flat message pattern into a data structure
([JSON Schema](https://github.com/mozilla/moz-l10n/blob/main/moz/l10n/message/schema.json)).

The `baseMsg` is required by `webext` for named placeholders.

If `onError` is undefined, errors are thrown.

### serializePattern()

```js
import { SerializeError, serializePattern } from '@mozilla/l10n'
```

```js
function serializePattern(
format: 'android' | 'fluent' | 'mf2' | 'plain' | 'webext' | 'xliff',
pattern: Pattern,
onError?: (error: SerializeError) => void
): string

class SerializeError extends Error {
pos?: number // Set for fluent, plain, and webext errors
}
```

Serialize the data representation of a single flat message pattern into a string

If `onError` is undefined, errors are thrown.

### Types

```ts
import type {
Message,
PatternMessage,
SelectMessage,
Pattern,
Expression,
Markup
} from '@mozilla/l10n'
```

```ts
type Message = Pattern | PatternMessage | SelectMessage
type Pattern = (string | Expression | Markup)[]
```
11 changes: 11 additions & 0 deletions js/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// @ts-check

import eslint from '@eslint/js'
import prettier from 'eslint-config-prettier'
import tseslint from 'typescript-eslint'

export default tseslint.config(
eslint.configs.recommended,
tseslint.configs.recommended,
prettier
)
Loading