@@ -5,7 +5,7 @@ import Path from './path'
55
66/**
77 * extend
8- *
8+ *
99 * @param {Vue } Vue
1010 * @return {Vue }
1111 */
@@ -69,13 +69,13 @@ export default function (Vue) {
6969
7070 function translate ( getter , lang , fallback , key , params ) {
7171 let res = null
72- res = interpolate ( getter ( lang ) , key , params )
73- if ( res ) { return res }
72+ res = interpolate ( getter ( lang ) , key , params )
73+ if ( res ) { return res }
7474
75- res = interpolate ( getter ( fallback ) , key , params )
75+ res = interpolate ( getter ( fallback ) , key , params )
7676 if ( res ) {
7777 if ( process . env . NODE_ENV !== 'production' ) {
78- warn ( 'Fall back to translate the keypath "' + key + '" with "'
78+ warn ( 'Fall back to translate the keypath "' + key + '" with "'
7979 + fallback + '" language.' )
8080 }
8181 return res
@@ -102,10 +102,23 @@ export default function (Vue) {
102102 return this . $options . locales [ lang ]
103103 }
104104
105+ function getOldChoiceIndexFixed ( choice ) {
106+ return choice ? choice > 1 ? 1 : 0 : 1
107+ }
108+
109+ function getChoiceIndex ( choice , choicesLength ) {
110+ choice = Math . abs ( choice )
111+
112+ if ( choicesLength === 2 ) return getOldChoiceIndexFixed ( choice )
113+
114+ return choice ? Math . min ( choice , 2 ) : 0
115+ }
116+
105117 function fetchChoice ( locale , choice ) {
106118 if ( ! locale && typeof locale !== 'string' ) { return null }
107119 const choices = locale . split ( '|' )
108- choice = choice - 1
120+
121+ choice = getChoiceIndex ( choice , choices . length )
109122 if ( ! choices [ choice ] ) { return locale }
110123 return choices [ choice ] . trim ( )
111124 }
@@ -135,7 +148,6 @@ export default function (Vue) {
135148 */
136149
137150 Vue . tc = ( key , choice , ...args ) => {
138- if ( ! choice ) { choice = 1 }
139151 return fetchChoice ( Vue . t ( key , ...args ) , choice )
140152 }
141153
@@ -173,7 +185,6 @@ export default function (Vue) {
173185 Vue . prototype . $tc = function ( key , choice , ...args ) {
174186 if ( typeof choice !== 'number'
175187 && typeof choice !== 'undefined' ) { return key }
176- if ( ! choice ) { choice = 1 }
177188 return fetchChoice ( this . $t ( key , ...args ) , choice )
178189 }
179190
0 commit comments