如何在Cloud Firestore中正确组织本地化的内容? [英] How to properly structure localized content in Cloud Firestore?
问题描述
我正在考虑从实时ratabase迁移到Cloud Firestore,并且想知道如何为本地化内容正确设置数据结构.
I'm thinking about migrating to Cloud Firestore from realtime ratabase and wondering how I can properly setup the datastructure for my localized content.
这是RTDB中的结构:
This is how it was structured in RTDB:
articles
en-US
article_01
de-DE
article_01
在Firestore中,我会创建类似的东西吗?
In Firestore, would I create something like this?
Collection: articles
document: article_01
然后将 article_01
的所有数据嵌套在两个名为"en-US"和"de-DE"的地图中?
And then nest all the data of article_01
in two maps called 'en-US' and 'de-DE'?
不确定在Firestore中是否没有更好的方法来组织本地化数据?
Not sure if there's not a better way to structure localized data in Firestore?
感谢您的帮助:)
推荐答案
在Cloud Firestore中有一种简单的结构化此类数据的方法.因此,可能的模式可能是:
There is a simple way for structuring such data in Cloud Firestore. So a possible schema might be:
Firestore-root
|
--- articles (collection)
|
--- articleId (document)
| |
| --- language: "en-US"
| |
| --- //other article properties
|
--- articleId (document)
|
--- language: "de-DE"
|
--- //other article properties
使用此数据库架构,在Android中,您可以简单地:
Using this database schema, in Android, you can simply:
-
仅使用
CollectionReference
获取所有文章,无论使用哪种语言:
Get all articles regardless of the language using just a
CollectionReference
:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference articlesRef = rootRef.collection("articles");
articlesRef.get().addOnCompleteListener(/* ... */);
使用 Query
获取所有与一种语言对应的文章:
Get all articles that correpond to a single language using a Query
:
FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
Query query = rootRef.collection("articles").whereEqualTo("language", "en-US");
query.get().addOnCompleteListener(/* ... */);
这篇关于如何在Cloud Firestore中正确组织本地化的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!