Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support scoped style while translating string with HTML tags. #75

Closed
myst729 opened this issue Oct 23, 2016 · 4 comments
Closed

Support scoped style while translating string with HTML tags. #75

myst729 opened this issue Oct 23, 2016 · 4 comments

Comments

@myst729
Copy link
Contributor

myst729 commented Oct 23, 2016

This is not an actual issue, but a feature request indeed.

Sometimes when we provide a piece of information, we would like to insert hyper links as well. For example:

<p>You can <a href="http://example.com">refund the ticket</a> until 30 minutes from departure.</p>

Following code would make it i18n ready:

const locales = {
  en: {
    refundInfo: 'You can <a href="{refundLink}">refund the ticket</a> until 30 minutes from departure.'
  },
  cn: {
    refundInfo: '办理<a href="{refundLink}">退票</a>手续的截止时间为发车前 30 分钟。'
  }
}
<p v-html="$t('refundInfo', {refundLink: 'http://example.com'})"></p>

This looks good. But when we work with <style scoped> in *.vue, it won't apply the styles. Because all selectors extracted from <style scoped> are restricted with a special attribute, which equals to the component's $options._scopeId. So do all elements in the template.

To deal with it, my current approach is like this:

const locales = {
  en: {
    refundInfo: 'You can <a {_scopeId} href="{refundLink}">refund the ticket</a> until 30 minutes from departure.'
  },
  cn: {
    refundInfo: '办理<a {_scopeId} href="{refundLink}">退票</a>手续的截止时间为发车前 30 分钟。'
  }
}
<p v-html="$t('refundInfo', {refundLink: 'http://example.com', _scopeId: $options._scopeId})"></p>

Or one step further:

<p v-html="$scopedT('refundInfo', {refundLink: 'http://example.com'})"></p>
methods: {
  $scopedT (key, obj) {
    obj._scopeId = this.$options._scopeId
    return this.$t(key, obj)
  }
}

This is annoying:

  • I need a wrapper for $t to inject _scopeId.
  • I need to hard code a {_scopeId} field for every tag in my i18n strings, too ugly.

I'm wondering whether the scope id injection could be a built-in feature? Either with or without a switch.

@myst729
Copy link
Contributor Author

myst729 commented Oct 24, 2016

Maybe another approach is to wrap v-html directive?

@myst729
Copy link
Contributor Author

myst729 commented Nov 6, 2016

Looks like can be indirectly solved by this new feature? http://vue-loader.vuejs.org/en/features/css-modules.html

@yyx990803
Copy link

Even without CSS modules you can wrap the v-html content in a div with a unique class and use a separate non-scoped <style> tag to get the normal cascading behavior.

@kazupon
Copy link
Owner

kazupon commented Dec 8, 2016

Close (say the below alternative)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants