Skip to content
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
15 changes: 13 additions & 2 deletions packages/craftcms-cp/src/utilities/translate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {formatNumber} from './format.js';

type TranslationStore = Record<string, Record<string, string>>;

let translationsStore: TranslationStore = {};

export function tokenizePattern(
pattern: string
): Array<string | string[]> | false {
Expand Down Expand Up @@ -202,18 +206,25 @@ export function formatMessage(pattern: string, params: object): string {
return tokens.join('');
}

export function setTranslations(translations: TranslationStore) {
translationsStore = translations;
}

export function t(
message: string,
params?: Record<any, any>,
category: string = 'app',
store?: Record<string, any>
store: TranslationStore = translationsStore
): string {
if (
store &&
typeof store[category] !== 'undefined' &&
typeof store[category][message] !== 'undefined'
) {
message = store[category][message];
const translated = store?.[category]?.[message];
if (translated !== undefined) {
message = translated;
}
}

if (params) {
Expand Down
3 changes: 3 additions & 0 deletions resources/js/bootstrap/cp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import ProjectConfig from '@/components/utilities/ProjectConfig/ProjectConfig.vu
import AssetIndexes from '@/components/utilities/AssetIndexes/AssetIndexes.vue';
import SystemMessages from '@/components/utilities/SystemMessages/SystemMessages.vue';
import DeprecationErrorsToolbar from '@/components/utilities/DeprecationErrors/DeprecationErrorsToolbar.vue';
import {setTranslations} from '@craftcms/cp/utilities/translate.ts.mjs';

let bootedCallbacks: Array<(instance: any) => void> = [];
let bootingCallbacks: Array<(instance: any) => void> = [];
Expand Down Expand Up @@ -58,6 +59,8 @@ const Cp = {
appId: config.get('systemUid', ''),
canAccessQueueManager: config.get('canAccessQueueManager', false),
});

setTranslations(this.initialConfig.translations);
},

async start() {
Expand Down
3 changes: 3 additions & 0 deletions src/Cp/Cp.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
namespace CraftCms\Cms\Cp;

use CraftCms\Cms\Cms;
use CraftCms\Cms\Support\Facades\I18N;
use CraftCms\Cms\Support\Url;
use Illuminate\Support\Collection;
use stdClass;

readonly class Cp
{
Expand All @@ -25,6 +27,7 @@ public static function config(): Collection
'csrfTokenName',
])
->merge([
'translations' => I18N::getAllTranslationsForLocale(app()->getLocale()) ?: new stdClass,
'csrfTokenValue' => csrf_token(),
'actionUrl' => Url::actionUrl(),
'cpUrl' => Url::cpUrl(),
Expand Down
Loading