Skip to main content
When you need to store multilingual values in the database, you can use locale-specific columns like name, name_ko, name_en, or a locale map like name: { ko: "...", en: "..." }. Sonamu automatically selects the appropriate value for the current locale using the localizedColumn() function.

localizedColumn Function

import { localizedColumn } from "../i18n/sd.generated";

const tag = {
  id: 1,
  name: "default",
  name_ko: "ํƒœ๊ทธ",
  name_en: "Tag",
};

const nestedTag = {
  id: 2,
  name: {
    ko: "ํƒœ๊ทธ",
    en: "Tag",
  },
};

// When Context locale is "ko"
localizedColumn(tag, "name"); // "ํƒœ๊ทธ"
localizedColumn(nestedTag, "name"); // "ํƒœ๊ทธ"

// When Context locale is "en"
localizedColumn(tag, "name"); // "Tag"
localizedColumn(nestedTag, "name"); // "Tag"

// When Context locale is "fr" (unsupported locale)
localizedColumn(tag, "name"); // "ํƒœ๊ทธ" (uses the default locale)

Priority Order

localizedColumn() looks for values in the following order:
Current localePriority
koname_ko โ†’ name โ†’ name_en
enname_en โ†’ name โ†’ name_ko
When suffix columns and a locale map both exist, the suffix column for the current locale is checked first, then the locale map value. Empty strings ("") and null are skipped, and the next priority is checked.

Entity Definition

Example of an Entity with multilingual columns:
{
  "id": "Tag",
  "table": "tags",
  "props": [
    { "name": "id", "type": "integer" },
    { "name": "name", "type": "string", "length": 100, "desc": "Default name" },
    { "name": "name_ko", "type": "string", "length": 100, "nullable": true, "desc": "Korean name" },
    { "name": "name_en", "type": "string", "length": 100, "nullable": true, "desc": "English name" }
  ]
}

Using in Models

Processing in Subset

You can use localizedColumn in a Subsetโ€™s enhancer to deliver only a single name field to the client:
import { localizedColumn } from "../i18n/sd.generated";

class TagModelClass extends BaseModelClass {
  async findMany<T extends TagSubsetKey>(subset: T, params: TagListParams) {
    // ...

    const enhancers = this.createEnhancers({
      A: (row) => ({
        ...row,
        // Replace with locale-appropriate name
        displayName: localizedColumn(row, "name"),
      }),
    });

    return this.executeSubsetQuery({ subset, qb, params, enhancers });
  }
}

Using in API Response

@api({ httpMethod: "GET" })
async getTagOptions(): Promise<{ id: number; label: string }[]> {
  const tags = await this.findMany("A", { num: 100 });

  return tags.rows.map(tag => ({
    id: tag.id,
    label: localizedColumn(tag, "name") ?? tag.name,
  }));
}

Using in Frontend

The same function can be used in the frontend (exported from webโ€™s sd.generated.ts):
import { localizedColumn } from "@/i18n/sd.generated";

function TagBadge({ tag }: { tag: Tag }) {
  return <span className="badge">{localizedColumn(tag, "name")}</span>;
}

Supported Value Shapes

localizedColumn() supports two storage shapes:
ShapeExampleDescription
Suffix columnsname, name_ko, name_enStore one column per locale.
Locale map{ name: { ko: "ํƒœ๊ทธ", en: "Tag" } }Store locale-specific values in the base column object.
The selected value can be a string or string[]. string[] values are returned as arrays and are not stringified. For suffix columns or base columns, scalar values such as number, boolean, or bigint are stringified for backward compatibility.

Suffix Column Naming Patterns

Base columnLocale columnsDescription
namename_ko, name_enName
titletitle_ko, title_enTitle
descriptiondescription_ko, description_enDescription
contentcontent_ko, content_enContent
When using suffix columns, use the {base_column}_{locale} format. When using a locale map, the base column value can store the locale-specific object.

Important Notes

  • localizedColumn is simply a function that selects column values. Query optimization (SELECTing only necessary columns) must be handled separately.
  • If all locale columns are null and the base column is also empty, undefined is returned.
  • If locale is not set in Context, defaultLocale is used.
  • If Context locale is unsupported, values are resolved from defaultLocale.

i18n Setup

Initial i18n configuration

Using the SD Function

Writing and using dictionaries