How to translate a rdlc file using resx files?

Asked

Viewed 52 times

2

I am trying through a rdlc files to configure to fetch translations from a Resource . resx file

 public static Stream TranslateReport(Stream reportStream, string partyId)
{
    var reportXml = XDocument.Load(reportStream);

    foreach (var element in reportXml.Descendants(XName.Get("Value", @"http://schemas.microsoft.com/sqlserver/reporting/2008/01/reportdefinition")))
    {
        var attribute = element.Attribute(XName.Get("LocID", @"http://schemas.microsoft.com/SQLServer/reporting/reportdesigner"));
        var rm = new ResourceManager($"TSG.Reporting.Resources._{partyId}.TransportAuthorizationRequestReport", Assembly.GetExecutingAssembly());
        var translatedValue = rm.GetString(attribute.Value);
        element.Value = string.IsNullOrEmpty(translatedValue) ? element.Value : translatedValue;
    }

    var ms = new MemoryStream();
    reportXml.Save(ms, SaveOptions.OmitDuplicateNamespaces);
    ms.Position = 0;

    return ms;
}

I found solutions that spoke of use http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition but this source is no longer working

It is also described here though in English https://social.msdn.microsoft.com/Forums/vstudio/en-US/ddff5e7f-2d77-4b64-b66f-9ef3aed041c7/invalid-report-definition-exception-when-creating-report-using-visual-studio-2017?forum=winforms

  • just create the resx file, and use the key names in the Valuelocid properties so that when generating the reports the texts are replaced

  • @Ricardopunctual I did it and it didn’t work

  • but you didn’t implement the code? you need to use the method TranslateReport for the report to be produced using the Resources file, because this process is not automatic

  • @Ricardopunctual yes is implemented but something is wrong in this implementation

  • is a little different than I remember... we’ve done this but it’s more like this example, see if it helps you: localization-multi-language-of-a-rdlc-report-with-microsoft-reportviewer

  • https://stackoverflow.com/questions/38902037/ssrs-report-definition-is-newer-than-server

  • 1

    Thanks @Ricardopunctual I developed the solution for this problem :)

Show 2 more comments

1 answer

0


This was my solution to translate the report

public static Stream TranslateReport(Stream reportStream, string Code)
    {
        var reportXml = XElement.Load(reportStream);
        reportXml = Translate(reportXml, Code);
        var ms = new MemoryStream();
        reportXml.Save(ms, SaveOptions.OmitDuplicateNamespaces);
        ms.Position = 0;
        return ms;
    }





public static XElement Translate(XElement reportStream, string Code)
        {
                XmlDocument xmld = new XmlDocument();
                xmld.Load(reportStream.CreateReader());
                XmlNamespaceManager manager = new XmlNamespaceManager(xmld.NameTable);
                manager.AddNamespace("x", "http://schemas.microsoft.com/sqlserver/reporting/2016/01/reportdefinition");
                manager.AddNamespace("rd", "http://schemas.microsoft.com/SQLServer/reporting/reportdesigner");
                XmlNode root = xmld.DocumentElement;
                XmlNodeList nodeList;
                nodeList = root.SelectNodes("//x:Value[@rd:LocID]", manager);
                var rm = new ResourceManager($"My.Resources._{Code}.FileResource", Assembly.GetExecutingAssembly());

                foreach (XmlNode node in nodeList)
                {
                    foreach (XmlAttribute attribute in node.Attributes)
                    {
                        if (attribute.Name.Split(':')[1] == "LocID")
                        {
                          if (!string.IsNullOrEmpty(attribute.Value))
                          {
                             var translatedValue = rm.GetString(attribute.Value);
                             node.InnerText = translatedValue;
                          }
                        }
                    }
                }
            return XDocument.Load(xmld.CreateNavigator().ReadSubtree()).Root;
        }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.