We’re overhauling Dgraph’s docs to make them clearer and more approachable. If
you notice any issues during this transition or have suggestions, please
let us know.
Strings and languages
Strings values in Dgraph are of UTF-8 format. Dgraph also supports values for string predicate types in multiple languages. The multi-lingual capability is particularly useful to build features, which requires you to store the same information in multiple languages. Let’s learn more about them! Let’s start with building a simple food review Graph. Here’s the Graph model.
food
node. The node representing a review comment as a review
node, and the
node representing the country of origin as a country
node.
Here’s the relationship between them:
- Every food item is connected to its reviews via the
review
edge. - Every food item is connected to its country of origin via the
origin
edge.

- Five blue food nodes.
- The green nodes represent the country of origin of these food items.
- The reviews of the food items are in pink.


Sushi
in more than one
language. We’ll be writing the review in three different languages: English,
Japanese, and Russian.
Here’s the mutation to do so.
comment
predicate
in different languages.
We used the language tags (@ru, @jp) as a suffix for the comment
predicate.
In the above mutation:
-
We used the
@ru
language tag to add the comment in Russian:"comment@ru": "очень вкусно"
. -
We used the
@jp
language tag to add the comment in Japanese:"comment@jp": "とても美味しい"
. -
The comment in
English
is untagged:"comment": "Tastes very good"
.
comment
, comment@ru
, and comment@jp
in different predicates inside the
same node.
Note: If you’re not clear about basic terminology like predicates
, do read
the first tutorial.
Let’s run the above mutation.
Go to the mutate tab, paste the mutation, and click Run.

@lang
directive to the schema.
Follow the instructions below to add the @lang
directive to the comment
predicate.
- Go to the Schema tab.
- Click on the
comment
predicate. - Tick mark the
lang
directive. - Click on the
Update
button.


Querying using language tags
Let’s obtain the review comments only forSushi
.
In the previous article, we learned about using the
eq
operator and the hash
index to query for string predicate values.
Using that knowledge, let’s first add the hash
index for the food_name
predicate.


Sushi
in Japanese.

Sushi
in Russian.

Sushi
written in any language.

Syntax | Result |
---|---|
comment | Look for an untagged string; return nothing if no untagged review exists. |
comment@. | Look for an untagged string, if not found, then return review in any language. But, this returns only a single value. |
comment@jp | Look for comment tagged @jp . If not found, the query returns nothing. |
comment@ru | Look for comment tagged @ru . If not found, the query returns nothing. |
comment@jp:. | Look for comment tagged @jp first. If not found, then find the untagged comment. If that’s not found too, return anyone comment in other languages. |
comment@jp:ru | Look for comment tagged @jp , then @ru . If neither is found, it returns nothing. |
comment@jp:ru:. | Look for comment tagged @jp , then @ru . If both not found, then find the untagged comment. If that’s not found too, return any other comment if it exists. |
comment@* | Return all the language tags, including the untagged. |
Borscht
with its review
in Russian
.

@ru
for the review written in
Russian.
Hence, if we query for all the reviews written in Russian
, the review for
Borscht
doesn’t make it to the list.
Only the review for Sushi,
written in Russian
, makes it to the list.

If you are representing the same information in different languages, don’t forget to add your language tags!
Summary
In this tutorial, we learned about using multi-language string and operations on them using the language tags. The usage of tags is not just restricted to multi-lingual strings. Language tags are just a use case of Dgraph’s capability to tag data. In the next tutorial, we’ll continue our quest into the string types in Dgraph. We’ll explore the string type indices in detail. Sounds interesting? Check out our next tutorial of the getting started series here.Need Help
- Please use discuss.hypermode.com for questions, feature requests, bugs, and discussions.