npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@json_web_component/ji18n

v2.1.5

Published

一个国际化的web component库, 可无感切换语言; An internationalized web component library with senseless language switching.

Downloads

41

Readme

Ji18n 国际化语言处理组件

English Documents

介绍

Ji18n 是一个原生 HTML 标签组件,用于处理国际化语言。它可以将文本、标签、按钮等 HTML 元素的显示语言切换为指定的语言。

配置

Ji18n 组件可以通过以下属性进行配置:

  • messages:翻译文本对象。
  • defaultLanguage:默认语言。
  • backLanguage:回退语言。

方法

Ji18n 组件提供以下方法:

  • createJi18n():创建 Ji18n 实例。
  • setLanguage():设置当前语言。
  • $t():获取翻译文本。
  • onChange():语言更换的回调。

使用示例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script type="module" src="https://unpkg.com/@json_web_component/ji18n/dist/main.min.js"></script>
    <title>Title</title>
    <script type="module">
        const en = {
            "app": {
                "test": "test"
            }
        }
        const zh = {
            "app": {
                "test": "测试"
            }
        }
        const messages = {en, zh}
        // 当不传入 defaultLanguage 和 backLanguage 时, 默认为系统语言
        createJi18n({messages, defaultLanguage: 'en', backLanguage: 'en'});
    </script>
</head>
<body>
<j-trans label="app.test" class="demo"></j-trans>
<input type="radio" name="lan" value="en"><label>English</label>
<input type="radio" name="lan" value="zh"><label>中文</label>
<script>
    const radios = document.querySelectorAll('input[type="radio"][name="lan"]');
    for (let radio of radios) {
        radio.addEventListener('change', e => {
            Ji18n.setLanguage(e.target.value)
            setTimeout(() => {
                console.log(Ji18n.$t("app.test"))
            }, 500)
        })
    }
</script>
</body>
</html>
<!-- 原生 html 引用 -->
<script type="module" src="https://unpkg.com/@json_web_component/ji18n/dist/main.min.js"></script>
// vue3 vite处理
export default defineConfig({
    plugins: [
        vue({
            template: {
                compilerOptions: {
                isCustomElement: tag => tag === 'j-trans'
                }
            }
        })
    ]
})
// vue 或 react只需在 main.js 中引用
import "@json_web_component/ji18n";

window.createJi18n({
  messages: {
    en: {},
    zh: {}
  }
})
// tsconfig 中添加 include
{
  "compilerOptions":{},
  "include": ["node_modules/@json_web_component/ji18n/dist/global.d.ts"],
}
// 如果需要用到 ts , 请实例化的时候重新赋值全局变量
// 示例:
window.Ji18n = createJi18n<"en" | "zh">({messages, defaultLanguage: 'en', backLanguage: 'en'});
// react 使用问题
// 如果ts报错: Property 'j-trans' does not exist on type 'JSX.IntrinsicElements'.
// 1. 创建一个全局类型文件 例如 global.d.ts
// 2. 在tsconfig 中include 中加入该全局类型文件
// 该全局文件的内容如下:
type JTransProps = React.HTMLAttributes<HTMLElement> & {
  label: string;
  options?: Record<string, any>;
  lang?: string;
}
declare namespace JSX {
  interface IntrinsicElements {
    'j-trans': React.DetailedHTMLProps<JTransProps, HTMLElement>;
  }
}