Search code examples
razorlocalizationorchardcmspoedit

POEdit - Parser for ASP.NET (.cshtml) files


I am developing an Orchard module and installed POEdit for managing the localization (.po) files. Is it possible to configure POEdit for .cshtml files? In the settings there is no parser for *.cshtml files, so I added it at the "C#" language. This is not working well.

If I have a C# block in my cshtml code, POEdit recognizes the translation item:

if (...)
{
    @T("test translation")
}

If I have a HTML block, it does not recognize the translation item:

<div title="@T("test translation inside html code")"></div>

Does anyone know how to solve this?


Solution

  • Thanks for Piotr Szmyd for your help. With the Orchard module Translation Manager I can update my .po files. But I had initial problems:

    • Update .po files with one click
    • Open .po files in POEdit (the extracted files from Translation Manager are not marked as UTF-8)

    The solution is a little VBS script, that maybe help some of you with the same problem. Just save this to updateHelper.vbs (Don't forget to set the configuration values):

    ' ####################
    ' Configuration
    Const OrchardPath       = ""      ' Path to Orchard
    Const Modules           = ""      ' e.g. Orchard.Blogs,Orchard.Pages
    Const DefaultCulture    = "en-us" ' default language
    ' ####################
    
    OrchardWeb = OrchardPath & "\src\Orchard.Web"
    OrchardBin = OrchardWeb & "\bin\"
    
    WScript.Echo "Extracting T-strings from module..."
    Set objShell = WScript.CreateObject("WScript.Shell")
    objShell.Run OrchardBin & "\Orchard extract default translation /Output:" & OrchardWeb & " /Extensions:" & Modules, 0, true
    
    WScript.Echo "Extracting archive..."
    ZipFile = OrchardWeb & "\Orchard." & DefaultCulture & ".po.zip"
    ExtractTo = OrchardWeb
    Set fso = CreateObject("Scripting.FileSystemObject")
    If NOT fso.FolderExists(ExtractTo) Then
       fso.CreateFolder(ExtractTo)
    End If
    Set objShell = CreateObject("Shell.Application")
    Set FilesInZip=objShell.NameSpace(ZipFile).items
    objShell.NameSpace(ExtractTo).CopyHere(FilesInZip)
    
    WScript.Echo "Deleting archive..."
    Set filesys = CreateObject("Scripting.FileSystemObject")
    If filesys.FileExists(ZipFile) Then
        filesys.DeleteFile ZipFile
    End If
    
    For Each orchardModule in Split(Modules, ",")
        WScript.Echo "Preparing files for module """ & orchardModule & """:"
        Set objFSO = CreateObject("Scripting.FileSystemObject")
        Set orchardModuleLanguages = objFSO.GetFolder(OrchardWeb & "\Modules\" & orchardModule & "\App_Data\Localization").Subfolders
        For Each orchardModuleLanguage in orchardModuleLanguages
            If NOT StrComp(orchardModuleLanguage.Name, DefaultCulture, vbTextCompare) = 0 Then
                WScript.Echo "Synchronizing to """ & orchardModuleLanguage.Name & """..."
                Set objShell = WScript.CreateObject("WScript.Shell")
                objShell.Run OrchardBin & "\Orchard sync translation /Input:" & OrchardWeb & " /Culture:" & orchardModuleLanguage.Name, 0, true
            End If
    
            WScript.Echo "Preparing """ & orchardModuleLanguage.Name & """ for POEdit..."
            TranslationFile = OrchardWeb & "\Modules\" & orchardModule & "\App_Data\Localization\" & orchardModuleLanguage.Name & "\orchard.module.po"
            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objFile = objFSO.OpenTextFile(TranslationFile, 1) ' Reading
            strContents = objFile.ReadAll
            objFile.Close
            Set objFile = objFSO.OpenTextFile(TranslationFile, 2) ' Writing
            objFile.WriteLine "msgid """""
            objFile.WriteLine "msgstr """""
            objFile.WriteLine """Language: " & orchardModuleLanguage.Name & "\n"""
            objFile.WriteLine """MIME-Version: 1.0\n"""
            objFile.WriteLine """Content-Type: text/plain; charset=UTF-8\n"""
            objFile.WriteLine """Content-Transfer-Encoding: 8bit\n"""
            objFile.WriteLine """X-Poedit-SourceCharset: UTF-8\n"""
            objFile.WriteLine vbCrLf & strContents
            objFile.Close
        Next
    Next
    
    WScript.Echo "Process completed!"
    

    Now create a .bat file with the following content, to get the progress output in the command shell:

    @echo off
    cscript updateHelper.vbs
    pause
    

    The script is executing the following commands:

    1. Extracting all T-strings from the default language from special modules (Translation Manager)
    2. Unzip the extracted archive to your module folder and overwrite the default language .po file
    3. Deleting archive
    4. Synchronizing to all other .po files, that are currently in your modules localization folder
    5. Setting header data to all .po files, so they are prepared for POEdit (as UTF-8)