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

Type locale.baseText() to i18n keys #3133

Merged
merged 10 commits into from
Apr 15, 2022
Merged
Show file tree
Hide file tree
Changes from 7 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
10 changes: 8 additions & 2 deletions packages/editor-ui/src/components/DeleteUserModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,10 @@ export default mixins(showMessage).extend({
},
title(): string {
const user = this.userToDelete && (this.userToDelete.fullName || this.userToDelete.email);
return this.$locale.baseText('settings.users.deleteUser', { interpolate: { user }});
return this.$locale.baseText(
'settings.users.deleteUser',
{ interpolate: { user: user || '' }},
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe move this || '' to variable above

);
},
enabled(): boolean {
if (this.isPending) {
Expand Down Expand Up @@ -138,7 +141,10 @@ export default mixins(showMessage).extend({
if (this.transferId) {
const getUserById = this.$store.getters['users/getUserById'];
const transferUser: IUser = getUserById(this.transferId);
message = this.$locale.baseText('settings.users.transferredToUser', { interpolate: { user: transferUser.fullName }});
message = this.$locale.baseText(
'settings.users.transferredToUser',
{ interpolate: { user: transferUser.fullName || '' }},
);
}

this.$showMessage({
Expand Down
5 changes: 4 additions & 1 deletion packages/editor-ui/src/components/InviteUsersModal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ export default mixins(showMessage).extend({
},
buttonLabel(): string {
if (this.emailsCount > 1) {
return this.$locale.baseText('settings.users.inviteXUser', { interpolate: { count: this.emailsCount }});
return this.$locale.baseText(
'settings.users.inviteXUser',
{ interpolate: { count: this.emailsCount.toString() }},
);
}

return this.$locale.baseText('settings.users.inviteUser');
Expand Down
7 changes: 5 additions & 2 deletions packages/editor-ui/src/components/Node.vue
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,16 @@ export default mixins(externalHooks, nodeBase, nodeHelpers, workflowHelpers).ext
if (this.nodeType !== null && this.nodeType.hasOwnProperty('eventTriggerDescription')) {
const nodeName = this.$locale.shortNodeType(this.nodeType.name);
const { eventTriggerDescription } = this.nodeType;
return this.$locale.nodeText().eventTriggerDescription(nodeName, eventTriggerDescription);
return this.$locale.nodeText().eventTriggerDescription(
nodeName,
eventTriggerDescription || '',
);
} else {
return this.$locale.baseText(
'node.waitingForYouToCreateAnEventIn',
{
interpolate: {
nodeType: this.nodeType && getTriggerNodeServiceName(this.nodeType.displayName),
nodeType: this.nodeType ? getTriggerNodeServiceName(this.nodeType.displayName) : '',
},
},
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<script lang="ts">
import Vue from 'vue';
import camelcase from 'lodash.camelcase';
import { CategoryName } from '@/plugins/i18n';

export default Vue.extend({
props: ['item'],
Expand All @@ -24,8 +25,8 @@ export default Vue.extend({
},
},
methods: {
renderCategoryName(categoryName: string) {
const key = `nodeCreator.categoryNames.${categoryName}`;
renderCategoryName(categoryName: CategoryName) {
const key = `nodeCreator.categoryNames.${categoryName}` as const;

return this.$locale.exists(key) ? this.$locale.baseText(key) : categoryName;
},
Expand Down Expand Up @@ -56,4 +57,4 @@ export default Vue.extend({
width: 12px;
color: $--node-creator-arrow-color;
}
</style>
</style>
2 changes: 1 addition & 1 deletion packages/editor-ui/src/components/WorkflowSettings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ export default mixins(
interpolate: {
defaultValue: this.defaultValues.saveDataErrorExecution === 'all'
? this.$locale.baseText('workflowSettings.saveDataErrorExecutionOptions.save')
: this.$locale.baseText('workflowSettings.saveDataErrorExecutionOptions.doNotsave'),
: this.$locale.baseText('workflowSettings.saveDataErrorExecutionOptions.doNotSave'),
},
},
),
Expand Down
26 changes: 23 additions & 3 deletions packages/editor-ui/src/plugins/i18n/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
locale,
} from 'n8n-design-system';

const englishBaseText = require('./locales/en');
import englishBaseText from './locales/en.json';

Vue.use(VueI18n);
locale.use('en');
Expand Down Expand Up @@ -62,8 +62,8 @@ export class I18nClass {
* Render a string of base text, i.e. a string with a fixed path to the localized value. Optionally allows for [interpolation](https://kazupon.github.io/vue-i18n/guide/formatting.html#named-formatting) when the localized value contains a string between curly braces.
*/
baseText(
key: string,
options?: { adjustToNumber: number; interpolate: { [key: string]: string } },
key: BaseTextKey,
options?: { adjustToNumber?: number; interpolate?: { [key: string]: string } },
): string {
if (options && options.adjustToNumber) {
return this.i18n.tc(key, options.adjustToNumber, options && options.interpolate).toString();
Expand Down Expand Up @@ -469,3 +469,23 @@ export function addHeaders(
Object.assign(i18nInstance.messages[language], { headers }),
);
}

// ----------------------------------
// typings
// ----------------------------------

declare module 'vue/types/vue' {
interface Vue {
$locale: I18nClass;
}
}

type GetBaseTextKey<T> = T extends `_${string}` ? never : T;

export type BaseTextKey = GetBaseTextKey<keyof typeof englishBaseText>;

type GetCategoryName<T> = T extends `nodeCreator.categoryNames.${infer C}`
? C
: never;

export type CategoryName = GetCategoryName<keyof typeof englishBaseText>;
16 changes: 7 additions & 9 deletions packages/editor-ui/src/plugins/i18n/locales/en.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{
"_reusableBaseText.cancel": "Cancel",
Copy link
Contributor

Choose a reason for hiding this comment

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

need to update the guide

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

"_reusableBaseText.name": "Name",
"_reusableBaseText.save": "Save",
"_reusableDynamicText.oauth2.clientId": "Client ID",
"_reusableDynamicText.oauth2.clientSecret": "Client Secret",
"about.aboutN8n": "About n8n",
"about.close": "Close",
"about.license": "License",
Expand Down Expand Up @@ -44,10 +49,8 @@
"auth.setup.ownerAccountBenefits": "Setting up an owner account allows you to invite others, and prevents people using n8n without an account",
"auth.setup.settingUpOwnerError": "Problem setting up owner",
"auth.setup.setupConfirmation.concatEntities": "{workflows} and {credentials}",
"auth.setup.setupConfirmation.credentialsCount": "{count} credentials",
"auth.setup.setupConfirmation.oneCredentialCount": "{count} credential",
"auth.setup.setupConfirmation.oneWorkflowCount": "{count} existing workflow",
"auth.setup.setupConfirmation.workflowsCount": "{count} existing workflows",
"auth.setup.setupConfirmation.credentials": "{count} credential | {count} credentials",
"auth.setup.setupConfirmation.existingWorkflows": "{count} existing workflow | {count} existing workflows",
"auth.setup.setupOwner": "Set up owner account",
"auth.setup.skipOwnerSetupQuestion": "Skip owner account setup?",
"auth.setup.skipSetup": "Skip setup",
Expand Down Expand Up @@ -573,11 +576,6 @@
"pushConnection.showMessage.title": "Workflow executed successfully",
"pushConnectionTracker.cannotConnectToServer": "You have a connection issue or the server is down. <br />n8n should reconnect automatically once the issue is resolved.",
"pushConnectionTracker.connectionLost": "Connection lost",
"reusableBaseText.cancel": "Cancel",
"reusableBaseText.name": "Name",
"reusableBaseText.save": "Save",
"reusableDynamicText.oauth2.clientId": "Client ID",
"reusableDynamicText.oauth2.clientSecret": "Client Secret",
"runData.binary": "Binary",
"runData.copyItemPath": "Copy Item Path",
"runData.copyParameterPath": "Copy Parameter Path",
Expand Down
7 changes: 0 additions & 7 deletions packages/editor-ui/src/plugins/i18n/types.d.ts

This file was deleted.

5 changes: 4 additions & 1 deletion packages/editor-ui/src/views/SettingsUsersView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,10 @@ export default mixins(showMessage).extend({
this.$showToast({
type: 'success',
title: this.$locale.baseText('settings.users.inviteResent'),
message: this.$locale.baseText('settings.users.emailSentTo', { interpolate: { email: user.email } }),
message: this.$locale.baseText(
'settings.users.emailSentTo',
{ interpolate: { email: user.email || '' } },
),
});
} catch (e) {
this.$showError(e, this.$locale.baseText('settings.users.userReinviteError'));
Expand Down
15 changes: 13 additions & 2 deletions packages/editor-ui/src/views/SetupView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,19 @@ export default mixins(
return true;
}

const workflows = this.workflowsCount > 0 ? this.$locale.baseText(this.workflowsCount === 1 ? 'auth.setup.setupConfirmation.oneWorkflowCount' : 'auth.setup.setupConfirmation.workflowsCount', { interpolate: { count: this.workflowsCount } }) : '';
const credentials = this.credentialsCount > 0 ? this.$locale.baseText(this.credentialsCount === 1? 'auth.setup.setupConfirmation.oneCredentialCount' : 'auth.setup.setupConfirmation.credentialsCount', { interpolate: { count: this.credentialsCount } }) : '';
const workflows = this.workflowsCount > 0
? this.$locale.baseText(
'auth.setup.setupConfirmation.existingWorkflows',
{ adjustToNumber: this.workflowsCount },
)
: '';

const credentials = this.credentialsCount > 0
? this.$locale.baseText(
'auth.setup.setupConfirmation.credentials',
{ adjustToNumber: this.credentialsCount },
)
: '';

const entities = workflows && credentials ? this.$locale.baseText('auth.setup.setupConfirmation.concatEntities', {interpolate: { workflows, credentials }}) : (workflows || credentials);
return await this.confirmMessage(
Expand Down
1 change: 1 addition & 0 deletions packages/editor-ui/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"esModuleInterop": true,
"allowSyntheticDefaultImports": true,
"useUnknownInCatchVariables": false,
"resolveJsonModule": true,
"sourceMap": true,
"baseUrl": ".",
"types": [
Expand Down
3 changes: 2 additions & 1 deletion packages/editor-ui/tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"linterOptions": {
"exclude": [
"node_modules/**/*",
"../../node_modules/**/*"
"../../node_modules/**/*",
"**/*.json"
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the i18n keys are now imported via resolveJsonModule, vue-cli attempts to lint JSON as JS, breaking the build.

]
},
"defaultSeverity": "error",
Expand Down