[F @sdZddlZddlZddlZddlZddlZddlZddlZddlm Z dddddd d d d d ddg Z ej j ej ddZddZddZGdddZGdddeZdddddZiZdddddddZddddd Ziaiadaddd Zddd Zddd Zd!d Zd"d#Zd$d Zd%d&Z d'dZ!d(d)Z"d*dZ#d+d,Z$eZ%dS)-aInternationalization and localization support. This module provides internationalization (I18N) and localization (L10N) support for your Python programs by providing an interface to the GNU gettext message catalog library. I18N refers to the operation by which a program is made aware of multiple languages. L10N refers to the adaptation of your program, once internationalized, to the local language and cultural habits. N)ENOENTNullTranslationsGNUTranslationsCatalogfind translationinstall textdomainbindtextdomaindgettext dngettextgettextngettextZsharelocalecsddlddl}|jtj|j}yfdd|D}Wn!|jk rstdYnX|rtdn|jdd}|jd d }t j d j d |}t j d fdddg}x|D]}|dkr$|j dq|dkr~t |dkrQtdnj |j}|dd|7Es zc2py..z;plural forms expression error, maybe unbalanced parenthesisz*plural forms expression could be dangerousz&&z and z||z or z\!([^=])z not \1z(.*?)\?(.*?):(.*)cs8d|jd|jdj|jdfS)Nz(%s if %s else %s)r)groupsub)r)exprreplrrrVszc2py..repl()rz%unbalanced parenthesis in plural formz(%s)zlambda n: int(%s)r")rtokenizegenerate_tokensioStringIOreadline TokenError ValueErrorreplacerecompilerappendlenpopeval)pluralr#tokensZdangerstackcsr)rrrrc2py=s4    r6c Cstj|}d}d }d }d}|jd}|dkro||d}|d|}||O}nd}|jd}|dkr||d}|d|}||O}nd}|jd}|dkr ||d}|d|}||O}nd}|} g} x~t|dD]l} | |@s.| } | |@r\| |7} n| |@rs| |7} n| |@r| |7} n| j| q.q.W| j| S) Nrrr@r._rr)rZ normalizerranger-reverse) locZCOMPONENT_CODESETZCOMPONENT_TERRITORYZCOMPONENT_MODIFIERmaskposZmodifiercodesetZ territoryZlanguageZretivalrrr _expand_langosJ           rCc@seZdZdddZddZddZdd Zd d Zd d ZddZ ddZ ddZ ddZ ddZ dddZdS)rNcCsDi|_d|_d|_d|_|dk r@|j|ndS)N)_info_charset_output_charset _fallback_parse)selffprrr__init__s      zNullTranslations.__init__cCsdS)Nr)rIrJrrrrHszNullTranslations._parsecCs)|jr|jj|n ||_dS)N)rG add_fallback)rIfallbackrrrrLs zNullTranslations.add_fallbackcCs|jr|jj|S|S)N)rGr )rImessagerrrr s zNullTranslations.gettextcCs|jr|jj|S|S)N)rGlgettext)rIrNrrrrOs zNullTranslations.lgettextcCs7|jr|jj|||S|dkr/|S|SdS)Nr)rGr)rImsgid1msgid2rrrrrs   zNullTranslations.ngettextcCs7|jr|jj|||S|dkr/|S|SdS)Nr)rG lngettext)rIrPrQrrrrrRs   zNullTranslations.lngettextcCs|jS)N)rD)rIrrrinfoszNullTranslations.infocCs|jS)N)rE)rIrrrcharsetszNullTranslations.charsetcCs|jS)N)rF)rIrrroutput_charsetszNullTranslations.output_charsetcCs ||_dS)N)rF)rIrTrrrset_output_charsetsz#NullTranslations.set_output_charsetcCsddl}|j|jdsz(GNUTranslations._parse..z4Iz>IIzBad magic numberzFile is corrupt ascii:rz content-typezcharset=z plural-forms;zplural=s)structunpackgetattr_catalogr1readr.LE_MAGICBE_MAGICOSErrorr;splitencodedecodestriplowerrDrEr6str enumerate) rIrJrifilenameZcatalogZbufZbuflenmagicversionZmsgcountZ masteridxZtransidxZiirAZmlenZmoffZmendZtlenZtoffZtendmsgtmsgZlastkkZb_itemitemvr1rTrPrQrrrrrHsj    % % # #             zGNUTranslations._parsecCsst}|jj||}||krG|jrC|jj|S|S|jr`|j|jS|jtjS)N) objectrkgetrGrOrFrqrgetpreferredencoding)rIrNmissingr{rrrrO1s    zGNUTranslations.lgettextc CsyL|j||j|f}|jr8|j|jS|jtjSWnEtk r|jr{|jj|||S|dkr|S|SYnXdS)Nr) rkr1rFrqrrKeyErrorrGrR)rIrPrQrr{rrrrR<s    zGNUTranslations.lngettextcCsKt}|jj||}||krG|jrC|jj|S|S|S)N)rrkrrGr )rIrNrr{rrrr Js   zGNUTranslations.gettextc Cssy |j||j|f}WnLtk rn|jrO|jj|||S|dkrd|}n|}YnX|S)Nr)rkr1rrGr)rIrPrQrr{rrrrSs      zGNUTranslations.ngettextN) r\r]r^rmrnrHrOrRr rrrrrrs  M  Fc Cs|dkrt}n|dkrg}x9d D]1}tjj|}|r.|jd}Pq.q.Wd|kr|jdqng}xA|D]9}x0t|D]"}||kr|j|qqWqW|rg} nd} x|D]}|dkrPntjj||dd|} tjjd|dd|} tjj | rk|rd| j| qk| Sntjj | r|r| j| q| SqqW| S) NLANGUAGELC_ALL LC_MESSAGESLANGrfCz%s.moz/usr/share/locale-langpack)rzLC_ALLz LC_MESSAGESr) _default_localedirosenvironrrpr-rCpathjoinexists) domain localedir languagesallZenvarrBZnelangsZlangZnelangresultmofileZ mofile_lprrrrasD           c Cs|dkrt}nt|||dd}|sU|r@tSttd|nd}x|D]}|tjj|f} tj | } | dkrt |d} tj | || } WdQXnt j | } |r| j |n|dkr| }qb|j| qbW|S)NrTz$No translation file found for domainrb)rrrrorrrabspath _translationsropen setdefaultcopyrVrL) rrrZclass_rMr@ZmofilesrrkeytrJrrrrs*    !  cCs,t||ddd|}|j|dS)NrMTr@)rr)rrr@r[rrrrrsZmessagescCs|dk r|antS)N)_current_domain)rrrrr s  cCs)|dk r|t| s<&T  2 *F5