kazupon / vue-i18n

:globe_with_meridians: Internationalization plugin for Vue.js
https://kazupon.github.io/vue-i18n/
MIT License
7.26k stars 860 forks source link

v-t directive if not path, support translate the innerText? #766

Open icai opened 4 years ago

icai commented 4 years ago

https://github.com/kazupon/vue-i18n/blob/adb5269e90ff945fda04c35652f826b82dd9cf8d/src/directive.js#L70

demo:

<h5 class="h3" v-t>Pages</h5>

if support that it can improve the code speed.

masongzhi commented 4 years ago

What you want is when v-t directive was empty, it should translate the keypath Pages like v-t="'Pages'"

You should change the following code:

<h5 class="h3">{{$t('Pages')}}</h5>
icai commented 4 years ago

@masongzhi , i know, but i want to support this syntax.

kleinfreund commented 4 years ago

I’m pretty sure this is not feasible. The following would be extremely confusing and complicated to parse correctly.

<h5 v-t>
  Text to translate
  <span>Some more markup</span>
</h5>

The way @masongzhi proposed is correct and works. What’s the use case for supporting a different way of marking translatable content?

icai commented 4 years ago

@kleinfreund only pure text, just an idea.

allenhwkim commented 4 years ago

I used this technique in Angular project, and it's really really helpful when you do code search. FYI, https://codeburst.io/low-verbosity-i18n-with-custom-vue-directives-c303a35c40c7

<form>
  <label v-t>login.username</label>
  <input type="text" v-t:placeholder="'login.user.placeholder'">
  <label v-t>login.password</label>
  <input type="password">
  <button v-t>login.submit</button>
</form>
crazynds commented 3 years ago

I’m pretty sure this is not feasible. The following would be extremely confusing and complicated to parse correctly.

<h5 v-t>
  Text to translate
  <span>Some more markup</span>
</h5>

The way @masongzhi proposed is correct and works. What’s the use case for supporting a different way of marking translatable content?

I also believe that this syntax must be accepted. And the example given is invalidated by the following fact that:

 <h5 v-t='text.translated'>
   <span>Some more markup</span>
</h5>

Does not work perfectly and deletes the span tag internally. But it can be circumvented using another tag:

 <h5>
   <span v-t='text.translated'></span>
   <span>Some more markup</span>
</h5>

I believe that the same thing can be done in the use that was mentioned above

What @icai talked about is that it would be interesting to add the syntax that by adding the empty v-t suffix within an element, it will take its content and translate it as if it were the way. Example below:

  <h5 v-t> 
     title.page.in.path
  </h5>

This code will have the same result as:

   <h5 v-t="'title.page.in.path'"> 
  </h5>

I believe that in the way that is being proposed in this issue it would be much more pleasant to read the code than to see the element tags that are often full of information.

HendrikJan commented 3 years ago

This looks like a feature that angular-translate supports for a long time: angular-translate directive I've used this a lot in Angularjs projects and really like it.

Writing {{ $(' and ') }} adds quite some cognitive load. The same goes for v-t="' and '".

Instead of writing

<span>{{ $('Accept') }}</span>

you can write

<span v-t>Accept</span>

I like this proposal.