Platform

Translated product copy for GoCommerce

A Go package that serves catalogue content in the language a shopper asked for. The engine already negotiates the language and has a port for overrides; this module is the table behind that port — product titles, descriptions and the two SEO fields, per language, written through three admin routes.

What it does

In the package’s words, the engine already had every part of this except the words. Config.Languages declares what a store serves, each request is negotiated down to one of them, and every public product read asks a translator port for overrides. Before this module nothing implemented that port, so a store configured for English and French negotiated French correctly and was then handed the English title.

It translates four product fields: title, description, seo_title and seo_description. A field with no translation keeps the product’s stored text — half a translation is common, and an English description beats an empty one.

A regional tag falls back to its language: a row stored for fr serves a shopper asking for fr-CA, and a row stored for fr-CA wins over it. Lookups are batched, one query for a page of products rather than one per product.

Configuration

The module has no settings of its own — translations.New() takes none. Whether it does anything is decided by the engine’s language list, set in gocommerce.Config.

Translations module settings — 2 settings, 1 required
SettingEnvironment variableRequiredWhat it does
Languages — Yes The engine’s setting, not the module’s: the tags the store serves. The module has nothing to serve until there is a language besides the default.
DefaultLanguage — No The language the catalogue itself is written in, en unless set. A request in the default language skips the translator entirely.

The module reads its Config struct, not the environment. The variable names are the ones the package’s own example or the reference binary uses; in your own main() you choose where each value comes from. Where a setting has a panel label, the admin’s settings drawer can hold it too, and a value typed there wins over Config.

Setting it up

  1. Declare the languagesSet Languages in gocommerce.Config — en, fr and de in the package’s example — with the catalogue’s own language as the default.
  2. Install the modulePass translations.New() to gocommerce.New in your own main(), as below. Neither the reference binary nor examples/store installs it.
  3. Write translationsPUT /api/admin/x/translations/product/{id}/{language} with fields for title, description, seo_title or seo_description. An unknown field is refused; an empty one is dropped.
  4. Ask in a languageThe storefront sends ?lang=fr or an Accept-Language header on product reads, and gets French where it exists and the stored text where it does not.

main.go

import (
	"github.com/itswadesh/gocommerce/core"
	"github.com/itswadesh/gocommerce/ext/translations"
)

app, err := gocommerce.New(gocommerce.Config{
	// DBURL, Addr and the rest of your Config, then:
	Languages: []string{"en", "fr", "de"},
}, translations.New())

The package doc’s own example, with its imports; the first language listed is the default unless DefaultLanguage says otherwise. Import path github.com/itswadesh/gocommerce/ext/translations.

How it works

What it does not do

Read these before an order depends on it. No store is known to run GoCommerce in production yet, so these come from the code, not from anyone’s experience.

FAQ

Questions about the Translations module

How does the store pick a language?

An explicit ?lang= wins, then Accept-Language in the client’s order of preference, then the default. Only languages in Config.Languages are considered, and a ?lang= the store does not serve falls back to the default.

What exactly is translated?

Four product fields: title, description, seo_title and seo_description. Anything without a translation keeps the product’s stored text.

Are category names translated?

No. The engine applies translations to products only, so category and collection names — and so the navigation — stay in the default language. The package says so rather than leaving it to be discovered.

Is there a screen for entering translations?

Not yet. They are written with PUT /api/admin/x/translations/product/{id}/{language} and read back with GET /api/admin/x/translations/product/{id}, using an admin token or a signed-in operator with the catalogue rights.

Source

Everything on this page is read from ext/translations in the GoCommerce repository, MIT licensed. When this page and the code disagree, the code is right and this page is out of date.

Try it against a store of your own

The one-command stack gives you GoCommerce’s API and admin on your own machine in minutes. Add this module to it and try it on test orders before a real one depends on it.

Chat on WhatsApp