Searching for all unconverted strings in a Vuejs project can be achieved by following these steps

I have recently found myself delving into a Vue.js and Typescript project with legacy code. The project utilizes the vue-i18n library for handling translations, using this.$t in every component to access translations stored in directories like translations/[en/es]/FileName.ts. Here's an example:

//translations/en/Forms.ts

export default {
  Fields: {
    Name: "Name",
    LastName: "Last name",
    Direction: "Full direction",
    ZipCode: "Zip code",
    Phone: {
        Number: "Number",
        Prefix: "Prefix"
    }
  }
};

The translation usage looks like this:

//pages/UserForm.vue

<div>
   <label> {{$t("Fields.Direction")}} </label>
   <input type="text" name="directionField" required />
</div>

Despite the integration of the library, there are still many strings within the <templates> that were manually included by previous developers without utilizing the translation library, such as:

<div v-if="hasError">
    Oops! There seem to be bugs
</div>

My query: I am wondering if there exists a method, library, plugin, or script that can highlight all untranslated strings automatically?

Answer №1

When working with VS Code, one useful plugin to consider is called "i18n-ally", which includes a handy feature for identifying hard-coded strings.

This feature can be accessed through the command palette, although it's currently labeled as experimental. Despite this, I've found it quite effective in practice.

Answer №2

There are actually two choices available:

  1. Performing runtime check through validation methods
  2. Conducting compile time check via typing functions

Runtime check

This approach is quite straightforward - simply run all translations through a helper function that will throw an error in the console if a translation is missing. However, it seems like vue-i18n already has this feature, and you're more interested in option number 2.

Compile time check

You can ensure type safety for your translation function by defining types for incoming values.

Issue one

Typing access to deeply nested structures with simple strings can be challenging. To restrict $t from accepting any strings other than paths present in your translations, you must use the latest TypeScript 4.1 version. To update to the newest version, run npm i -D typescript.

I've asked a similar question on Stack Overflow and made progress in this area. You can explore my properly-typed project on Github where you'll find useful types for your task.

It might look something like this:

[Typescript code snippet]

Here's a link to a Playground for testing.

I may consider releasing this as an NPM package in the future, once I have the time. I'll keep you posted on any developments.

Issue two

Different languages may have varying missing translations. In such cases, you'll need a typecheck based on the selected language. Your typedTranslate function should accept different types for different translations.

[More Typescript code snippet]

Here's a link to a Playground for trying it out.

Issue three

It appears that you have folder structures for each language, so you'll need to consolidate them into a single object or type to utilize the typedTranslate function effectively.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

It seems that every time you save data in Angular, the local storage array gets overwritten

While using Angular, I encountered an issue with saving to local storage. The code works fine for saving items initially, but on refreshing the page and trying to add more objects to the local storage array, it overwrites instead of appending. Can you help ...

Switching between the open and close buttons within a Vue component

Just starting out with Vue and need some guidance on implementing a specific feature. I want an open button (icon) in the navigation bar. Clicking it should change it to a close icon and load a new template within the same page. Clicking the close icon sh ...

Error: SignalR Server has sent a "Close" message

I have been struggling with this error for quite a while. I am trying to make signalr listen to my 2 methods. It works perfectly when I comment out the (this) methods. However, it does not work. Can anyone help me figure this out? Sometimes I can retrieve ...

Guide to pairing array elements in JavaScript

To streamline the array, only elements with a value equal to or greater than the set threshold will be retained. These selected elements will then be used to create a new array comprising multiple objects. Each object will consist of two properties: the st ...

Angular: Inject all dependencies into a component's class

I'm curious about how to supply an injectable class in Angular (4+) that includes all of its dependencies. For example: If we have an injectable called DepMaster, which has dependencies such as: DepServantA DepServantB Simply providing DepMaster a ...

Retrieve and present JSON information (object, array) using a service provider in Ionic 2 with TypeScript and Angular 2

I am currently delving into Angular 2 and Ionic 2 (not to forget about NodeJS and TypeScript). For testing purposes, I have a list of users available here. Eventually, I will be fetching real user data from an API connected to my database. To handle this ...

Angular has deprecated the use of `isObject` and `isNullOrUndefined` in TypeScript

Upon upgrading from Angular 7 to Angular 10 and implementing TypeScript 4 or higher, I began receiving deprecation warnings for the functions isObject() and isNullOrUndefined() when running ng lint warnings The function isNullOrUndefined has been depreca ...

Challenges with comprehending VueX in VueJS

My project involves building a Single Page Application (SPA) using VueJs and VueX. I have implemented two buttons, "Login" and "Sign Up", in one component and a <component></component> tag in another component where one of two Modals ("SignUp" ...

Reorganizing the layout of a React grid in response to changes in screen size

Currently, I'm facing a challenge in creating a container for a React app that includes a Highcharts chart and three textboxes. My struggle lies in getting the three textboxes to adjust and rearrange beneath the chart component when resizing the scree ...

Ways to display two tabs in reactjs

My typical app is running smoothly, but I am trying to implement a feature where it launches two tabs upon opening. The first tab should open with a normal path of '/' and the second one with a path of '/board'. Here's an example: ...

Utilizing Vue components within SweetAlert2 content

I have a couple of basic Sweetalert2 pop-up modals within a Vue project. My goal is to incorporate a custom component into one of the alerts. For instance: <template> <h1>Greetings {{name}}</h1> </template> <script> module. ...

Accessing the Nuxt store in module mode from a JavaScript file

I'm facing a challenge with my Nuxt app, where I have an AuthService within a namespaced store. The issue lies in how to import the store into my AuthService so that I can commit mutations from there. In some examples I've seen, the store is imp ...

Setting up a React state with an Object using Typescript: A step-by-step guide

As someone who is new to TypeScript, I have a solid understanding of how to set up an interface for certain types. However, I am struggling to comprehend how the same concept would apply to an Object type. interface MyProps { defaultgreeting: 'He ...

Leverage vue-i18n in library plugin mode

Greetings! I've been working on creating a series of UI components using the library mode in vue3/rollup. The entire setup is compiled into a local npm package to be easily utilized across multiple projects. My library project consists of numerous .v ...

Animating number counters in Ionic 3 with Angular

Displayed in my user interface is a simple number, nothing fancy. <ion-label>{{myCount}}</ion-label> Next to the number, there is a button labeled "reset." When pressed, the counter resets to 0. This functionality works well with a basic func ...

Modifying state within reducers is not allowed

Encountered the following error while using @ngrx/store: index.js?4b23:19 State mutation is prohibited inside of reducers. (anonymous) @ index.js?4b23:19 (anonymous) @ index.ts:54 rootReducer @ index.ts:70 _initialStateFactory @ ng2.ts?2a33:24 AppModule ...

What is the best way to store a logged-in user's email in a React

I have implemented a login API and I would like to save the email of the logged-in user in the state or another variable, so that I can display it in the header after successful login. The user's email is located in the data.email. The following code ...

Tips for integrating the react-financial-charts library into your React and JavaScript project

While exploring the react-financial-charts library, I discovered that it is written in TypeScript (TS). Despite my lack of expertise in TypeScript, I am interested in using this library in my React+JS project due to its active contributions. However, I hav ...

How can we reduce the size of a JSON object in Typescript before sending it to the client?

Currently, I am faced with a common challenge. I have a database object that is a standard JS object originating from a document database, and my goal is to transmit this object to the client. However, certain fields contain sensitive information that shou ...

Using an Angular 2 filter pipe to search across multiple values

Currently, I am working on implementing a filter for a table of users using pipes. While I have successfully made it work for filtering based on one selection, I am now attempting to extend this functionality to accommodate multiple selections. It's i ...