You can therefore import from the `~/locale` file.
For example:
This means you can externalize strings in Vue templates without having to import these functions from the `~/locale` file:
```javascript
import{__,s__}from'~/locale';
constlabel=__('Subscribe');
constnameSpacedlabel=__('Plan|Subscribe');
```html
<template>
<h1>{{ s__('Branches|Create a new branch') }}</h1>
<gl-button>{{ __('Create branch') }}</gl-button>
</template>
```
For the static text strings we suggest two patterns for using these translations in Vue files:
If you need to translate strings in the Vue component's JavaScript, you can import the necessary externalization function from the `~/locale` file as described in the [JavaScript files](#javascript-files) section.
- External constants file:
To test Vue translations, learn about [manually testing translations from the UI](#manually-test-translations-from-the-ui).
```javascript
#### Recommendations
If strings are reused throughout a component, it can be useful to define these strings as variables. We recommend defining an `i18n` property on the component's `$options` object. If there is a mixture of many-use and single-use strings in the component, consider using this approach to create a local [Single Source of Truth](https://about.gitlab.com/handbook/values/#single-source-of-truth) for externalized strings.
Also consider defining these strings in a `constants.js` file, especially if they need
to be shared across different modules.
```javascript
javascripts
│
└───alert_settings
...
...
@@ -179,60 +209,41 @@ For the static text strings we suggest two patterns for using these translations
/* Integration constants */
exportconstI18N_ALERT_SETTINGS_FORM={
saveBtnLabel:__('Save changes'),
};
exportconstMSG_ALERT_SETTINGS_FORM_ERROR=__('Failed to save alert settings.')
// alert_settings_form.vue
import{
I18N_ALERT_SETTINGS_FORM,
MSG_ALERT_SETTINGS_FORM_ERROR,
}from'../constants';
<script>
exportdefault{
i18n:{
I18N_ALERT_SETTINGS_FORM,
}
MSG_ALERT_SETTINGS_FROM_ERROR,
}
</script>
<template>
<gl-button
ref="submitBtn"
variant="success"
type="submit"
>
{{$options.i18n.I18N_ALERT_SETTINGS_FORM}}
</gl-button>
<gl-alertv-if="showAlert">
{{$options.MSG_ALERT_SETTINGS_FORM_ERROR}}
</gl-alert>
</template>
```
When possible, you should opt for this pattern, as this allows you to import these strings directly into your component specs for re-use during testing.
- Internal component `$options` object:
```
```javascript
<script>
exportdefault{
i18n:{
buttonLabel:s__('Plan|Button Label')
}
},
</script>
Using either `constants` or `$options.i18n` allows us to reference messages directly in specs: