Localized section
How a Sneferu section shows English and Arabic: the two locale files, the t() calls and localized settings, from the starter theme hero.
On this page
What you'll build
The same hero in English and Arabic from two language files, with the layout flipping to right to left. The Result below is drawn by the real Sneferu renderer from the code on this page.
Requirements
- Node.js 20 or newer
- A theme made with
npx sneferu init. Every file below is part of it. - The preview running:
npx sneferu dev
Code
Concepts this teaches: Locale files, t(), Localized settings, Right to left.
These files come from the starter theme that npx sneferu init creates. The example is locales/en.json.
{
"skip_to_content": "Skip to content",
"menu": "Menu",
"search": "Search",
"search_placeholder": "What are you looking for?",
"home": "Home",
"breadcrumb": "Breadcrumb",
"sale": "Sale",
"sold_out_badge": "Sold out",
"products_label": "products",
"sort_by": "Sort by",
"filters": "Filters",
"results_for": "Results for",
"results_label": "results",
"newsletter_cta": "Subscribe",
"payment_methods": "Payment methods",
"product_info": "Product details",
"hero_title": "Everything you need, in one place",
"hero_text": "Pick what you like and check out in a minute.",
"product_note": "Order confirmation and delivery details are sent by email after you pay."
}{
"skip_to_content": "تخطي إلى المحتوى",
"menu": "القائمة",
"search": "بحث",
"search_placeholder": "بتدور على إيه؟",
"home": "الرئيسية",
"breadcrumb": "مسار الصفحة",
"sale": "خصم",
"sold_out_badge": "نفد المخزون",
"products_label": "منتج",
"sort_by": "ترتيب حسب",
"filters": "فلاتر",
"results_for": "نتائج البحث عن",
"results_label": "نتيجة",
"newsletter_cta": "اشترك",
"payment_methods": "طرق الدفع",
"product_info": "تفاصيل المنتج",
"hero_title": "كل اللي محتاجه في مكان واحد",
"hero_text": "اختار اللي يعجبك وكمّل الطلب في دقيقة.",
"product_note": "هيوصلك تأكيد الطلب وتفاصيل التسليم على الإيميل بعد الدفع."
}Download: locales/en.json, locales/ar.json in the public SDK repo at commit c96cbf2, or the whole starter theme folder.
Preview
Customize
Settings in the hk-schema of sections/hero.snf. The merchant changes them in the theme editor; the default is what a new section starts with.
| Setting | Label | Type | Default | Notes |
|---|---|---|---|---|
title | Heading | text | set per language | |
subtitle | Text | textarea | set per language | |
button_label | Button | text | {"ar":"تسوق دلوقتي","en":"Shop now"} | set per language |
button_link | Button link | link | /products | |
button_style | Button style | select | solid | one of solid, outline, ghost |
image | Image | image | ||
image_alt | Image description | text | set per language | |
align | Text alignment | select | start | one of start, center |
background | Background | color | theme:secondary | |
text_color | Text colour | color | theme:background | |
padding_y | Vertical padding | range | 88 | 0 to 200 in steps of 8 |
Text keys the code reads with t(). Change the English text in locales/en.json and the Arabic text under the same key in locales/ar.json.
| Key | English text |
|---|---|
hero_title | Everything you need, in one place |
hero_text | Pick what you like and check out in a minute. |
How it works
- This code uses the tags .
locales/en.jsonandlocales/ar.jsonhold the same keys; each value is the text for that language.- The hero prints
t('hero_title')andt('hero_text'), which look the key up in the visitor's language (see Lookups). - A setting marked
localized: truein the schema is stored once per language, so the merchant types the Arabic and English text separately. default: t('hero_title')makes the language file the fallback when the setting is empty (see Filters).- Arabic pages are laid out right to left automatically; your CSS does not need a second set of rules.
