Total.js Platform
Total.js Platform

Total.js Platform news and tutorials

Building SaaS Website #08: Localization (Multi-Language Website)

Building SaaS Website #08: Localization (Multi-Language Website)

In this blog post, we explore an essential feature of modern web applications: localization. Creating a multilingual website allows you to reach a global audience and deliver a tailored user experience based on language preferences. Total.js provides a robust localization mechanism, making it easier to build and manage multilingual applications.

Here’s what we’ll cover in this post:

  • The notion of localization.
  • How localization works in client-server relations.
  • Initializing localization in Total.js to get or set client language preferences.
  • Translation string syntax.
  • File types that support localization.
  • Total.js language resource files and translations.
  • Free machine translation using Total.js’s online translator.

Let’s get started!



The Notion of Localization

Localization refers to the process of adapting a website or application to support multiple languages and regional preferences. It goes beyond translating text by ensuring cultural and contextual appropriateness. For web developers, this means creating tools to:

  • Detect user language preferences.
  • Serve translated content dynamically.
  • Support various file formats and ensure consistent functionality across languages.

A localized website provides a seamless and inclusive experience for users worldwide, enhancing accessibility and user satisfaction.



How Localization Works in Client-Server Relations

Localization in web development typically involves communication between the client (browser) and server. Here’s how it works:

  1. Language Detection: The server identifies the user’s language preference using methods such as:
    • Query parameters (e.g., ?language=en).
    • Browser settings (Accept-Language header).
    • User account preferences (if logged in).
  1. Localized Content Delivery: Based on the detected language, the server fetches and serves the appropriate language resources.
  1. Dynamic Rendering: Total.js localizes views and static .html files at compile time. Translated views are stored in memory, ensuring efficient delivery.

This seamless exchange ensures users receive content in their preferred language without extra effort on their part.



Initializing Localization in Total.js

To set up localization in a Total.js project, you need to define how the server determines the client’s language. This is done by registering a localization delegate that processes incoming requests.

Example:

/definitions/localization.js

This function retrieves the language preference from the query parameter language. If no preference is provided, it defaults to English (en).



Translation String Syntax

In Total.js, text intended for translation is wrapped in the @(text) markup. During localization, this text is replaced with its corresponding translation from the resource files.

Example in an HTML file:

If the localized text contains special characters like " or ', you must HTML-encode them to ensure proper usage in JavaScript or HTML.



File Types Supporting Localization

Localization in Total.js applies to the following file types:

  1. Static .html files: Translated at compile time.
  2. Views: Dynamic templates processed by the framework.

Each translated file is stored in memory for fast and efficient retrieval.



Total.js Language Resource Files and Translations

Total.js uses resource files (.resource) to store translations. These files are created by scanning the source code for text wrapped in @() and generating unique keys for each string.

Example with Updated HTML Code

/views/layout.html

Free Machine Translation with Total.js Translator

Total.js offers an online translator tool for free machine translation of resource files. This tool simplifies the translation process and ensures consistency across languages.

Try it here: Total.js Translator

Features:

  • Upload resource files for translation.
  • Download translated files in the same format.

By implementing localization in Total.js, you can create a truly global website that adapts to user preferences. This improves user engagement and accessibility, ensuring your application stands out in a competitive market.

Stay tuned for the next episode of our TotalGPT SaaS Website series!