Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 265

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 62k
Author
Asked: November 25, 20242024-11-25T09:16:07+00:00 2024-11-25T09:16:07+00:00

i18n-manager web component

  • 62k

i18n-manager is an element and helpers classes produced to help us manage our i18n needs in web components.

Recapping requirements:

  • Must work without requiring dependencies to adopt
  • Must have good / simple DX
  • Must work with multiple translation files

Goal is to translate ALL of our elements but ultimately supply full internationalization support for the HAX editor, a complex Mobx powered series of web components to transform web editing.

What integration looks like

Here's a simple example of an integration using our @lrnwebcomponents/self-check web component. (code simplified to highlight integration only)

import { LitElement, html, css, svg } from "lit-element/lit-element.js"; import { I18NMixin } from "@lrnwebcomponents/i18n-manager/lib/I18NMixin.js";  class SelfCheck extends I18NMixin(LitElement) {   constructor() {     super();     this.t = {       reveal: "Reveal Answer",       close: "Close",       more: "More information",     };     this.registerTranslation({       context: this,       basePath: import.meta.url,       locales: ["en-UK", "ja"],     });   }   static get styles() {     return [       ...super.styles,       css`         :host {           display: block;           margin: 15px 0;         }       `,     ];   }   render() {     return html`     <div class="card">       <simple-icon-button         controls="answer_wrap"         aria-label="${this.t.reveal}"         id="checkBtn"         class="check-btn"         icon="icons:check-circle"         ?dark="${this.dark}"         @click="${this.openAnswer}"       ></simple-icon-button>       <simple-tooltip aria-hidden="true" for="checkBtn" position="left">         ${this.t.reveal}       </simple-tooltip>       <user-action track="click" every         ><a href="${this.link}" target="_blank"           >${this.t.more}...</a         ></user-action       >     </div>`;   } } window.customElements.define(SelfCheck.tag, SelfCheck); export { SelfCheck }; 
Enter fullscreen mode Exit fullscreen mode

This has 4 key parts:

  • I18NMixin is applied to LitElement base class.
  • this.t = {} has keys which are then supplying strings.
  • this.registerTranslation supplies the context, a reference to the current file location (the import.meta.url) portion and then an Array of locales that it supports
  • the render function then leverages this.t.reveal which will print the text in its place, defaulting to en

From the code above:

this.registerTranslation({       context: this,       basePath: import.meta.url,       locales: ["en-UK", "ja"],     }); 
Enter fullscreen mode Exit fullscreen mode

We can see that the self-check element supports English, British English (so dialect within a language), and Japanese. The locales + basePath imply that there are JSON files located in a pattern of {pathToThisElement}/locales/{tagName}.{lang}.json and then data found there is injected back into this element when language changes. This causes the this.t object to be updated, which in turn causes the element to reflect the update.

self-check.ja.json

{   "reveal": "答えを明らかにする",   "close": "閉じる",   "more": "詳細" } 
Enter fullscreen mode Exit fullscreen mode

This integration has LitElement in mind and while the mixin is vanilla, it's making some assumptions about LitElement obviously via the this.t as t is supplies as an Object in the properties getter. However, this can work with 0 dependencies as the main thing happening in this Mixin is simply sending an event!

zero dependency integration

Here's all you need to work with i18n-manager:

constructor() {   super();   this.t = {     whatever: "Whatever"   };   window.dispatchEvent(       new CustomEvent("i18n-manager-register-element", {         detail: {           context: this,           namespace: "simple-login",           localesPath:             new URL('../locales', import.meta.url).href,           updateCallback: "render",           locales: ["es"],         },       })     ); } 
Enter fullscreen mode Exit fullscreen mode

uhh… how'd you do that!?

This supplies a context this, namespace (name of the element), a resolved localesPath based on loaded file location on the front end, an updateCallback (saying to run render on variable change), and then a locales Array.

Then it all magically works because, Magic.

No really, how we made that work

This works because we've adopted a Singleton pattern; meaning that we have a single web component that sits in the DOM and is in charge of a single concept of state management. In this case, i18n-manager is in charge of JUST translation. Who cares about translation, what translations are supplied, what the language currently is, and who to notify when language changes.

The next post will go a bit into how i18n-manager's internals are constructed and lead into a video stepping through the whole process described in these posts.

haxthewebi18nwebcomponentswebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.