1+ ( function ( root , factory ) {
2+ //amd
3+ if ( typeof define === "function" && define . amd ) {
4+ define ( [ 'sprintf' ] , function ( sprintf ) {
5+ return factory ( sprintf . vsprintf ) ;
6+ } ) ;
7+
8+ //commonjs
9+ } else if ( typeof module === "object" && module . exports ) {
10+ module . exports = factory ( require ( 'sprintf-js' ) . vsprintf ) ;
11+
12+ //global
13+ } else {
14+ root . gettext = factory ( window . vsprintf ) ;
15+ }
16+ } ( this , function ( vsprintf ) {
17+
18+ function Translator ( translations ) {
19+ this . dictionary = { } ;
20+ this . plurals = { } ;
21+ this . domain = null ;
22+
23+ if ( translations ) {
24+ this . loadTranslations ( translations ) ;
25+ }
26+ }
27+
28+ Translator . prototype = {
29+ loadTranslations : function ( translations ) {
30+ var domain = translations . domain || '' ;
31+
32+ if ( this . domain === null ) {
33+ this . domain = domain ;
34+ }
35+
36+ if ( this . dictionary [ domain ] ) {
37+ mergeTranslations ( this . dictionary [ domain ] , translations . messages ) ;
38+ return this ;
39+ }
40+
41+ var plural = translations [ 'plural-forms' ] . split ( ';' , 2 ) ;
42+
43+ this . plurals [ domain ] = {
44+ count : parseInt ( plural [ 0 ] . replace ( 'nplurals=' , '' ) ) ,
45+ code : plural [ 1 ] . replace ( 'plural=' , 'return ' ) + ';'
46+ } ;
47+
48+ this . dictionary [ domain ] = translations . messages ;
49+
50+ return this ;
51+ } ,
52+
53+ defaultDomain : function ( domain ) {
54+ this . domain = domain ;
55+
56+ return this ;
57+ } ,
58+
59+ gettext : function ( original ) {
60+ return this . dpgettext ( this . domain , null , original ) ;
61+ } ,
62+
63+ ngettext : function ( original , plural , value ) {
64+ return this . dnpgettext ( this . domain , null , original , plural , value ) ;
65+ } ,
66+
67+ dngettext : function ( domain , original , plural , value ) {
68+ return this . dnpgettext ( domain , null , original , plural , value ) ;
69+ } ,
70+
71+ npgettext : function ( context , original , plural , value ) {
72+ return this . dnpgettext ( this . domain , context , original , plural , value ) ;
73+ } ,
74+
75+ pgettext : function ( context , original ) {
76+ return this . dpgettext ( this . domain , context , original ) ;
77+ } ,
78+
79+ dgettext : function ( domain , original ) {
80+ return this . dpgettext ( domain , null , original ) ;
81+ } ,
82+
83+ dpgettext : function ( domain , context , original ) {
84+ var translation = getTranslation ( this . dictionary , domain , context , original ) ;
85+
86+ if ( translation !== false && translation [ 0 ] !== '' ) {
87+ return translation [ 0 ] ;
88+ }
89+
90+ return original ;
91+ } ,
92+
93+ dnpgettext : function ( domain , context , original , plural , value ) {
94+ var index = getPluralIndex ( this . plurals , domain , value ) ;
95+ var translation = getTranslation ( this . dictionary , domain , context , original ) ;
96+
97+ if ( translation [ index ] && translation [ index ] !== '' ) {
98+ return translation [ index ] ;
99+ }
100+
101+ return ( index === 0 ) ? original : plural ;
102+ } ,
103+
104+ __ : function ( original ) {
105+ return format (
106+ this . gettext ( original ) ,
107+ Array . prototype . slice . call ( arguments , 1 )
108+ ) ;
109+ } ,
110+
111+ n__ : function ( original , plural , value ) {
112+ return format (
113+ this . ngettext ( original , plural , value ) ,
114+ Array . prototype . slice . call ( arguments , 3 )
115+ ) ;
116+ } ,
117+
118+ p__ : function ( context , original ) {
119+ return format (
120+ this . pgettext ( context , original ) ,
121+ Array . prototype . slice . call ( arguments , 2 )
122+ ) ;
123+ } ,
124+
125+ d__ : function ( domain , original ) {
126+ return format (
127+ this . dgettext ( domain , original ) ,
128+ Array . prototype . slice . call ( arguments , 2 )
129+ ) ;
130+ } ,
131+
132+ dp__ : function ( domain , context , original ) {
133+ return format (
134+ this . dgettext ( domain , context , original ) ,
135+ Array . prototype . slice . call ( arguments , 3 )
136+ ) ;
137+ } ,
138+
139+ np__ : function ( context , original , plural , value ) {
140+ return format (
141+ this . npgettext ( context , original , plural , value ) ,
142+ Array . prototype . slice . call ( arguments , 4 )
143+ ) ;
144+ } ,
145+
146+ dnp__ : function ( domain , context , original , plural , value ) {
147+ return format (
148+ this . dnpgettext ( domain , context , original , plural , value ) ,
149+ Array . prototype . slice . call ( arguments , 5 )
150+ ) ;
151+ }
152+ } ;
153+
154+ function getTranslation ( dictionary , domain , context , original ) {
155+ context = context || '' ;
156+
157+ if ( ! dictionary [ domain ] || ! dictionary [ domain ] [ context ] || ! dictionary [ domain ] [ context ] [ original ] ) {
158+ return false ;
159+ }
160+
161+ return dictionary [ domain ] [ context ] [ original ] ;
162+ }
163+
164+ function getPluralIndex ( plurals , domain , value ) {
165+ if ( ! plurals [ domain ] ) {
166+ return value == 1 ? 0 : 1 ;
167+ }
168+
169+ if ( ! plurals [ domain ] . fn ) {
170+ plurals [ domain ] . fn = new Function ( 'n' , plurals [ domain ] . code ) ;
171+ }
172+
173+ return plurals [ domain ] . fn . call ( this , value ) ;
174+ }
175+
176+ function mergeTranslations ( translations , newTranslations ) {
177+ for ( var context in newTranslations ) {
178+ if ( ! translations [ context ] ) {
179+ translations [ context ] = newTranslations [ context ] ;
180+ continue ;
181+ }
182+
183+ for ( var original in newTranslations [ context ] ) {
184+ translations [ context ] [ original ] = newTranslations [ context ] [ original ] ;
185+ }
186+ }
187+ }
188+
189+ function format ( text , args ) {
190+ if ( ! args . length ) {
191+ return text ;
192+ }
193+
194+ if ( args [ 0 ] instanceof Array ) {
195+ return vsprintf ( text , args [ 0 ] ) ;
196+ }
197+
198+ return vsprintf ( text , args ) ;
199+ }
200+
201+ return Translator ;
202+ } ) ) ;
0 commit comments