Skip to content

Commit

Permalink
Support Angular's null translations in JSON
Browse files Browse the repository at this point in the history
Files generated by ngx-translate can contain null for the translation,
meaning "use default language".

Poedit doesn't support editing this meaning, but it should be able to
read such files and fill in actual translation.
  • Loading branch information
vslavik committed May 28, 2024
1 parent 5296735 commit 353a876
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
20 changes: 14 additions & 6 deletions src/catalog_json.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,17 @@ class GenericJSONItem : public JSONCatalogItem
GenericJSONItem(int id, const std::string& key, json_t& node) : JSONCatalogItem(id, node)
{
m_string = str::to_wx(key);
auto trans = str::to_wx(node.get<std::string>());
m_translations.push_back(trans);
m_isTranslated = !trans.empty();
if (node.is_null())
{
m_translations.push_back(wxString());
m_isTranslated = false;
}
else
{
auto trans = str::to_wx(node.get<std::string>());
m_translations.push_back(trans);
m_isTranslated = !trans.empty();
}
}

void UpdateInternalRepresentation() override
Expand Down Expand Up @@ -235,9 +243,9 @@ class GenericJSONCatalog : public JSONCatalog
for (auto& el : node.items())
{
auto& val = el.value();
if (val.is_string())
if (val.is_string() || val.is_null())
{
m_items.push_back(std::make_shared<GenericJSONItem>(++id, prefix + el.key(), el.value()));
m_items.push_back(std::make_shared<GenericJSONItem>(++id, prefix + el.key(), val));
}
else if (val.is_object())
{
Expand Down Expand Up @@ -337,7 +345,7 @@ class FlutterCatalog : public JSONCatalog
{
auto mi = metadata.find(key);
auto meta = (mi != metadata.end()) ? mi->second : nullptr;
m_items.push_back(std::make_shared<FlutterItem>(++id, prefix + el.key(), el.value(), meta));
m_items.push_back(std::make_shared<FlutterItem>(++id, prefix + el.key(), val, meta));
}
else if (val.is_object())
{
Expand Down
6 changes: 4 additions & 2 deletions tests/json/de.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{
"demo.greeting": "Hallo {{name}}!",
"demo.text": "Dies ist eine einfache Applikation um die Funktionen von ngx-translate zu demonstrieren.",
"demo.text": "Dies ist eine einfache Applikation um die Funktionen von ngx-translate zu demonstrieren.",
"demo.text-in-code": "Verwendung von Übersetzungen im Sourcecode",
"demo.title": "Übersetzungs-Demo"
"demo.title": "Übersetzungs-Demo",
"empty_string_translation": "",
"null_translation": null
}
4 changes: 3 additions & 1 deletion tests/json/en_dos.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
"demo.greeting": "Hello {{name}}!",
"demo.text": "This is a simple demonstration app for ngx-translate",
"demo.text-in-code": "Translation used from code",
"demo.title": "Translation demo"
"demo.title": "Translation demo",
"empty_string_translation": "",
"null_translation": null
}

0 comments on commit 353a876

Please sign in to comment.