Vue-i18n: Linked Pluralization

Created on 12 Jul 2017  路  15Comments  路  Source: kazupon/vue-i18n

using vue 2.2.6 and vue-i18n 6.1.1

Can this be done?
I'm trying to do a "action string"
ex: User added 1 photo - if there are more photos i want to show "photos"

i have this

export default {
    en: {
        actions: {
            photo: 'added {count} @:photo'
        },
        photo: 'photo | photos'
    }
}

How do I pass the count parameter in a pluralized linked locale message ?

High Feature

Most helpful comment

While I agree with @amertum's point on duplicates not being a bad thing ,I don't fully agree with the points on maintenance and other languages causing problems

In my particular use case:

// en.js
export default {
  name:            'Name',
  collection:      'Collection | Collections',
  file:            'File | Files',

  collection_name: '@:collection @:name',
  file_name:       '@:file @:name'
};

the names collection and file are used in a lot of different instances (this is only a subset) and don't change between instances. By using the _linked locale messages_ feature, we can prevent spelling errors by only having to enter the "general" terms once.

Next to this, additional translations can be a lot smaller by using the fallback language for the combined messages like follows:

// nl.js
export default {
  name:            'Naam',
  collection:      'Collectie | Collecties',
  file:            'Bestand | Bestanden',

  // use English fallback for collection_name & file_name
};

If the combined message doesn't fit well in the other language, the _linked locale message_ can be updated on a per-instance basis:

// nl.js
export default {
  name:            'Naam',
  collection:      'Collectie | Collecties',
  file:            'Bestand | Bestanden',

  // use English fallback for collection_name

  // Override file_name for Dutch
  file_name: '@:name van het @:file'
};

First syntax ideas:

@:apple
@:apple(1)
@:apple(10, { count: 10 })

OR:

@:apple
@:apple:1
@:apple:10:{"count": 10}

@kazupon What are your thoughts on the matter? Would you be interested in figuring out an addition to the _linked locale message_ syntax in order to support pluralization?

All 15 comments

up ?

IMHO, you should not use "Linked message" (using @:) as it provides more complexity over usefulness. Having duplicated string in i18n messages is normal and provide flexibility and room for change.

@amertum just to be sure i understand what you are saying

I should have two strings in my locales file, one for singular and one for plural and to have something like {{ data.length > 0 ? $t('singular') : $t('plural') }} ?

What I meant was :

export default {
  en: {
    actions: {
      photo: 'added 0 photo | added 1 photo | added {0} photos'
    }
  }
}
{{ $tc('actions.photo', count, [count]) }}

I agree that the documentation should be more prolixe about this. But it is clear in the source code ^^.

I have a slightly different, but related problem. I have a set of common words together with their plural form in a common.json file. Then I have components that come with their own loca file. What I would like to do is have the component loca link to a common word in its singular form. Can this be done? Currently, if my common file has something like "common": { "artist": "Artist | Artists" } and the component loca "artist": "@:common.artist" and I use $t("artist"), I get Artist | Artists.

@smares Duplicate strings is not a bad thing in messages. You should not seek for hard normalization. It will be difficult to maintain and it does not handle well with other languages pluralizations.

While I agree with @amertum's point on duplicates not being a bad thing ,I don't fully agree with the points on maintenance and other languages causing problems

In my particular use case:

// en.js
export default {
  name:            'Name',
  collection:      'Collection | Collections',
  file:            'File | Files',

  collection_name: '@:collection @:name',
  file_name:       '@:file @:name'
};

the names collection and file are used in a lot of different instances (this is only a subset) and don't change between instances. By using the _linked locale messages_ feature, we can prevent spelling errors by only having to enter the "general" terms once.

Next to this, additional translations can be a lot smaller by using the fallback language for the combined messages like follows:

// nl.js
export default {
  name:            'Naam',
  collection:      'Collectie | Collecties',
  file:            'Bestand | Bestanden',

  // use English fallback for collection_name & file_name
};

If the combined message doesn't fit well in the other language, the _linked locale message_ can be updated on a per-instance basis:

// nl.js
export default {
  name:            'Naam',
  collection:      'Collectie | Collecties',
  file:            'Bestand | Bestanden',

  // use English fallback for collection_name

  // Override file_name for Dutch
  file_name: '@:name van het @:file'
};

First syntax ideas:

@:apple
@:apple(1)
@:apple(10, { count: 10 })

OR:

@:apple
@:apple:1
@:apple:10:{"count": 10}

@kazupon What are your thoughts on the matter? Would you be interested in figuring out an addition to the _linked locale message_ syntax in order to support pluralization?

if I'm reading this, I guess I'm needing this 馃憤

If this isn't possible I think linking and plural is kind of redundant as you still need to type the full sentence where you would otherwise just use a mix of plural and linked. Yes the functionals are usefull on their own but if you could mix them it would be event better.

Hello all,

Is there any update on this issue?

This would be useful. The case that brought me here was to create translation files for time-stamping items such as messages. i.e. received {count} _minutes_ ago
where minutes could be replaced with _hours_ or _days_ for example.
Unless someone has a better solution for this :)

Hi there. I am not familiar with this projects label structure, what does the Status: Ready label indicate?

we supported in Vue I18n v9.
please try it!

we supported in Vue I18n v9.
please try it!

How do I exactly try v9? I only see v8 on github

Was this page helpful?
0 / 5 - 0 ratings