2016-01-23 56 views
3

我在本地化的博客應用程序中遇到困難時期的數據結構。構建本地化反應/還原應用程序的商店

我的應用程序以三種語言(英語,法語和俄語)顯示嵌入圖片(一對多)的帖子。

訪問者可以選擇其語言環境。編輯人員可以用三種語言編輯他們的帖子的本地化版本。

目前,我店的結構是這樣的:

{ articles: 
    { 
    en: { 
     1: { id: 1, title: "my first title", body: "my first body", picture_ids: [1, 2, 3]}, 
     2: { id: 2, title: "my second title", body: "my second body", picture_ids: [1, 4, 5]}, 
     3: { id: 3, title: "my third title", body: "my third body", picture_ids: [6, 7, 8]}, 
     ... 
    }, 
    fr: { 
     1: { id: 1, title: "mon premier titre", body: "mon premier corps de texte", picture_ids: [1, 2, 3]}, 
     2: { id: 2, title: "mon second titre", body: "mon second corps de texte", picture_ids: [1, 4, 5]}, 
     3: { id: 3, title: "mon troisième titre", body: "mon troisième corps de texte", picture_ids: [6, 7, 8]}, 
     ... 
    } 
    }, 
    pictures: 
    { 
     en: { 
     1: { id: 1, title: "My great picture", url: "http://..."}, 
     2: { id: 2, title: "My other great picture", url: "http://..."}, 
     3: { id: 3, title: "My not so great picture", url: "http://..."}, 
     ... 
     }, 
     fr: { 
     1: { id: 1, title: "Ma superbe photo", url: "http://..."}, 
     2: { id: 2, title: "Mon autre superbe photo", url: "http://..."}, 
     3: { id: 3, title: "Ma photo pas vraiment superbe", url: "http://..."}, 
     ... 
     } 
    }, 
    editStateOfFieldsOfArticles: 
    { 
     en: { 
     1: { title: true, body: false }, 
     2: { title: false, body: true }, 
     3: { title: false, body: false }, 
     ... 
     }, 
     fr: { 
     1: { title: false, body: false }, 
     2: { title: false, body: false }, 
     3: { title: true, body: true }, 
     ... 
     } 
    } 
} 

在這個階段,我的減速器是不是太臃腫,但我有,我應該爲了進一步規範了感覺,當我預測我要添加標籤,作者和其他國際化項目與文章的關係。所以我正在考慮(一)在商店裏爲語言創建一個額外的字典,(二)「扁平化」所有其他字典,擺脫「語言環境」子代鍵,(三)添加一個language_id字段在存儲在其他字典中的每個項目中,並且(iv)將我的每個字典中的數字鍵修改爲組合鍵。這應該是這樣的:

{languages: 
    { 
    1: {id: 1, locale: "en", long: "English"}, 
    2: {id: 2, locale: "fr", long: "Français"}, 
    3: {id: 3, locale: "ru", long: "русская"} 
    } 
    articles: 
    { 
    11: {id: 1, title: "my first title", body: "my first body", picture_ids: [1, 2, 3], language_id: 1}, 
    21: {id: 2, title: "my second title", body: "my second body", picture_ids: [1, 4, 5], language_id: 1}, 
    31: {id: 3, title: "my third title", body: "my third body", picture_ids: [6, 7, 8], language_id: 1}, 
    42: {id: 1, title: "mon premier titre", body: "mon premier corps de texte", picture_ids: [1, 2, 3], language_id: 2}, 
    52: {id: 2, title: "mon second titre", body: "mon second corps de texte", picture_ids: [1, 4, 5], language_id: 2}, 
    62: {id: 3, title: "mon troisième titre", body: "mon troisième corps de texte", picture_ids: [6, 7, 8], language_id: 2}, 
    ... 
    }, 
    pictures: 
    { 
    11: {id: 1, title: "My great picture", url: "http://...", language_id: 1}, 
    21: {id: 2, title: "My other great picture", url: "http://...", language_id: 1}, 
    31: {id: 3, title: "My not so great picture", url: "http://...", language_id: 1}, 
    12: {id: 1, title: "Ma superbe photo", url: "http://...", language_id: 2}, 
    22: {id: 2, title: "Mon autre superbe photo", url: "http://...", language_id: 2}, 
    32: {id: 3, title: "Ma photo pas vraiment superbe", url: "http://...", language_id: 2}, 
    ... 
    }, 
    editStateOfFieldsOfArticles: 
    } 
    11: {title: true, body: false, language_id: 1}, 
    21: {title: false, body: true, language_id: 1}, 
    31: {title: false, body: false, language_id: 1}, 
    12: {title: false, body: false, language_id: 2}, 
    22: {title: false, body: false, language_id: 2}, 
    32: {title: true, body: true, language_id: 2}, 
    ... 
    } 
} 

「公司章程」的鍵的最後一個數字,「圖片」和「editStatesOfFieldsOfArticles」字典將對應於區域/ LANGUAGE_ID和其他數字將對應於項目ID 。

我看到,當我有適用於所有三種語言的商店修改時,通過擺脫對語言進行迭代的需要,我將受益於這樣一種更平坦的結構(例如,如果刪除了文章,文章應該在所有三種語言中刪除,我現在必須爲本地化的子詞典和所有附屬商店的子詞典(例如「editStateOfFieldsOfArticles」(我有一些其他這樣的輔助詞典)

但是,我並不完全確定,我真的會採取任何其他的好處,進一步扁平化我的哈希(和for ...循環是沒有問題,只有三種語言管理 - 此外,在我的用例,我需要刪除一篇文章和這個刪除需要反映在所有三種語言中,我仍然需要遍歷三種語言數組以刪除扁平文章詞典中的三個對應記錄。

另外,我關心的是性能問題:因爲我將把我的整個集合(文章或任何其他國際化項目)存儲在相同的扁平樹下,無論它們涉及哪種語言,我擔心訪問值相比於更結構化的樹,我可以通過在本地化分支中「sub-keying」來訪問項目 - 事實上,後端數據庫將本地化的項目存儲在不同的本地化表格中)。

我會很感激與結構Redux的商店國際化的內容或其他商店複雜的交叉關係,誰可以提供一些反饋意見或有關其自己的經驗建議或提示經驗的人: - 代碼的可讀性,尤其是在減速器和備忘錄選擇器中, - 嵌套樹木與平坦樹木的比較性能, - 進一步正常化與保持「嵌套」的「位」的總體益處。

+0

塞德里克我會給你一個upvote,但我不知道如何幫助你。祝你好運! – m0meni

+0

非常感謝@ AR7 – Cedric

回答

0

超級遲到這次派對,但如果你仍然好奇。

我認爲你的第一個實現絕對是優越的。 for循環超過3項是不是徵稅的交易,你可以(也應該)因素是出到一個輔助功能:

var forEachLanguage = function(state, dataType, callback){ 
    var languages_data = state[dataType]; 
    for(language_index in languages_data){ 
     var data = languages_data[language_index]; 
     callback(data); 
    } 
} 

//Example for deleting item 2 from articles 
forEachLanguage(state, 'articles', function(data){ 
    delete data[2]; 
}); 

此外,如果你擔心重複查找變得不必要昂貴的(儘管實際上,我沒有看到這是一個問題),您可以使用Reselect來緩存查找並製作包含語言環境的可重用查找函數。

相關問題