Spittal / vue-i18n-extract

Manage vue-i18n localization with static analysis
https://pixari.github.io/vue-i18n-extract/#what-is-it
MIT License
313 stars 86 forks source link

Add an option to allow to specify name of function #140

Closed vricosti closed 2 years ago

vricosti commented 3 years ago

I am not satisfied by the default behavior of vue-i18n and when a language has an empty translation I would like to display the key. So I wrote the following plugin:

// Declare a plugin to add a global method $T() available inside our vuejs app
export default {
  install(Vue) {

    // define an instance method
    Vue.prototype.$T = function (key) {
      // When translation is empty we take the key
      let trans = this.$t(key);
      if (!trans) {
        trans = key;
      }
      return trans;
    }
  }
}

So I am declaring a $T function but in this case vue-18n-extract does not work, would it be possible to add an option methodName for instance where we could specify the name of the method ?

The change would be something like:

function extractMethodMatches (file: SimpleFile, methodName: string): I18NItemWithBounding[] {
  const defMethodRegExp = /(?:[$ ."'`]t[cm]?)\(\s*?(["'`])((?:[^\\]|\\.)*?)\1/g;
  let methodRegExp = defMethodRegExp;
  if (methodName) {
    methodRegExp = new RegExp(defMethodRegExp.toString().replace('t',methodName).slice(1,-1));
  }

  return [ ...getMatches(file, methodRegExp, 2) ];
}

I haven't tested yet but I have a branch here: feat/AddOptionToSpecifyMethodName

JamesBoon commented 3 years ago

Maybe this would be a solution:

// "methodNamesRegExp" must be validated not to contain "(", ")" or ":" ... and more?
const methodNamesRegExp = 't[cm]?';
// default RegExp: /(?:[$\s.:"'`+\(\[\{]t[cm]?)\(\s*?(["'`])((?:[^\\]|\\.)*?)\1/g;
const regExpStr = `(?:[$\\s.:"'\`+\\(\\[\\{]${methodNamesRegExp})\\(\\s*?(["'\`])((?:[^\\\\]|\\\\.)*?)\\1`;
const methodRegExp = new RegExp(regExpStr, "g");

The methodNamesRegExp would be the parameter passed to extractMethodMatches. Allowing a custom RegExp-part is a mighty thing - and error-prone. Maybe the mentioned RegExp validation is not enough.

Spittal commented 2 years ago

Yeah we've grappled with adding custom Regex's before and because @pixari and I only have a small amount of time to maintain this project we've decided that it's best if we focus the scope of this project to the most common use cases of i18n, sorry! But feel free to fork the project!

hl037 commented 1 month ago

Hi, @Spittal I understand your point about focusing on the most common use cases, and I would also hear you that this change may open a pandora box : "if this feature pass, why this one won't ?". However, I think the ability to allow other function names is not a huge addition, an would benefit a lot of projects. It would allow wrappers, and also defining keys elsewhere, like with a noop function. The other point is that the current regex is quite unreadable, and having a variable to represent the part matching the name may be cleaner.

I myself really need this feature, so if I may, I would like to ask this question : if I make a PR with unit tests, would it be rejected without review on the principle stated above, or would you rethink about it and may be reconsider your position on the subject ?

(I also know I can fork, but well, I may have the time to maintain a small modification, but not the time to keep track of changes on the mother project...)