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(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder()
.ClassBuilder.kind(): ClassBuilderSets 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`kind()
.NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Person', 'en')
.NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Pessoa', 'pt')
.ClassBuilder.build(): ClassBuilds 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,`build();
On an existing element, reach the LanguageString directly. add sets one
language; addAll sets several at once:
const person: Classperson.NamedElement.name: LanguageStringThe name of this element, possibly in multiple languages.name.LanguageString.add(value: string, language?: string): voidAdds a text value in the given language, replacing any existing value for
that language.add('Pessoa', 'pt');
const person: Classperson.NamedElement.name: LanguageStringThe 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.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: LanguageStringThe name of this element, possibly in multiple languages.name.LanguageString.get(language?: string): string | nullReturns 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`.get(); // 'Person' (preference order)
const const portuguese: stringportuguese = const person: Classperson.NamedElement.name: LanguageStringThe name of this element, possibly in multiple languages.name.LanguageString.get(language?: string): string | nullReturns 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`.get('pt'); // 'Pessoa'
const const everyValue: [string, string][]everyValue = const person: Classperson.NamedElement.name: LanguageStringThe 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): stringReturns the name of this element in the given language, falling back to
its
id
when no suitable name is available.getNameOrId('pt'); // 'Pessoa', or the id if unnamed
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 LanguageStringA 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: stringThe 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 LanguageStringA 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: LanguageStringA free-text account of this element, possibly in multiple languages.description.LanguageString.add(value: string, language?: string): voidAdds a text value in the given language, replacing any existing value for
that language.add('A human being.', 'en');
const person: Classperson.NamedElement.description: LanguageStringA free-text account of this element, possibly in multiple languages.description.LanguageString.add(value: string, language?: string): voidAdds a text value in the given language, replacing any existing value for
that language.add('Um ser humano.', 'pt');
const person: Classperson.NamedElement.addAlternativeName(value: LanguageString): voidAdds an alternative name to this element, such as a synonym or an alias.addAlternativeName(new new LanguageString(value?: string, language?: string): LanguageStringA 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: JSONAn 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.stringify(const person: Classperson.NamedElement.name: LanguageStringThe 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.
Show the solution
Recap
- Names and descriptions are
LanguageString, not strings — one value per BCP 47 language tag. - Set values with
.name(text, lang)on a builder, oradd/addAllon the text itself. - Read with
get(lang),get()(preference order), orgetNameOrId(lang). LanguageString.defaultLanguageandlanguagePreferencecontrol the assumed language for writing and reading.