Skip to main content
Version: Next

Language strings

Ontologies are often shared across languages, so in ontouml-js a name isn't a plain string — it's a LanguageString: a set of values, each tagged with an IETF BCP 47 language tag ('en', 'pt', 'de', …). The same class can be Person in English and Pessoa in Portuguese at once.

Names, descriptions, alternative names, and editorial notes are all LanguageString, so everything you learn here applies to each of them.

Naming in several languages​

At creation time, chain .name() once per language — it adds, rather than replaces:

const const person: Classperson = const model: Packagemodel
  .Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
()
.ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
()
.NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Person', 'en')
.NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Pessoa', 'pt')
.ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();

On an existing element, reach the LanguageString directly. add sets one language; addAll sets several at once:

const person: Classperson.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
.LanguageString.add(value: string, language?: string): void
Adds a text value in the given language, replacing any existing value for that language.
@paramvalue - the text to add.@paramlanguage - language tag of `value`; defaults to defaultLanguage.@throwsan error if `language` is not a valid language tag.
add
('Pessoa', 'pt');
const person: Classperson.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
.
LanguageString.addAll(obj: {
    [key: string]: string;
}): void
Adds every entry of an object mapping language tags to text values, replacing existing values for those languages.
@paramobj - object whose keys are language tags and whose values are the corresponding texts (e.g., `{ en: 'Person', pt: 'Pessoa' }`).
addAll
({ de: stringde: 'Person', fr: stringfr: 'Personne' });

Reading a value back​

get() returns the value for a language. With no argument it follows the language preference (English by default); with a tag it returns that exact language. getNameOrId() is a safe shortcut that falls back to the element's id when it has no name.

const const preferred: stringpreferred = const person: Classperson.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
.LanguageString.get(language?: string): string | null
Returns the text value for the given language. If no language is provided, returns the value of the first language in languagePreference for which a value exists. If no such value exists, returns the first value it finds. If no values are defined, returns `null`.
@paramlanguage - language tag of the desired value (e.g., `"en"`).@throwsan error if `language` is provided but invalid or has no value.
get
(); // 'Person' (preference order)
const const portuguese: stringportuguese = const person: Classperson.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
.LanguageString.get(language?: string): string | null
Returns the text value for the given language. If no language is provided, returns the value of the first language in languagePreference for which a value exists. If no such value exists, returns the first value it finds. If no values are defined, returns `null`.
@paramlanguage - language tag of the desired value (e.g., `"en"`).@throwsan error if `language` is provided but invalid or has no value.
get
('pt'); // 'Pessoa'
const const everyValue: [string, string][]everyValue = const person: Classperson.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
.LanguageString.entries(): [string, string][]
Returns all values of this text as `[language, value]` pairs.
entries
(); // [['en','Person'], ['pt','Pessoa']]
const const label: stringlabel = const person: Classperson.NamedElement.getNameOrId(language?: string): string
Returns the name of this element in the given language, falling back to its id when no suitable name is available.
@paramlanguage - language tag of the desired name (e.g., `"en"`); when omitted, the name is selected according to LanguageString.languagePreference.
getNameOrId
('pt'); // 'Pessoa', or the id if unnamed
warning

get('xx') throws if there's no value for that exact tag. Use get() with no argument (preference-based, may return null) or getNameOrId() when you can't be sure a language is present.

Defaults and preference​

Two static fields on LanguageString tune the behaviour globally:

// Language assumed when you add a value without a tag (default: 'en')
class LanguageString
A text value expressed in one or more languages. Each value is indexed by an IETF BCP 47 language tag (e.g., `"en"`, `"pt"`), allowing elements to be named and described multilingually. For example, a class may be named `"Person"` in English and `"Pessoa"` in Portuguese. The static fields defaultLanguage and languagePreference control, respectively, the language assumed when adding a value without a tag and the order in which languages are tried when retrieving a value without specifying one.
LanguageString
.LanguageString.defaultLanguage: string
The language tag assumed when a value is added without one; `"en"` by default.
defaultLanguage
= 'pt';
// Order languages are tried when reading without a tag (default: ['en']) class LanguageString
A text value expressed in one or more languages. Each value is indexed by an IETF BCP 47 language tag (e.g., `"en"`, `"pt"`), allowing elements to be named and described multilingually. For example, a class may be named `"Person"` in English and `"Pessoa"` in Portuguese. The static fields defaultLanguage and languagePreference control, respectively, the language assumed when adding a value without a tag and the order in which languages are tried when retrieving a value without specifying one.
LanguageString
.LanguageString.languagePreference: string[]
The ordered list of language tags tried when retrieving a value without specifying a language; `['en']` by default.
languagePreference
= ['pt', 'en'];

So builder.name('Pessoa') with no tag stores it under defaultLanguage, and text.get() with no tag walks languagePreference until it finds a value.

It's not just names​

Descriptions, alternative names, and editorial notes are multilingual too:

const person: Classperson.NamedElement.description: LanguageString
A free-text account of this element, possibly in multiple languages.
description
.LanguageString.add(value: string, language?: string): void
Adds a text value in the given language, replacing any existing value for that language.
@paramvalue - the text to add.@paramlanguage - language tag of `value`; defaults to defaultLanguage.@throwsan error if `language` is not a valid language tag.
add
('A human being.', 'en');
const person: Classperson.NamedElement.description: LanguageString
A free-text account of this element, possibly in multiple languages.
description
.LanguageString.add(value: string, language?: string): void
Adds a text value in the given language, replacing any existing value for that language.
@paramvalue - the text to add.@paramlanguage - language tag of `value`; defaults to defaultLanguage.@throwsan error if `language` is not a valid language tag.
add
('Um ser humano.', 'pt');
const person: Classperson.NamedElement.addAlternativeName(value: LanguageString): void
Adds an alternative name to this element, such as a synonym or an alias.
@throwsan error if the value is null or undefined.
addAlternativeName
(new new LanguageString(value?: string, language?: string): LanguageString
A text value expressed in one or more languages. Each value is indexed by an IETF BCP 47 language tag (e.g., `"en"`, `"pt"`), allowing elements to be named and described multilingually. For example, a class may be named `"Person"` in English and `"Pessoa"` in Portuguese. The static fields defaultLanguage and languagePreference control, respectively, the language assumed when adding a value without a tag and the order in which languages are tried when retrieving a value without specifying one.
LanguageString
('Human', 'en'));

In the serialized JSON​

A LanguageString serializes to an object keyed by language tag — the shape prescribed by the OntoUML JSON Schema:

const const json: stringjson = var JSON: JSON
An intrinsic object that provides functions to convert JavaScript values to and from the JavaScript Object Notation (JSON) format.
JSON
.JSON.stringify(value: any, replacer?: (this: any, key: string, value: any) => any, space?: string | number): string (+1 overload)
Converts a JavaScript value to a JavaScript Object Notation (JSON) string.
@paramvalue A JavaScript value, usually an object or array, to be converted.@paramreplacer A function that transforms the results.@paramspace Adds indentation, white space, and line break characters to the return-value JSON text to make it easier to read.@throws{TypeError} If a circular reference or a BigInt value is found.
stringify
(const person: Classperson.NamedElement.name: LanguageString
The name of this element, possibly in multiple languages.
name
);
// {"en":"Person","pt":"Pessoa"}

Now you try it​

Create a class named in three languages, then print the name in each — and the full list of entries.

Loading playground…
Show the solution
Loading playground…

Recap​

  • Names and descriptions are LanguageString, not strings — one value per BCP 47 language tag.
  • Set values with .name(text, lang) on a builder, or add / addAll on the text itself.
  • Read with get(lang), get() (preference order), or getNameOrId(lang).
  • LanguageString.defaultLanguage and languagePreference control the assumed language for writing and reading.