Skip to content

Commit 7647241

Browse files
committed
Some better activity translations
1 parent cf01490 commit 7647241

File tree

5 files changed

+34
-13
lines changed

5 files changed

+34
-13
lines changed

app/Http/Controllers/Base/LocaleController.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,16 @@ protected function i18n(array $data): array
5757
if (is_array($value)) {
5858
$data[$key] = $this->i18n($value);
5959
} else {
60-
$data[$key] = preg_replace('/:([\w-]+)(\W?|$)/m', '{{$1}}$2', $value);
60+
// Find a Laravel style translation replacement in the string and replace it with
61+
// one that the front-end is able to use. This won't always be present, especially
62+
// for complex strings or things where we'd never have a backend component anyways.
63+
//
64+
// For example:
65+
// "Hello :name, the :notifications.0.title notification needs :count actions :foo.0.bar."
66+
//
67+
// Becomes:
68+
// "Hello {{name}}, the {{notifications.0.title}} notification needs {{count}} actions {{foo.0.bar}}."
69+
$data[$key] = preg_replace('/:([\w.-]+\w)([^\w:]?|$)/m', '{{$1}}$2', $value);
6170
}
6271
}
6372

app/Transformers/Api/Client/ActivityLogTransformer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ protected function hasAdditionalMetadata(ActivityLog $model): bool
8181
}
8282

8383
$str = trans('activity.' . str_replace(':', '.', $model->event));
84-
preg_match_all('/:(?<key>[\w-]+)(?:\W?|$)/', $str, $matches);
84+
preg_match_all('/:(?<key>[\w.-]+\w)(?:[^\w:]?|$)/', $str, $matches);
8585

8686
$exclude = array_merge($matches['key'], ['ip', 'useragent']);
8787
foreach ($model->properties->keys() as $key) {

resources/lang/en/activity.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,18 @@
5454
'delete' => 'Deleted database :name',
5555
],
5656
'file' => [
57-
'compress_one' => 'Compressed :directory/:file',
57+
'compress_one' => 'Compressed :directory:file',
5858
'compress_other' => 'Compressed :count files in :directory',
5959
'read' => 'Viewed the contents of :file',
6060
'copy' => 'Created a copy of :file',
6161
'create-directory' => 'Created a new directory :name in :directory',
6262
'decompress' => 'Decompressed :files in :directory',
63-
'delete_one' => 'Deleted :directory/:files',
63+
'delete_one' => 'Deleted :directory:files',
6464
'delete_other' => 'Deleted :count files in :directory',
6565
'download' => 'Downloaded :file',
6666
'pull' => 'Downloaded a remote file from :url to :directory',
67-
'rename' => 'Renamed files in :directory',
67+
'rename_one' => 'Renamed :directory:files.0.from to :directory:files.0.to',
68+
'rename_other' => 'Renamed :count files in :directory',
6869
'write' => 'Wrote new content to :file',
6970
'upload' => 'Began a file upload',
7071
],
@@ -75,7 +76,7 @@
7576
'delete' => 'Deleted the :allocation allocation',
7677
],
7778
'schedule' => [
78-
'store' => 'Created the :name schedule',
79+
'create' => 'Created the :name schedule',
7980
'update' => 'Updated the :name schedule',
8081
'execute' => 'Manually executed the :name schedule',
8182
'delete' => 'Deleted the :name schedule',

resources/scripts/components/elements/activity/ActivityLogEntry.tsx

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,28 @@ import ActivityLogMetaButton from '@/components/elements/activity/ActivityLogMet
1010
import { TerminalIcon } from '@heroicons/react/solid';
1111
import classNames from 'classnames';
1212
import style from './style.module.css';
13+
import { isObject } from '@/helpers';
1314

1415
interface Props {
1516
activity: ActivityLog;
1617
children?: React.ReactNode;
1718
}
1819

20+
const formatProperties = (properties: Record<string, unknown>): Record<string, unknown> => {
21+
return Object.keys(properties).reduce((obj, key) => {
22+
const value = properties[key];
23+
// noinspection SuspiciousTypeOfGuard
24+
const isCount = key === 'count' || (typeof key === 'string' && key.endsWith('_count'));
25+
26+
return {
27+
...obj,
28+
[key]: isCount || typeof value !== 'string'
29+
? (isObject(value) ? formatProperties(value) : value)
30+
: `<strong>${value}</strong>`,
31+
};
32+
}, {});
33+
};
34+
1935
export default ({ activity, children }: Props) => {
2036
const location = useLocation();
2137
const actor = activity.relationships.actor;
@@ -27,12 +43,7 @@ export default ({ activity, children }: Props) => {
2743
return current.toString();
2844
};
2945

30-
const properties = Object.keys(activity.properties).reduce((obj, key) => ({
31-
...obj,
32-
[key]: key === 'count' || key.endsWith('_count')
33-
? activity.properties[key]
34-
: `<strong>${activity.properties[key]}</strong>`,
35-
}), {});
46+
const properties = formatProperties(activity.properties);
3647

3748
return (
3849
<div className={'grid grid-cols-10 py-4 border-b-2 border-gray-800 last:rounded-b last:border-0 group'}>

resources/scripts/helpers.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,4 +74,4 @@ export const isEmptyObject = (o: {}): boolean =>
7474
Object.keys(o).length === 0 && Object.getPrototypeOf(o) === Object.prototype;
7575

7676
// eslint-disable-next-line @typescript-eslint/ban-types
77-
export const getObjectKeys = <T extends {}> (o: T): Array<keyof T> => Object.keys(o) as Array<keyof T>;
77+
export const getObjectKeys = <T extends {}> (o: T): (keyof T)[] => Object.keys(o) as (keyof typeof o)[];

0 commit comments

Comments
 (0)