Skip to content

Latest commit

 

History

History
102 lines (81 loc) · 4.06 KB

PX1050.md

File metadata and controls

102 lines (81 loc) · 4.06 KB

PX1050

This document describes the PX1050 diagnostic.

Summary

Code Short Description Type Code Fix
PX1050 Hardcoded strings cannot be used as parameters for localization methods, PXException and PXExceptionInfo constructors. Error Unavailable

Diagnostic Description

You should use message strings from the classes with the PX.Common.PXLocalizableAttribute attribute instead of hardcoded strings 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

Hardcoded strings cannot be localized. The diagnostic displays error for such strings including interpolated strings.

To fix the issue, you declare the string as a public const field of a class with the PX.Common.PXLocalizableAttribute attribute and replace the hardcoded string with this field.

Example of Incorrect Code

public string PXLocalizerAll()
{
    string localizedString;
    object parameter = new object();
 
    localizedString = PXLocalizer.Localize("Hardcoded String"); // The first PX1050 error is displayed for this line.
    localizedString = PXLocalizer.Localize("Hardcoded String", typeof(MyMessages).FullName); // The second PX1050 error is displayed for this line.
    localizedString = PXLocalizer.LocalizeFormat("Hardcoded String To Format {0}", parameter); // The third PX1050 error is displayed for this line.
 
    return localizedString;
}
public class LocalizationExceptions
{
    public void ExceptionsLocalization()
    {
        throw new PXArgumentException(nameof(ExceptionsLocalization), "Hardcoded String"); // The fourth PX1050 error is displayed for this line.
    }
}
 
public class DetailNonLocalizableBypassedException : PXException
{
    public object ItemToBypass { get; }
    public DetailNonLocalizableBypassedException(object itemToBypass)
        : base("Hardcoded String") // The fifth PX1050 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