Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MNT Use FQCN for PHP translations #164

Merged
merged 1 commit into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
108 changes: 76 additions & 32 deletions lang/en.yml
Original file line number Diff line number Diff line change
@@ -1,44 +1,88 @@
en:
LinkField:
ANCHOR_DESCRIPTION: 'Do not prepend "#". Anchor suggestions will be displayed once the linked page is attached.'
ANCHOR_FIELD_TITLE: 'Anchor'
SilverStripe\LinkField\Controllers\LinkFieldController:
BAD_DATA: 'Bad data'
CREATE_LINK: 'Create link'
DATA_HAS_NO_TYPEKEY: '"{class}": $data does not have a typeKey.'
EMPTY_DATA: 'Empty data'
EXTERNAL_URL_FIELD: 'External url'
EMAIL_FIELD: 'Email address'
FILE_FIELD: 'File'
INVALID_DATA_TO_ARRAY: '"{class}": Could not convert $data to an array.'
INVALID_ID': 'Invalid ID'
INVALID_JSON: '"{class}": Decoding json string failred with "{error}"'
INVALID_ID: 'Invalid ID'
INVALID_OWNER: 'Invalid Owner'
INVALID_OWNER_CLASS: 'Invalid ownerClass'
INVALID_OWNER_ID: 'Invalid ownerID'
INVALID_OWNER_RELATION: 'Invalid ownerRelation'
INVALID_TOKEN: 'Invalid CSRF token'
INVALID_TYPEKEY: 'Invalid typeKey'
INVALID_TYPENAME: '"{class}": {typename} is not a valid link type'
KEYS_ARE_NOT_ARRAY: 'If `keys` is provdied, it must be an array'
LINK_TYPE_TITLE: 'Link Type'
LINK_FIELD_TITLE: 'Title'
NO_CLASSNAME: '"{class}": All types should reference a valid classname'
NOT_REGISTERED_LINKTYPE: '"{class}": "{typekey}" is not a registered Link Type.'
NOTHING_TO_PROCESS: "Nothing to process for `{table}`\r\n"
OPEN_IN_NEW_TITLE: 'Open in new window?'
PAGE_FIELD_TITLE: 'Page'
PHONE_FIELD: 'Phone'
PROCESSING_TABLE: "Processing `{table}`\r\n"
QUERY_FIELD_TITLE: 'Query string'
QUERY_STRING_DESCRIPTION: 'Do not prepend "?". EG: "option1=value&option2=value2"'
RECORDS_INSERTED: "{numrecords} records inserted, finished processing `{table}`\r\n"
TITLE_DESCRIPTION: 'Auto generated from Page title if left blank'
UNAUTHORIZED: 'Unauthorized'
MENUTITLE: SilverStripe\LinkField\Controllers\LinkFieldController
UNAUTHORIZED: Unauthorized
UPDATE_LINK: 'Update link'
VERSIONED_STATUS_MISMATCH: 'Linkable and LinkField do not have matching Versioned applications. Make sure that both are either un-Versioned or Versioned'
SilverStripe\LinkField\Form\Traits\AllowedLinkClassesTrait:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Change references to these in the trait from __CLASS__ to be the FCQN of the trait hardcoded, because:

https://www.php.net/manual/en/language.constants.magic.php

When used in a trait method, CLASS is the name of the class the trait is used in.

In other words the call to _t() will pass in the name of the class (not the trait) - but here the key is the trait (not the class)

Copy link
Member Author

@emteknetnz emteknetnz Jan 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good spot :-)

Updated to use __TRAIT__ (have confirmed it works as expected)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OH, nice! I should have looked at that list I linked to a little longer, I didn't notice that option.

INVALID_TYPECLASS: '"{class}": {typeclass} is not a valid Link Type'
INVALID_TYPECLASS_EMPTY: '"{class}": Allowed types cannot be empty'
SilverStripe\LinkField\Models\EmailLink:
EMAIL_FIELD: 'Email address'
PLURALNAME: 'Email Links'
PLURALS:
one: 'An Email Link'
other: '{count} Email Links'
SINGULARNAME: 'Email Link'
db_Email: Email
SilverStripe\LinkField\Models\ExternalLink:
EXTERNAL_URL_FIELD: 'External url'
PLURALNAME: 'External Links'
PLURALS:
one: 'An External Link'
other: '{count} External Links'
SINGULARNAME: 'External Link'
db_ExternalUrl: 'External url'
SilverStripe\LinkField\Models\FileLink:
CANNOT_VIEW_FILE: 'Cannot view file'
FILE_DOES_NOT_EXIST: 'File does not exist'
FILE_FIELD: File
MISSING_DEFAULT_TITLE: 'File missing'
PLURALNAME: 'File Links'
PLURALS:
one: 'A File Link'
other: '{count} File Links'
SINGULARNAME: 'File Link'
has_one_File: File
SilverStripe\LinkField\Models\Link:
LINK_FIELD_TITLE: Title
LINK_FIELD_TITLE_DESCRIPTION: 'If left blank, an appropriate default title will be used on the front-end'
LINK_TYPE_TITLE: 'Link Type'
MISSING_DEFAULT_TITLE: 'No link provided'
OPEN_IN_NEW_TITLE: 'Open in new window?'
PLURALNAME: Links
PLURALS:
one: 'A Link'
other: '{count} Links'
SINGULARNAME: Link
db_OpenInNew: 'Open in new'
db_Title: Title
db_Version: Version
has_one_Owner: Owner
SilverStripe\LinkField\Models\PhoneLink:
PHONE_FIELD: Phone
PLURALNAME: 'Phone Links'
PLURALS:
one: 'A Phone Link'
other: '{count} Phone Links'
SINGULARNAME: 'Phone Link'
db_Phone: Phone
SilverStripe\LinkField\Models\SiteTreeLink:
ANCHOR_DESCRIPTION: 'Do not prepend "#". Anchor suggestions will be displayed once the linked page is attached.'
ANCHOR_FIELD_TITLE: Anchor
CANNOT_VIEW_PAGE: 'Cannot view page'
MISSING_DEFAULT_TITLE: 'Page missing'
SilverStripe\LinkField\Models\FileLink:
MISSING_DEFAULT_TITLE: 'File missing'
SilverStripe\LinkField\Form\Traits\AllowedLinkClassesTrait:
INVALID_TYPECLASS: '"{class}": {typeclass} is not a valid Link Type'
INVALID_TYPECLASS_EMPTY: '"{class}": Allowed types cannot be empty'
PAGE_DOES_NOT_EXIST: 'Page does not exist'
PAGE_FIELD_TITLE: Page
PLURALNAME: 'Site Tree Links'
PLURALS:
one: 'A Site Tree Link'
other: '{count} Site Tree Links'
QUERY_FIELD_TITLE: 'Query string'
QUERY_STRING_DESCRIPTION: 'Do not prepend "?". EG: "option1=value&option2=value2"'
SINGULARNAME: 'Site Tree Link'
TITLE_DESCRIPTION: 'Auto generated from Page title if left blank'
db_Anchor: Anchor
db_QueryString: 'Query string'
has_one_Page: Page
SilverStripe\LinkField\Tasks\LinkableMigrationTask:
VERSIONED_STATUS_MISMATCH: 'Linkable and LinkField do not have matching Versioned applications. Make sure that both are either un-Versioned or Versioned'
52 changes: 26 additions & 26 deletions src/Controllers/LinkFieldController.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,17 @@ public function linkForm(): Form
if ($id) {
$link = Link::get()->byID($id);
if (!$link) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
$operation = 'edit';
if (!$link->canView()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
} else {
$typeKey = $this->typeKeyFromRequest();
$link = LinkTypeService::create()->byKey($typeKey);
if (!$link) {
$this->jsonError(404, _t('LinkField.INVALID_TYPEKEY', 'Invalid typeKey'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_TYPEKEY', 'Invalid typeKey'));
}
$operation = 'create';
}
Expand Down Expand Up @@ -110,7 +110,7 @@ public function linkData(HTTPRequest $request): HTTPResponse
private function getLinkData(Link $link): array
{
if (!$link->canView()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
$data = $link->jsonSerialize();
$data['canDelete'] = $link->canDelete();
Expand All @@ -127,11 +127,11 @@ public function linkDelete(): HTTPResponse
{
$link = $this->linkFromRequest();
if (!$link->canDelete()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
// Check security token on destructive operation
if (!SecurityToken::inst()->checkRequest($this->getRequest())) {
$this->jsonError(400, _t('LinkField.INVALID_TOKEN', 'Invalid CSRF token'));
$this->jsonError(400, _t(__CLASS__ . '.INVALID_TOKEN', 'Invalid CSRF token'));
}
// delete() will also delete any published version immediately
$link->delete();
Expand Down Expand Up @@ -167,7 +167,7 @@ public function getLinkForm(): Form
public function save(array $data, Form $form): HTTPResponse
{
if (empty($data)) {
$this->jsonError(400, _t('LinkField.EMPTY_DATA', 'Empty data'));
$this->jsonError(400, _t(__CLASS__ . '.EMPTY_DATA', 'Empty data'));
}

/** @var Link $link */
Expand All @@ -177,28 +177,28 @@ public function save(array $data, Form $form): HTTPResponse
$operation = 'edit';
$link = Link::get()->byID($id);
if (!$link) {
$this->jsonErorr(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonErorr(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
if (!$link->canEdit()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
} else {
// Creating a new Link
$operation = 'create';
$typeKey = $this->typeKeyFromRequest();
$className = LinkTypeService::create()->byKey($typeKey) ?? '';
if (!$className) {
$this->jsonError(404, _t('LinkField.INVALID_TYPEKEY', 'Invalid typeKey'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_TYPEKEY', 'Invalid typeKey'));
}
$link = $className::create();
if (!$link->canCreate()) {
$this->jsonError(403, _t('LinkField.UNAUTHORIZED', 'Unauthorized'));
$this->jsonError(403, _t(__CLASS__ . '.UNAUTHORIZED', 'Unauthorized'));
}
}

// Ensure that ItemID url param matches the ID in the form data
if (isset($data['ID']) && ((int) $data['ID'] !== $id)) {
$this->jsonError(400, _t('LinkField.BAD_DATA', 'Bad data'));
$this->jsonError(400, _t(__CLASS__ . '.BAD_DATA', 'Bad data'));
}

// Update DataObject from form data
Expand Down Expand Up @@ -285,8 +285,8 @@ private function createLinkForm(Link $link, string $operation): Form

// Add save action button
$title = $id
? _t('LinkField.UPDATE_LINK', 'Update link')
: _t('LinkField.CREATE_LINK', 'Create link');
? _t(__CLASS__ . '.UPDATE_LINK', 'Update link')
: _t(__CLASS__ . '.CREATE_LINK', 'Create link');
$actions = FieldList::create([
FormAction::create('save', $title)
->setSchemaData(['data' => ['buttonStyle' => 'primary']]),
Expand Down Expand Up @@ -323,11 +323,11 @@ private function linkFromRequest(): Link
{
$itemID = $this->itemIDFromRequest();
if (!$itemID) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
$link = Link::get()->byID($itemID);
if (!$link) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
return $link;
}
Expand All @@ -339,11 +339,11 @@ private function linksFromRequest(): DataList
{
$itemIDs = $this->itemIDsFromRequest();
if (empty($itemIDs)) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
$links = Link::get()->byIDs($itemIDs);
if (!$links->exists()) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
return $links;
}
Expand All @@ -356,7 +356,7 @@ private function itemIDFromRequest(): int
$request = $this->getRequest();
$itemID = (string) $request->param('ItemID');
if (!ctype_digit($itemID)) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
return (int) $itemID;
}
Expand All @@ -370,13 +370,13 @@ private function itemIDsFromRequest(): array
$itemIDs = $request->getVar('itemIDs');

if (!is_array($itemIDs)) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}

$idsAsInt = [];
foreach ($itemIDs as $id) {
if (!is_int($id) && !ctype_digit($id)) {
$this->jsonError(404, _t('LinkField.INVALID_ID', 'Invalid ID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_ID', 'Invalid ID'));
}
$idsAsInt[] = (int) $id;
}
Expand All @@ -392,7 +392,7 @@ private function typeKeyFromRequest(): string
$request = $this->getRequest();
$typeKey = (string) $request->getVar('typeKey');
if (strlen($typeKey) === 0 || !preg_match('#^[a-z\-]+$#', $typeKey)) {
$this->jsonError(404, _t('LinkField.INVALID_TYPEKEY', 'Invalid typeKey'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_TYPEKEY', 'Invalid typeKey'));
}
return $typeKey;
}
Expand All @@ -406,11 +406,11 @@ private function ownerFromRequest(): DataObject
$request = $this->getRequest();
$ownerID = (int) ($request->getVar('ownerID') ?: $request->postVar('OwnerID'));
if ($ownerID === 0) {
$this->jsonError(404, _t('LinkField.INVALID_OWNER_ID', 'Invalid ownerID'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER_ID', 'Invalid ownerID'));
}
$ownerClass = $request->getVar('ownerClass') ?: $request->postVar('OwnerClass');
if (!is_a($ownerClass, DataObject::class, true)) {
$this->jsonError(404, _t('LinkField.INVALID_OWNER_CLASS', 'Invalid ownerClass'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER_CLASS', 'Invalid ownerClass'));
}
$ownerRelation = $this->ownerRelationFromRequest();
/** @var DataObject $obj */
Expand All @@ -435,7 +435,7 @@ private function ownerFromRequest(): DataObject
return $owner;
}
}
$this->jsonError(404, _t('LinkField.INVALID_OWNER', 'Invalid Owner'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER', 'Invalid Owner'));
}

/**
Expand All @@ -447,7 +447,7 @@ private function ownerRelationFromRequest(): string
$request = $this->getRequest();
$ownerRelation = $request->getVar('ownerRelation') ?: $request->postVar('OwnerRelation');
if (!$ownerRelation) {
$this->jsonError(404, _t('LinkField.INVALID_OWNER_RELATION', 'Invalid ownerRelation'));
$this->jsonError(404, _t(__CLASS__ . '.INVALID_OWNER_RELATION', 'Invalid ownerRelation'));
}
return $ownerRelation;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Form/Traits/AllowedLinkClassesTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private function validateTypes(array $types): bool
if (empty($types)) {
throw new InvalidArgumentException(
_t(
__CLASS__ . '.INVALID_TYPECLASS_EMPTY',
__TRAIT__ . '.INVALID_TYPECLASS_EMPTY',
'"{class}": Allowed types cannot be empty',
['class' => static::class],
),
Expand All @@ -60,7 +60,7 @@ private function validateTypes(array $types): bool
} else {
throw new InvalidArgumentException(
_t(
__CLASS__ . '.INVALID_TYPECLASS',
__TRAIT__ . '.INVALID_TYPECLASS',
'"{class}": {typeclass} is not a valid Link Type',
['class' => static::class, 'typeclass' => $type],
sprintf(
Expand Down
2 changes: 1 addition & 1 deletion src/Models/EmailLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getCMSFields(): FieldList
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$fields->replaceField('Email', EmailField::create(
'Email',
_t('LinkField.EMAIL_FIELD', 'Email address'),
_t(__CLASS__ . '.EMAIL_FIELD', 'Email address'),
));
});
return parent::getCMSFields();
Expand Down
2 changes: 1 addition & 1 deletion src/Models/ExternalLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$linkField = $fields->dataFieldByName('ExternalUrl');
$linkField->setTitle(_t('LinkField.EXTERNAL_URL_FIELD', 'External url'));
$linkField->setTitle(_t(__CLASS__ . '.EXTERNAL_URL_FIELD', 'External url'));
});
return parent::getCMSFields();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Models/FileLink.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function getCMSFields(): FieldList
{
$this->beforeUpdateCMSFields(function (FieldList $fields) {
$linkField = $fields->dataFieldByName('File');
$linkField->setTitle(_t('LinkField.FILE_FIELD', 'File'));
$linkField->setTitle(_t(__CLASS__ . '.FILE_FIELD', 'File'));
});
return parent::getCMSFields();
}
Expand Down
Loading