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

Add JS package with pattern parsers & serializers #46

merged 5 commits into from
Feb 7, 2025

Conversation

eemeli
Copy link
Member

@eemeli eemeli commented Jan 29, 2025

Adds the start of a JS library in parallel with the Python one, primarly to fulfil the needs of the Pontoon frontend.

Includes support for the following message pattern formats:

  • android: Android string resources
  • fluent: Fluent (without internal selectors)
  • mf2: MessageFormat 2.0
  • plain: Patterns without placeholders
  • 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.

The primary entry points are:

import { parsePattern, serializePattern } from '@mozilla/l10n'
function parsePattern(
  format: 'android' | 'fluent' | 'mf2' | 'plain' | 'webext' | 'xliff',
  src: string,
  baseMsg?: Message,
  onError?: (error: ParseError) => void
): Pattern

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

These functions convert a flat message pattern between its data structure representation, and the format's serialized form. The data structure is the same as the one used by moz.l10n.message.message_from_json and moz.l10n.message.message_to_json.

A CI test action is included, and the package should be ready to publish on npm.

@eemeli eemeli requested a review from mathjazz January 29, 2025 19:18
Copy link
Contributor

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you thought about putting the JS library into a separate repository? It feels a bit odd to keep the Python version of the library in /moz/l10n when the JS version is in /js.

@eemeli eemeli requested a review from mathjazz February 7, 2025 11:02
@eemeli
Copy link
Member Author

eemeli commented Feb 7, 2025

Have you thought about putting the JS library into a separate repository?

Yes, but decided that having it in the same repo is the best way to ensure that the functionalities that are supported by both the Python & JS implementations continue to match. If we need to e.g. add a field to the JSON Schema or change how XLIFF patterns are represented, we can (and should!) apply the change to both implementations in the same PR.

It feels a bit odd to keep the Python version of the library in /moz/l10n when the JS version is in /js.

I can move the Python version to a python/ subdirectory after this lands, but didn't want to touch it in the same PR.

Copy link
Contributor

@mathjazz mathjazz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@mathjazz
Copy link
Contributor

mathjazz commented Feb 7, 2025

I can move the Python version to a python/ subdirectory after this lands, but didn't want to touch it in the same PR.

I'll leave that decision to you - mostly wanted to understand if you've considered a separate repo.

@eemeli eemeli merged commit f9dae80 into main Feb 7, 2025
13 checks passed
@eemeli eemeli deleted the js branch February 7, 2025 12:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants