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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@itrocks/property-translate

v0.0.3

Published

A @Translate property decorator that translates a property's value at display time

Readme

npm version npm downloads GitHub issues discord

property-translate

A @Translate property decorator that translates a property's value at display time.

This documentation was written by an artificial intelligence and may contain errors or approximations. It has not yet been fully reviewed by a human. If anything seems unclear or incomplete, please feel free to contact the author of this package.

Installation

npm i @itrocks/property-translate

Usage

@itrocks/property-translate provides a property decorator @Translate() that marks a field as “translatable”: instead of displaying the raw stored value, the value is passed through your translation system when rendered to the user (for example with @itrocks/transformer and @itrocks/translate).

You can:

  • decorate properties with @Translate() so their display value is automatically translated,
  • inspect a property with translateOf() to know whether translation is enabled,
  • plug the package into your transformer pipeline using propertyTranslateDependsOn() and the helpers from the @itrocks/property-translate/transformers sub-module.

Minimal example

import { Translate } from '@itrocks/property-translate'

class Product {
  // The stored value is a translation key, e.g. "product.name.book"
  @Translate()
  name = ''

  // Raw value, never translated automatically
  sku  = ''
}

In this example, any display logic that is aware of @Translate() can apply the translation before showing name (for instance in a table, detail view or template), while sku is output as-is.

Complete example with transformer wiring

In a typical application you combine this package with @itrocks/transformer and @itrocks/translate. The wiring below is high-level and intentionally simplified: the exact integration will depend on how you configure your framework.

import type { ObjectOrType } from '@itrocks/class-type'
import { applyTransformer, HTML, OUTPUT } from '@itrocks/transformer'
import { tr } from '@itrocks/translate'
import {
  Translate,
  propertyTranslateDependsOn,
} from '@itrocks/property-translate'
import {
  setPropertyTranslateTransformers,
} from '@itrocks/property-translate/transformers'

// Tell the translation decorator how to register its transformers
propertyTranslateDependsOn({
  setTransformers: setPropertyTranslateTransformers,
})

class Product {
  // Will be translated at display time using tr('product.name.book')
  @Translate()
  name = 'product.name.book'

  // Not translated
  description = ''
}

async function propertyOutput<T extends object>(
  object: T,
  property: keyof T,
): Promise<string> {
  // In a real app this function is usually provided by the framework
  return applyTransformer(
    await (object as any)[property],
    object as T,
    property as any,
    HTML,
    OUTPUT,
  ) as Promise<string>
}

async function showProduct(product: Product) {
  console.log(await propertyOutput(product, 'name'))
  console.log(await propertyOutput(product, 'description'))
}

Here, name is stored as a translation key and is automatically passed through tr() when displayed, while description is displayed as-is.

API

function propertyTranslateDependsOn(dependencies: Partial<Dependencies>): void

Registers how @Translate() should configure its transformers. This is typically called once at application startup.

Parameters

  • dependencies – an object that can provide the following optional field:
    • setTransformers – function that receives a target object and property name, and installs the transformers used to translate the property at display time.

Typical usage

You normally pass setPropertyTranslateTransformers from the @itrocks/property-translate/transformers module:

import { propertyTranslateDependsOn } from '@itrocks/property-translate'
import { setPropertyTranslateTransformers } from '@itrocks/property-translate/transformers'

propertyTranslateDependsOn({
  setTransformers: setPropertyTranslateTransformers,
})

function Translate<T extends object>(value?: boolean): DecorateCaller<T>

Property decorator indicating that a field should be translated at display time.

Parameters

  • value (optional, default: true) – enables or disables the translation marker on the property. In everyday code you will usually write simply @Translate(). Passing false can be useful in advanced scenarios to remove or override an inherited translation marker.

Return value

  • DecorateCaller<T> – function from @itrocks/decorator/property used internally by TypeScript to apply the decorator. You normally do not call it directly; you just apply @Translate() to the property.

Example

class Category {
  // Uses translation
  @Translate()
  label = 'category.label.food'

  // Explicitly disabled (no translation)
  @Translate(false)
  rawCode = 'FOOD'
}

function translateOf<T extends object>(target: ObjectOrType<T>, property: KeyOf<T>): boolean

Checks whether a given property is marked with @Translate().

Parameters

  • target – the class (Product) or instance (new Product()) that owns the property to check.
  • property – the name of the property to inspect.

Return value

  • booleantrue if the property is currently marked as translatable, false otherwise.

Example

import type { ObjectOrType } from '@itrocks/class-type'
import { Translate, translateOf } from '@itrocks/property-translate'

class Product {
  @Translate()
  name = 'product.name.book'

  sku = ''
}

function isTranslatable<T extends object>(
  type: ObjectOrType<T>,
  property: keyof T,
): boolean {
  return translateOf(type, property)
}

isTranslatable(Product, 'name') // true
isTranslatable(Product, 'sku')  // false

Transformers sub-module

The @itrocks/property-translate/transformers sub-module exposes helpers that integrate @Translate() with the generic transformer pipeline used by the framework.

function setPropertyTranslateHtmlTransformers<T extends object>(target: ObjectOrType<T>, property: KeyOf<T>): void

Registers the transformers required to translate a property when generating HTML output.

You rarely call this directly. It is mainly used internally by setPropertyTranslateTransformers, but it can be useful if you only want HTML-level integration and manage other kinds of output yourself.

function setPropertyTranslateTransformers<T extends object>(target: ObjectOrType<T>, property: KeyOf<T>): void

Registers all transformers used by @Translate() for a given property. This is the function you usually reference from propertyTranslateDependsOn.

Example

import { propertyTranslateDependsOn } from '@itrocks/property-translate'
import { setPropertyTranslateTransformers } from '@itrocks/property-translate/transformers'

// Called once at startup
propertyTranslateDependsOn({
  setTransformers: setPropertyTranslateTransformers,
})

Typical use cases

  • Store translation keys in your domain models ('product.name.book') and automatically translate them when generating user-visible output.
  • Mark only some properties of a model as translatable (labels, titles, status names) while leaving others as raw values or codes.
  • Build a generic UI (tables, forms, detail views) that inspects decorator metadata (translateOf) to decide whether a field should be passed through the translation system.
  • Integrate property-level translation with the @itrocks/transformer pipeline using propertyTranslateDependsOn and setPropertyTranslateTransformers.