|
| 1 | +// import { TranslationPath } from '@/Interface'; |
| 2 | +import Vue from 'vue'; |
| 3 | + |
| 4 | +/** |
| 5 | + * Mixin to translate: |
| 6 | + * - base strings, i.e. any string that is not node- or credentials-specific, |
| 7 | + * - specific strings, |
| 8 | + * - node-specific strings, i.e. those in `NodeView.vue`, |
| 9 | + * - credentials-specific strings, i.e. those in `EditCredentials.vue`. |
| 10 | + */ |
| 11 | +export const translate = Vue.extend({ |
| 12 | + computed: { |
| 13 | + /** |
| 14 | + * Node type for the active node in `NodeView.vue`. |
| 15 | + */ |
| 16 | + activeNodeType (): string { |
| 17 | + return this.$store.getters.activeNode.type; |
| 18 | + }, |
| 19 | + }, |
| 20 | + |
| 21 | + methods: { |
| 22 | + // ----------------------------------------- |
| 23 | + // main methods |
| 24 | + // ----------------------------------------- |
| 25 | + |
| 26 | + /** |
| 27 | + * Translate a base string. Called directly in Vue templates. |
| 28 | + * Optionally, [interpolate a variable](https://kazupon.github.io/vue-i18n/guide/formatting.html#named-formatting). |
| 29 | + */ |
| 30 | + $baseText( |
| 31 | + key: string, |
| 32 | + options?: { interpolate?: { [key: string]: string } }, |
| 33 | + ): string { |
| 34 | + const translatedBaseString = options && options.interpolate |
| 35 | + ? this.$t(key, options.interpolate) |
| 36 | + : this.$t(key); |
| 37 | + |
| 38 | + return translatedBaseString.toString(); |
| 39 | + }, |
| 40 | + |
| 41 | + /** |
| 42 | + * Translate a node- or credentials-specific string. |
| 43 | + * Called in-mixin by node- or credentials-specific methods, |
| 44 | + * which are called directly in Vue templates. |
| 45 | + */ |
| 46 | + translateSpecific( |
| 47 | + { key, fallback }: { key: string, fallback: string }, |
| 48 | + ): string { |
| 49 | + return this.$te(key) ? this.$t(key).toString() : fallback; |
| 50 | + }, |
| 51 | + |
| 52 | + // ----------------------------------------- |
| 53 | + // node-specific methods |
| 54 | + // ----------------------------------------- |
| 55 | + |
| 56 | + /** |
| 57 | + * Translate a top-level node parameter name, i.e. leftmost parameter in `NodeView.vue`. |
| 58 | + */ |
| 59 | + $translateNodeParameterName( |
| 60 | + { name: parameterName, displayName }: { name: string; displayName: string; }, |
| 61 | + ) { |
| 62 | + return this.translateSpecific({ |
| 63 | + key: `${this.activeNodeType}.parameters.${parameterName}.displayName`, |
| 64 | + fallback: displayName, |
| 65 | + }); |
| 66 | + }, |
| 67 | + |
| 68 | + /** |
| 69 | + * Translate a top-level parameter description for a node or for credentials. |
| 70 | + */ |
| 71 | + $translateDescription( |
| 72 | + { name: parameterName, description }: { name: string; description: string; }, |
| 73 | + ) { |
| 74 | + return this.translateSpecific({ |
| 75 | + key: `${this.activeNodeType}.parameters.${parameterName}.description`, |
| 76 | + fallback: description, |
| 77 | + }); |
| 78 | + }, |
| 79 | + |
| 80 | + /** |
| 81 | + * Translate the name for an option in a `collection` or `fixed collection` parameter, |
| 82 | + * e.g. an option name in an "Additional Options" fixed collection. |
| 83 | + */ |
| 84 | + $translateCollectionOptionName( |
| 85 | + { name: parameterName }: { name: string; }, |
| 86 | + { name: optionName, displayName }: { name: string; displayName: string; }, |
| 87 | + ) { |
| 88 | + return this.translateSpecific({ |
| 89 | + key: `${this.activeNodeType}.parameters.${parameterName}.options.${optionName}.displayName`, |
| 90 | + fallback: displayName, |
| 91 | + }); |
| 92 | + }, |
| 93 | + |
| 94 | + /** |
| 95 | + * Translate the label for a button that adds another field-input pair to a collection. |
| 96 | + */ |
| 97 | + $translateMultipleValueButtonText( |
| 98 | + { name: parameterName, typeOptions: { multipleValueButtonText } }: |
| 99 | + { name: string, typeOptions: { multipleValueButtonText: string } }, |
| 100 | + ) { |
| 101 | + return this.translateSpecific({ |
| 102 | + key: `${this.activeNodeType}.parameters.${parameterName}.multipleValueButtonText`, |
| 103 | + fallback: multipleValueButtonText, |
| 104 | + }); |
| 105 | + }, |
| 106 | + |
| 107 | + // ----------------------------------------- |
| 108 | + // creds-specific methods |
| 109 | + // ----------------------------------------- |
| 110 | + |
| 111 | + /** |
| 112 | + * Translate a credentials property name, i.e. leftmost parameter in `CredentialsEdit.vue`. |
| 113 | + */ |
| 114 | + $translateCredentialsPropertyName( |
| 115 | + { name: parameterName, displayName }: { name: string; displayName: string; }, |
| 116 | + { nodeType, credentialsName }: { nodeType: string, credentialsName: string; }, |
| 117 | + ) { |
| 118 | + if (['clientId', 'clientSecret'].includes(parameterName)) { |
| 119 | + return this.$t(`oauth2.${parameterName}`); |
| 120 | + } |
| 121 | + |
| 122 | + return this.translateSpecific({ |
| 123 | + key: `${nodeType}.credentials.${credentialsName}.${parameterName}.displayName`, |
| 124 | + fallback: displayName, |
| 125 | + }); |
| 126 | + }, |
| 127 | + |
| 128 | + /** |
| 129 | + * Translate a credentials property description, i.e. label tooltip in `CredentialsEdit.vue`. |
| 130 | + */ |
| 131 | + $translateCredentialsPropertyDescription( |
| 132 | + { name: parameterName, description }: { name: string; description: string; }, |
| 133 | + { nodeType, credentialsName }: { nodeType: string, credentialsName: string; }, |
| 134 | + ) { |
| 135 | + return this.translateSpecific({ |
| 136 | + key: `${nodeType}.credentials.${credentialsName}.${parameterName}.description`, |
| 137 | + fallback: description, |
| 138 | + }); |
| 139 | + }, |
| 140 | + |
| 141 | + // ----------------------------------------- |
| 142 | + // node- and creds-specific methods |
| 143 | + // ----------------------------------------- |
| 144 | + |
| 145 | + /** |
| 146 | + * Translate the placeholder inside the input field for a string-type parameter. |
| 147 | + */ |
| 148 | + $translatePlaceholder( |
| 149 | + { name: parameterName, placeholder }: { name: string; placeholder: string; }, |
| 150 | + isCredential = false, |
| 151 | + { nodeType, credentialsName } = { nodeType: '', credentialsName: '' }, |
| 152 | + ) { |
| 153 | + const key = isCredential |
| 154 | + ? `${nodeType}.credentials.${credentialsName}.placeholder` |
| 155 | + : `${this.activeNodeType}.parameters.${parameterName}.placeholder`; |
| 156 | + |
| 157 | + return this.translateSpecific({ |
| 158 | + key, |
| 159 | + fallback: placeholder, |
| 160 | + }); |
| 161 | + }, |
| 162 | + |
| 163 | + /** |
| 164 | + * Translate the name for an option in an `options` parameter, |
| 165 | + * e.g. an option name in a "Resource" or "Operation" dropdown menu. |
| 166 | + */ |
| 167 | + $translateOptionsOptionName( |
| 168 | + { name: parameterName }: { name: string }, |
| 169 | + { value: optionName, name: displayName }: { value: string; name: string; }, |
| 170 | + isCredential = false, |
| 171 | + { nodeType, credentialsName } = { nodeType: '', credentialsName: '' }, |
| 172 | + ) { |
| 173 | + const key = isCredential |
| 174 | + ? `${nodeType}.credentials.${credentialsName}.options.${optionName}.displayName` |
| 175 | + : `${this.activeNodeType}.parameters.${parameterName}.options.${optionName}.displayName`; |
| 176 | + |
| 177 | + return this.translateSpecific({ |
| 178 | + key, |
| 179 | + fallback: displayName, |
| 180 | + }); |
| 181 | + }, |
| 182 | + |
| 183 | + /** |
| 184 | + * Translate the description for an option in an `options` parameter, |
| 185 | + * e.g. an option name in a "Resource" or "Operation" dropdown menu. |
| 186 | + */ |
| 187 | + $translateOptionsOptionDescription( |
| 188 | + { name: parameterName }: { name: string }, |
| 189 | + { value: optionName, description }: { value: string; description: string; }, |
| 190 | + isCredential = false, |
| 191 | + { nodeType, credentialsName } = { nodeType: '', credentialsName: '' }, |
| 192 | + ) { |
| 193 | + const key = isCredential |
| 194 | + ? `${nodeType}.credentials.${credentialsName}.options.${optionName}.description` |
| 195 | + : `${this.activeNodeType}.parameters.${parameterName}.options.${optionName}.description`; |
| 196 | + |
| 197 | + return this.translateSpecific({ |
| 198 | + key, |
| 199 | + fallback: description, |
| 200 | + }); |
| 201 | + }, |
| 202 | + }, |
| 203 | +}); |
0 commit comments