Skip to content

Latest commit

 

History

History
106 lines (86 loc) · 4.23 KB

PX1051.md

File metadata and controls

106 lines (86 loc) · 4.23 KB

PX1051

This document describes the PX1051 diagnostic.

Summary

Code Short Description Type Code Fix
PX1051 The strings defined in a class without the PXLocalizable attribute cannot be used as parameters for localization methods, PXException and PXExceptionInfo constructors. Error Unavailable

Diagnostic Description

You should use a message string from a class with the PX.Common.PXLocalizableAttribute attribute in the following cases:

  • In the message parameter of the static localization methods of the PX.Data.PXLocalizer class
  • In the message parameter of the static localization methods of the PX.Data.PXMessages class
  • In the message parameter of the constructor of the PXException class and all derived classes
  • In the message parameter of the constructor of the PX.Objects.Common.Exceptions.PXExceptionInfo class and all derived classes

The strings from the classes without the PXLocalizable attribute cannot be localized.

To fix the issue, you add the PXLocalizable attribute to the class with the string constants.

Example of Incorrect Code

public static class Messages
{
    public const string SpecialText = "Hardcoded String";
    public const string SpecialTextToFormat = "Hardcoded String To Format {0}";
}

public string PXLocalizerAll()
{
    string localizedString;
    object parameter = new object();
 
    localizedString = PXLocalizer.Localize(Messages.SpecialText); // The first PX1051 error is displayed for this line.
    localizedString = PXLocalizer.Localize(Messages.SpecialText, typeof(Messages).FullName); // The second PX1051 error is displayed for this line.
    localizedString = PXLocalizer.LocalizeFormat(Messages.SpecialTextToFormat, parameter); // The third PX1051 error is displayed for this line.
 
    return localizedString;
}
public class LocalizationExceptions
{
    public void ExceptionsLocalization()
    {
        throw new PXArgumentException(nameof(ExceptionsLocalization), Messages.SpecialText); // The fourth PX1051 error is displayed for this line.
    }
}
 
public class DetailNonLocalizableBypassedException : PXException
{
    public object ItemToBypass { get; }
    public DetailNonLocalizableBypassedException(object itemToBypass)
        : base(Messages.SpecialText) // The fifth PX1051 error is displayed for this line.
    {
        ItemToBypass = itemToBypass;
    }
}

Example of Possible Code Fix

[PXLocalizable]
public static class Messages
{
    public const string SpecialText = "Hardcoded String";
    public const string SpecialTextToFormat = "Hardcoded String To Format {0}";
}

public string PXLocalizerAll()
{
    string localizedString;
    object parameter = new object();
 
    localizedString = PXLocalizer.Localize(Messages.SpecialText);
    localizedString = PXLocalizer.Localize(Messages.SpecialText, typeof(MyMessages).FullName);
    localizedString = PXLocalizer.LocalizeFormat(Messages.SpecialTextToFormat, parameter);
 
    return localizedString;
}
public class LocalizationExceptions
{
    public void ExceptionsLocalization()
    {
        throw new PXArgumentException(nameof(ExceptionsLocalization), Messages.SpecialText);
    }
}
 
public class DetailNonLocalizableBypassedException : PXException
{
    public object ItemToBypass { get; }
    public DetailNonLocalizableBypassedException(object itemToBypass)
        : base(Messages.SpecialText)
    {
        ItemToBypass = itemToBypass;
    }
}

Related Articles