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

google-object-translate

v1.0.3

Published

A module that parse serializable javascript object to pseudo HTML to translate and parse it back.

Downloads

11

Readme

google-object-translate

A module that parse object to pseudo HTML to translate and parse it back.

The core concept of this module is that google can translate any html while keeping its structure. Therefore, we can parse the pseudo HTML to translate and parse it back.

Installation

npm install google-object-translate

To use google-object-translate/translate module, package @google-cloud/translate is also required:

npm install @google-cloud/translate

Usage

Use build-in translate function

import {translate} from 'google-object-translate';

const client = translate.ObjectTranslate.createClient('v2', {
    // your google-cloud config here
    // See: https://cloud.google.com/nodejs/docs/reference/translate/latest/translate/v2.translateconfig
})

const objToTranslate = {
    "title": "Hello World",
    "viewerNumber": 1,
    "content": "World",
    "hybridList": [
        "Hello",
        null,
        undefined,
        "World"
    ]
}

const result = await client.translate(
    objToTranslate,
    // See: https://cloud.google.com/nodejs/docs/reference/translate/latest/translate/v2.translaterequest
    {
        from: 'en',
        to: 'zh-CN',

    })
// result = {
//     "title": "你好世界",
//     "viewerNumber": 1,
//     "content": "世界",
//     "hybridList": [
//         "你好",
//         null,
//         undefined,
//         "世界"
//     ]
// }

Use custom translate function

import {parser} from 'google-object-translate';

parser.fromObject : Parse object to pseudo HTML

  • Arguments:

| Name | Type | Required | Default | Comment | | -------- | ------------------------------------------------- | -------- | -------- | ------------------------------------------------------------ | | obj | TranslationObject | true | - | The object to translate | | filter | (path: string[], sentence: Sentence) => boolean | false | ()=>true | Filter function that decide a Sentence is translatable or not. True means it is translatable. |

  • Return Type: string

parser.toObject : Parse pseudo HTML back to object

  • Arguments:

| Name | Type | Required | Default | Comment | | --------------- | ----------------- | -------- | --------- | ------------------------------------------------------------ | | pseudoHTML | string | true | - | - | | _originalObject | TranslationObject | false | undefined | The original object that will be merged to translated object. |

  • Return Type: TranslationObject

Todo

  • [ ] Support v3 and v3-beta API in translate.ts

Types

| Type | Alias for | | ----------------- |-----------| | Sentence | string | object | undefined | | SentenceArray | Sentence[] | | TranslationObject | Sentence | SentenceArray |

Conversion Rules

Some values are skipped

Some values will not be included to pseudo HTML because it's meaningless. This includes:

  • Number
  • Boolean
  • Empty String
  • null
  • undefined

But these values in an array will be replaced with undefined as placeholder.

string

before:

"some str"

after:

some str

Array

Untranslatable item will become empty tag to maintain the structure.

before:

["item 1", 123, true, "item 2"]

after:


<body>
<ol>
    <li>item 1</li>
    <li></li>
    <li></li>
    <li>item 2</li>
</ol>
</body>

Object

before:

{
  "a": "",
  "b": [
    "item1",
    2,
    true
  ],
  "c": null,
  "d": {
    "d1": "d1v",
    "d2": "d2v"
  }
}

after:

<body>
<div>
    <ol id="b">
        <li>item1</li>
        <li></li>
        <li></li>
    </ol>
    <div id="d">
        <p id="d1">d1v</p>
        <p id="d2">d2v</p>
    </div>
</div>
</body>