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

Observe the language of a node #9918

Closed
jakearchibald opened this issue Nov 8, 2023 · 2 comments
Closed

Observe the language of a node #9918

jakearchibald opened this issue Nov 8, 2023 · 2 comments
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest

Comments

@jakearchibald
Copy link
Contributor

jakearchibald commented Nov 8, 2023

What problem are you trying to solve?

Let's say I'm creating a custom form input field as a custom element. The implementation includes labels, titles, alt text etc.

Ideally, like regular HTML inputs, this text should be presented in the correct locale & language, and updated if those conditions change.

What solutions exist today?

There's an algorithm for determining language which would be simple to reimplement (although it's sad that it would need to be reimplemented).

However, it gets a bit tricky when it comes to observing when to re-run this algorithm. You'd need to observe the tree between the element and the ancestor with lang (or the root) for changes to the lang attribute (on all elements in the sequence), or changes to the tree structure.

:lang() already exists as a CSS selector, but there's no way to observe changes in selector matching. I guess you could try to use this in combination with a resize observer to detect when this selector matches/unmatches.

There's a languagechange event, but this only operates on the global.

How would you solve it?

A few ideas:

node.computedLang - returns the result of determine the language of a node.

But there also needs to be a way to observe changes:

Option 1

Extend MutationObserver (or create a new LanguageObserver) to allow for observing computedLang. It feels like this should be an observer rather than an event since it's so closely linked to DOM changes.

Option 2

Provide a way to observe changes in CSS selector matching.

const result = element.matchSelector(`:lang(${element.computedLang})`);

result.addEventListener('change', () => {
  const newLang = element.computedLang;
  // …
});

This is based on window.matchMedia, but matches a selector.

Anything else?

No response

@jakearchibald jakearchibald added addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest labels Nov 8, 2023
@annevk
Copy link
Member

annevk commented Nov 8, 2023

cc @whatwg/css @rniwa @dbaron

@domenic
Copy link
Member

domenic commented Nov 8, 2023

Dupe of #7039 and #7994, although the problem statement here is quite nice. Let's consolidate in the former though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
addition/proposal New features or enhancements needs implementer interest Moving the issue forward requires implementers to express interest
Development

No branches or pull requests

3 participants