GithubHelp home page GithubHelp logo

Comments (4)

adams85 avatar adams85 commented on July 20, 2024 1

I leave this issue open so that I Can Come back here with samples of my monkey patch in case it is useful to someone else.

Good idea! 👍

Unfortunately sending a PR would be way over my head

Once you have something working, I'll consider including it in the project (probably as an optional feature). If so, I'll handle the git-related stuff as well.

I am not familiar at all with git's tooling and workflow and am overwhelmed with work so I wouldn't be able to add learning git to my plate

I recommend learning it eventually because of its ubiquity (and because it's a truly capable tool). The basics are not complicated at all once you get the hang of its distributed nature (i.e. the concept of remote repos and local working copies). But interestingly, you don't need a remote server for using it - which was one of the biggest selling points for me, coming from SVN...

My daily driver is using Mercurial using the GUI TortoiseHG.

There's Tortoise software for Git as well! ;)

from aspnetskeleton2.

adams85 avatar adams85 commented on July 20, 2024

Hi there!

I'm glad you find the project useful (despite the lack of documentation)!

Technically, the source code surrounding the call in your example is not a message context in PO terms but a comment (a previous value comment, more precisely). This is an important distinction because comments have no effect on translation lookup (they're just extra information stored along with the entry for translators, tools, etc.) but message context is a part of the PO entry key, that is, its value is used in the equality check when looking up the translation for a specific key. In other words, message context is for distinguishing PO entries with identical message IDs.

So, if your goal is to just add some extra information as some kind of comment, that's relatively easy to implement: you'll just need to modify the extractor tool. Start with looking around in this class. Then you need to figure out how to get the surrounding code from Roslyn. Once you get that done you can include it in the returned LocalizableTextInfo objects and, as a final step, add it as comments to the generated entries by modifying the PO catalog building logic around here.

However, if you want to add surrounding code as actual message contexts, I can't see an easy way to achieve that. The first part of it would be the same as I described above, except for adding message contexts instead of comments to the catalog entries. The second part would be inventing some magic on the lookup side, which is able to figure out the source code context of the executing code at run-time. And that looks like a pretty tough nut to crack, TBH. It may be doable using source generators though.

One more thing worth mentioning if you really want to go down this path: the implementation used by this template project allows you to pass message context to the lookup logic like this: T["Error", TextContext.From("Pages")], where TextContext.From must be the last argument.

Hope this helps.

from aspnetskeleton2.

nkosi23 avatar nkosi23 commented on July 20, 2024

Thanks a lot for these insights! I was indeed confused, what I am really after are comments (so that they show up in the comments section of the POedit application). Your pointers are exactly what I needed to get the ball rolling, thank you so much for that 😃

I leave this issue open so that I Can Come back here with samples of my monkey patch in case it is useful to someone else.

Unfortunately sending a PR would be way over my head, I am not familiar at all with git's tooling and workflow and am overwhelmed with work so I wouldn't be able to add learning git to my plate I'm afraid. All I'm able to do is forking and downloading zips from Github as far as git is concerned 😆 My daily driver is using Mercurial using the GUI TortoiseHG.

from aspnetskeleton2.

nkosi23 avatar nkosi23 commented on July 20, 2024

Okay I got it working! 😃 Here is what I've done:

I have added the following methods to CSharpTextExtractor.cs as well as an additional field:

private List<string> _linesInFile = new List<string>();

protected void SetSourceCodeLines(string content)
{
    var lines = content.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries) ?? new string[] { };
    _linesInFile = new List<string>(lines);
}

protected virtual string GetCode(string content, CancellationToken cancellationToken)
{
    SetSourceCodeLines(content);
    return content;
}

private string GetSurroundingSourceLines(int lineNumber)
{
    //TODO: Add to CSharpTextExtractorSettings
    int numberOfLinesBefore = 2;
    int numberOfLinesAfter = 2;

    var startLine = lineNumber - numberOfLinesBefore;
    startLine = startLine < 0 ? 0 : startLine;

    var endLine = lineNumber + numberOfLinesAfter;
    endLine = endLine > _linesInFile.Count ? _linesInFile.Count : endLine;

    if (startLine == endLine)
        return _linesInFile.ElementAt(lineNumber - 1);

    var sb = new StringBuilder();
    for (int i = startLine - 1; i < endLine; i++)
    {
        //The comment sign is already added to the first line
        if (i == startLine - 1)
        {
            sb.AppendLine($"{_linesInFile.ElementAt(i)}");
            continue;
        }

        sb.AppendLine($"#. {_linesInFile.ElementAt(i)}");
    }

    return sb.ToString();
}

In the same file I have also modified the methods AnalyzeDecoratedDeclaration and AnalyzeElementAccessExpressions to set the Comment property of LocalizableTextInfo:

return new LocalizableTextInfo
{
    LineNumber = lineNumber,
    Id = id,
    PluralId = GetPluralId(argList),
    ContextId = GetContextId(argList),
    Comment = GetSurroundingSourceLines(lineNumber) //This is what I have added
};

We must also modify CSharpRazorTextExtractor to ensure it makes the call to SetSourceCodeLines:

protected override string GetCode(string content, CancellationToken cancellationToken)
{
    SetSourceCodeLines(content); //This is the line I have added

    var sourceDocument = RazorSourceDocument.Create(content, "_");
    var codeDocument = _projectEngine.Process(sourceDocument, fileKind: null, importSources: null, tagHelpers: null);
    var parsedDocument = codeDocument.GetCSharpDocument();
    var errorDiagnostic = parsedDocument.Diagnostics.OfType<RazorDiagnostic>().FirstOrDefault(d => d.Severity == RazorDiagnosticSeverity.Error);
    if (errorDiagnostic != null)
        throw new ArgumentException($"Razor code has errors: {errorDiagnostic}.", nameof(content));

    return parsedDocument.GeneratedCode;
}

Now POEdit displays the source code lines like I wanted 😃

image

One remark is that the field I have added makes the extractor even less thread safe than before.

from aspnetskeleton2.

Related Issues (4)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.