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 for %s parameters #56

Open
nkostadinov opened this issue Oct 31, 2017 · 6 comments
Open

Support for %s parameters #56

nkostadinov opened this issue Oct 31, 2017 · 6 comments

Comments

@nkostadinov
Copy link

Currently I'm using translations from a PHP application to translate. All the strings there are defined in the format: 'You have %s unread article in %s feed". How can I provide an array of params to be replaces in this order in the translated strings.

@kemar
Copy link
Contributor

kemar commented Oct 31, 2017

Looks like this is not a vue-gettext issue.

@kemar kemar closed this as completed Oct 31, 2017
@nkostadinov
Copy link
Author

Well since its a recommended way to use gettext I think this should be handled by the component. A lot of sites and apps use this format, don't believe they will create new strings just to be compatible.

https://www.gnu.org/software/gettext/manual/html_node/Preparing-Strings.html

@nkostadinov
Copy link
Author

BTW I wrote my own component (which relies on vue-gettext) that handles this and interpolation with Vue components. But is should be nice to be part of the extension.

@kemar
Copy link
Contributor

kemar commented Oct 31, 2017

Can I take a look at the code of your component?

@nkostadinov
Copy link
Author

Sure :) . The components relies on the following markup to work:

<v-translate>
  For advanced filtering, please go to %s
  <a slot="s1" class="link" @click="$emit('navigate', 'SettingsRules')" v-translate>Rules</a>
</v-translate>

All the %s are replaced with the corresponding slots based on their index e.g. the first %s is replaces with slot "s1", the second "%s" is replaced with slot "s2" and so on. The base text string to be translated is taken from the Text node inside the component tag without the slot(s).

This is the code for the component. It is used only in this project and have not tested it thoroughly but seem to work ok.

<script>
  export default {
    name: 'v-translate',
    functional: true,
    render (createElement, data) {
      let slots = data.slots()
      let text = slots.default[0].text.trim()
      let translated = data.parent.$gettext(text)

      let tag = data.props.tag ? data.props.tag : 'span'
      let childs = []

      let i = 0
      let offset = 0
      translated.replace('%s', (str, idx) => {
        i++
        if(idx > 0) {
          let text = translated.substring(offset, idx)
          childs.push(data.parent._v(text))
        }
        childs.push(slots['s' + i][0])
      })

      return createElement(tag, childs)
    }
  }
</script>

@kemar
Copy link
Contributor

kemar commented Nov 2, 2017

Ok I may work on it when I have free time. But that's not guaranteed.

@kemar kemar reopened this Nov 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants