Skip to content

Commit

Permalink
Crowdin: store language code as part of filename
Browse files Browse the repository at this point in the history
Add language tag to the filename of locally downloaded Crowdin files for
use when the file format doesn't contain project etc. metadata.

This fixes issue similar to vslavik#792 when handling native XLIFF files, which
unlike Crowdin-converted XLIFF files may also contain zh-Hans/Hant as
the language rather than zh-CN/TW expected by Crowdin API.

See vslavik#792.
  • Loading branch information
vslavik committed May 3, 2023
1 parent b05c33b commit 39a0c82
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/crowdin_gui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,8 +695,9 @@ class CrowdinOpenDialog : public wxDialog
std::replace_if(fileName.begin(), fileName.end(), boost::is_any_of("\\/:\"<>|?*"), '_');
std::replace_if(projectName.begin(), projectName.end(), boost::is_any_of("\\/:\"<>|?*"), '_');

// NB: sync this with ExtractCrowdinMetadata()
const wxString dir = GetCrowdinCacheDir() + projectName + " - " + lang.Code();
wxFileName localFileName(dir + "/Crowdin." << projectId << '.' << fileId << ' ' << fileName);
wxFileName localFileName(dir + wxString::Format("/Crowdin.%d.%d.%s %s", projectId, fileId, lang.LanguageTag(), fileName));

auto ext = localFileName.GetExt().Lower();
if (ext == "po")
Expand Down Expand Up @@ -744,12 +745,19 @@ bool ExtractCrowdinMetadata(CatalogPtr cat,
std::string *xliffRemoteFilename = nullptr)
{
auto& hdr = cat->Header();
bool crowdinSpecificLangUsed = false;

if (lang)
{
*lang = hdr.HasHeader("X-Crowdin-Language")
? Language::FromLanguageTag(hdr.GetHeader("X-Crowdin-Language").ToStdString())
: cat->GetLanguage();
if (hdr.HasHeader("X-Crowdin-Language"))
{
*lang = Language::FromLanguageTag(hdr.GetHeader("X-Crowdin-Language").ToStdString());
crowdinSpecificLangUsed = true;
}
else
{
*lang = cat->GetLanguage();
}
}

const auto xliff = std::dynamic_pointer_cast<XLIFFCatalog>(cat);
Expand Down Expand Up @@ -783,7 +791,8 @@ bool ExtractCrowdinMetadata(CatalogPtr cat,
return true;
}

static const std::wregex RE_CROWDIN_FILE(L"^Crowdin\\.([0-9]+)\\.([0-9]+) .*");
// NB: sync this with CreateLocalFilename()
static const std::wregex RE_CROWDIN_FILE(L"^Crowdin\\.([0-9]+)\\.([0-9]+)(\\.([a-zA-Z-]+))? .*");
auto name = wxFileName(cat->GetFileName()).GetName().ToStdWstring();

std::wsmatch m;
Expand All @@ -793,6 +802,8 @@ bool ExtractCrowdinMetadata(CatalogPtr cat,
*projectId = std::stoi(m.str(1));
if (fileId)
*fileId = std::stoi(m.str(2));
if (lang && !crowdinSpecificLangUsed && m[4].matched)
*lang = Language::FromLanguageTag(str::to_utf8(m[4].str()));
return true;
}

Expand Down

0 comments on commit 39a0c82

Please sign in to comment.