Skip to content

Commit

Permalink
feat(admin-ui): Allow configuration of available locales (#2550)
Browse files Browse the repository at this point in the history
  • Loading branch information
HoseinGhanbari authored Dec 4, 2023
1 parent 3645819 commit dfddf0f
Show file tree
Hide file tree
Showing 12 changed files with 681 additions and 335 deletions.
254 changes: 253 additions & 1 deletion packages/admin-ui-plugin/src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import path from 'path';
export const DEFAULT_APP_PATH = path.join(__dirname, '../admin-ui');
export const loggerCtx = 'AdminUiPlugin';
export const defaultLanguage = LanguageCode.en;
export const defaultLocale = undefined;
export const defaultLocale = 'US';

export const defaultAvailableLanguages = [
LanguageCode.he,
Expand All @@ -26,3 +26,255 @@ export const defaultAvailableLanguages = [
LanguageCode.ne,
LanguageCode.hr,
];

export const defaultAvailableLocales = [
'AF',
'AL',
'DZ',
'AS',
'AD',
'AO',
'AI',
'AQ',
'AG',
'AR',
'AM',
'AW',
'AU',
'AT',
'AZ',
'BS',
'BH',
'BD',
'BB',
'BY',
'BE',
'BZ',
'BJ',
'BM',
'BT',
'BO',
'BQ',
'BA',
'BW',
'BV',
'BR',
'IO',
'BN',
'BG',
'BF',
'BI',
'CV',
'KH',
'CM',
'CA',
'KY',
'CF',
'TD',
'CL',
'CN',
'CX',
'CC',
'CO',
'KM',
'CD',
'CG',
'CK',
'CR',
'HR',
'CU',
'CW',
'CY',
'CZ',
'CI',
'DK',
'DJ',
'DM',
'DO',
'EC',
'EG',
'SV',
'GQ',
'ER',
'EE',
'SZ',
'ET',
'FK',
'FO',
'FJ',
'FI',
'FR',
'GF',
'PF',
'TF',
'GA',
'GM',
'GE',
'DE',
'GH',
'GI',
'GR',
'GL',
'GD',
'GP',
'GU',
'GT',
'GG',
'GN',
'GW',
'GY',
'HT',
'HM',
'VA',
'HN',
'HK',
'HU',
'IS',
'IN',
'ID',
'IR',
'IQ',
'IE',
'IM',
'IL',
'IT',
'JM',
'JP',
'JE',
'JO',
'KZ',
'KE',
'KI',
'KP',
'KR',
'KW',
'KG',
'LA',
'LV',
'LB',
'LS',
'LR',
'LY',
'LI',
'LT',
'LU',
'MO',
'MG',
'MW',
'MY',
'MV',
'ML',
'MT',
'MH',
'MQ',
'MR',
'MU',
'YT',
'MX',
'FM',
'MD',
'MC',
'MN',
'ME',
'MS',
'MA',
'MZ',
'MM',
'NA',
'NR',
'NP',
'NL',
'NC',
'NZ',
'NI',
'NE',
'NG',
'NU',
'NF',
'MK',
'MP',
'NO',
'OM',
'PK',
'PW',
'PS',
'PA',
'PG',
'PY',
'PE',
'PH',
'PN',
'PL',
'PT',
'PR',
'QA',
'RO',
'RU',
'RW',
'RE',
'BL',
'SH',
'KN',
'LC',
'MF',
'PM',
'VC',
'WS',
'SM',
'ST',
'SA',
'SN',
'RS',
'SC',
'SL',
'SG',
'SX',
'SK',
'SI',
'SB',
'SO',
'ZA',
'GS',
'SS',
'ES',
'LK',
'SD',
'SR',
'SJ',
'SE',
'CH',
'SY',
'TW',
'TJ',
'TZ',
'TH',
'TL',
'TG',
'TK',
'TO',
'TT',
'TN',
'TR',
'TM',
'TC',
'TV',
'UG',
'UA',
'AE',
'GB',
'UM',
'US',
'UY',
'UZ',
'VU',
'VE',
'VN',
'VG',
'VI',
'WF',
'EH',
'YE',
'ZM',
'ZW',
'AX',
];
22 changes: 18 additions & 4 deletions packages/admin-ui-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { MiddlewareConsumer, NestModule } from '@nestjs/common';
import { DEFAULT_AUTH_TOKEN_HEADER_KEY, DEFAULT_CHANNEL_TOKEN_KEY } from '@vendure/common/lib/shared-constants';
import {
DEFAULT_AUTH_TOKEN_HEADER_KEY,
DEFAULT_CHANNEL_TOKEN_KEY,
} from '@vendure/common/lib/shared-constants';
import {
AdminUiAppConfig,
AdminUiAppDevModeConfig,
Expand Down Expand Up @@ -27,6 +30,7 @@ import {
defaultLocale,
DEFAULT_APP_PATH,
loggerCtx,
defaultAvailableLocales,
} from './constants';
import { MetricsService } from './service/metrics.service';

Expand Down Expand Up @@ -257,8 +261,17 @@ export class AdminUiPlugin implements NestModule {
const propOrDefault = <Prop extends keyof AdminUiConfig>(
prop: Prop,
defaultVal: AdminUiConfig[Prop],
isArray: boolean = false,
): AdminUiConfig[Prop] => {
return partialConfig ? (partialConfig as AdminUiConfig)[prop] || defaultVal : defaultVal;
if (isArray) {
const isValidArray = !!partialConfig
? !!((partialConfig as AdminUiConfig)[prop] as any[])?.length
: false;

return !!partialConfig && isValidArray ? (partialConfig as AdminUiConfig)[prop] : defaultVal;
} else {
return partialConfig ? (partialConfig as AdminUiConfig)[prop] || defaultVal : defaultVal;
}
};
return {
adminApiPath: propOrDefault('adminApiPath', apiOptions.adminApiPath),
Expand All @@ -274,11 +287,12 @@ export class AdminUiPlugin implements NestModule {
),
channelTokenKey: propOrDefault(
'channelTokenKey',
apiOptions.channelTokenKey || DEFAULT_CHANNEL_TOKEN_KEY
apiOptions.channelTokenKey || DEFAULT_CHANNEL_TOKEN_KEY,
),
defaultLanguage: propOrDefault('defaultLanguage', defaultLanguage),
defaultLocale: propOrDefault('defaultLocale', defaultLocale),
availableLanguages: propOrDefault('availableLanguages', defaultAvailableLanguages),
availableLanguages: propOrDefault('availableLanguages', defaultAvailableLanguages, true),
availableLocales: propOrDefault('availableLocales', defaultAvailableLocales, true),
loginUrl: options.adminUiConfig?.loginUrl,
brand: options.adminUiConfig?.brand,
hideVendureBranding: propOrDefault(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class AppShellComponent implements OnInit {
uiLanguageAndLocale$: LocalizationLanguageCodeType;
direction$: LocalizationDirectionType;
availableLanguages: LanguageCode[] = [];
availableLocales: string[] = [];
hideVendureBranding = getAppConfig().hideVendureBranding;
pageTitle$: Observable<string>;
mainNavExpanded$: Observable<boolean>;
Expand Down Expand Up @@ -57,6 +58,8 @@ export class AppShellComponent implements OnInit {

this.availableLanguages = this.i18nService.availableLanguages;

this.availableLocales = this.i18nService.availableLocales;

this.pageTitle$ = this.breadcrumbService.breadcrumbs$.pipe(
map(breadcrumbs => breadcrumbs[breadcrumbs.length - 1].label),
);
Expand All @@ -70,17 +73,18 @@ export class AppShellComponent implements OnInit {
this.uiLanguageAndLocale$
.pipe(
take(1),
switchMap(([currentLanguage, currentLocale]) =>
this.modalService.fromComponent(UiLanguageSwitcherDialogComponent, {
switchMap(([currentLanguage, currentLocale]) => {
return this.modalService.fromComponent(UiLanguageSwitcherDialogComponent, {
closable: true,
size: 'lg',
locals: {
availableLocales: this.availableLocales,
availableLanguages: this.availableLanguages,
currentLanguage,
currentLocale,
currentLanguage: currentLanguage,
currentLocale: currentLocale,
},
}),
),
});
}),
switchMap(result =>
result ? this.dataService.client.setUiLanguage(result[0], result[1]) : EMPTY,
),
Expand Down
Loading

0 comments on commit dfddf0f

Please sign in to comment.