Translating variables

I have a Report entity that contains a field named priority with integer as the type. This integer maps to different priorities based on the priority value. These are represented through a class array constant inside the entity.

Inside this entity, I have a method to output the Report’s priority in text/string. Sometimes I want to output the string-version of the priority to the URL as a means to filter the types of priorities. For this I want the English (untranslated) version, but when I want to display the priority in the HTML, I want the translated version.

A cleaned-up version of this entity is here:

/**
 * Report Entity
 *
 * @property int $priority
 * @property int $status
 */
class Report extends Entity
{
    /**
     * Report low priority
     */
    public const LOW = 0;

    /**
     * Report medium priority
     */
    public const MEDIUM = 1;

    /**
     * Report high priority
     */
    public const HIGH = 2;

    /**
     * Report priorities
     */
    public const PRIORITY = [
        self::LOW => 'low',
        self::MEDIUM => 'medium',
        self::HIGH => 'high',
    ];

    /**
     * Output the report's priority in text
     *
     * @param string $default What value to use when the offset does not exist.
     * @return string
     */
    public function priorityInText(string $default = ''): string
    {
        return self::PRIORITY[$this->priority] ?? $default;
    }
}

For now, I’ve encapsulated the $report->priorityInText() with the translation function, like this __($report->priorityInText()).

Sure, this works, but whenever I want to extract translation strings using bin/cake i18n e, I get marker errors, as the extractor cannot know what the string I want to translate is. I have to manually add these values to the locale files. Whenever I want to edit my translations through the software PoEdit, the software removes these translations as they are “not used” according to the extractor.

What method would you use to work around this issue?

Adding another constant inside the Report entity, with the translated strings won’t work, as class constants can’t use expressions (i.e. translate strings). I can work around this with global constants (using define()), but I want to avoid them.

An extremely hacky version would be to put this in a file somewhere that the i18n process looks at:

if (false) {
    __('low') . __('medium') . __('high');
}

That would at least make sure that your strings never get removed from your pot / po files.

I have a less hacky way of doing this in my code, but it’s still pretty hacky, more complicated and error-prone than this, and doesn’t look like it would play well with the const structure you have set up (which I think is good) so I’ll not give any further details on them here.