/* ********************************************************************* This source file is a part of the standard library of Scol For the latest info, see http://www.scolring.org Copyright (c) 2013 Stephane Bisaro aka Iri based on a work of Sylvain Huet and Sebastien Deneux This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA, or go to http://www.gnu.org/copyleft/lesser.txt ********************************************************************* */ /* * A way to internationalize an application * See http://redmine.scolring.org/projects/tutorials/wiki/Scol_usage * for more informations */ /*! \file loc.pkg * \author Scol team * \version 0.2 * \copyright GNU Lesser General Public License 2.0 or later * \brief Localization API * * \details This API provides a method to internationalize an application. * * Translators and/or contributors can easily give a translation. * * For an application, a sub directory is created. Within are the translations. * Each translations are named myapp..lang. For example : *
myapp.english.lang
myapp.french.lang
myapp.russian.lang
myapp.spanish.lang
...
* In each file, a list of reference and translations : *
REFERENCE_1 word_translated
REFERENCE_2 string translated
REFERENCE_3 substring_1 ## substring_2 ## substring_3
...
* Each ## will be replaced by a provided parameter, if needed. You can change * this symbol as you want, see below. * * - loc_init is the function to load the language file for a given user. * - loc_get is the function for a simple translation. * - loc_gets loc are the functions for a translation with parameters * * See http://redmine.scolring.org/projects/tutorials/wiki/How_to_localize_a_program_in_Scol * **/ var locDefaultlang = "english";; typeof locLang = S;; var locOk = 0;; var locUnknown = "*";; var locDefaultParam = "##";; var locDefaultParamSize = 2;; typeof loc_tabLoc = tab [[S S] r1];; /*fonction de hachage*/ fun loc_hachage (ref)= let ((nth_char ref 0)+(nth_char ref 1))&255 -> x in if x == nil then 0 else x;; fun loc_check ()= locOk;; fun loc_rebuild (list)= if list == nil then nil else let list -> [a b] in if b == nil then a :: nil else a :: " " :: loc_rebuild b;; fun loc_loadloc2 (list)= if list == nil then 0 else let list -> [[a b] n] in ( if (nth_char a 0) != '# then //' let loc_hachage a -> i in set loc_tabLoc.i = [a strcatn loc_rebuild b] :: loc_tabLoc.i else nil; loc_loadloc2 n );; fun loc_loadloc (file, lang)= let _checkpack strcatn file :: "." :: lang :: ".lang" :: nil -> pFile in if pFile == nil then let _checkpack strcatn file :: "." :: locDefaultlang :: ".lang" :: nil -> pFileDefault in if nil == pFileDefault then ( set locOk = 0; nil ) else ( set loc_tabLoc = mktab 256 nil; set locLang = locDefaultlang; loc_loadloc2 strextr _getpack pFileDefault ) else ( set loc_tabLoc = mktab 256 nil; loc_loadloc2 strextr _getpack pFile );; fun loc_findloc (list, s)= if list == nil then locUnknown else let list -> [[a b] n] in if !strcmp a s then b else loc_findloc n s;; /*! \brief Return the translation of a given reference. * If the reference is not found, the "unknown string" (see below) is returned * * \ingroup loc * \see loc_setUnknown * \see loc_getUnknown * * Prototype: fun [S] S * * \param S : a reference * * \return S : the appropriate string **/ fun loc_get (ref)= if loc_check then let loc_hachage ref -> i in loc_findloc loc_tabLoc.i ref else locUnknown;; fun loc_gets2 (s, listParam)= if listParam == nil then // the same thing than loc_loc s else let strfind locDefaultParam s 0 -> i in if i == nil then s else strcat substr s 0 i strcat hd listParam loc_gets2 substr s i+locDefaultParamSize strlen s tl listParam;; /*! \brief Return the translation of a given reference. * If the reference is not found, the "unknown string" (see below) is returned. * * \ingroup loc * \see loc_setUnknown * \see loc_getUnknown * * Prototype: fun [S [S r1]] S * * \param S : a reference * \param [S r1] : a list of strings to replace parameters (in the same order) * * \return S : the appropriate string **/ fun loc_gets (ref, listParam)= if loc_check then let loc_get ref -> s in if listParam == nil then // the same thing than loc_loc s else loc_gets2 s listParam else locUnknown;; /*! \brief Initialize and load the language file * The language is the favorite language defined in the Scol settings * by the client. * If not found, the default language is taken off. * \ingroup loc * \see loc_setUnknown * \see loc_initEx * * Prototype: fun [S] I * * \param S : a base pathname (like "tutorials/lang/mylocapp") i.e. * without language name nor extension (see above) * * \return I : 0 if success, nil if error. **/ fun loc_init (file)= set locOk = 1; set locLang = _getress "DefaultLanguage"; loc_loadloc file locLang;; /*! \brief Initialize and load the language file * The language is forced (the Scol settings choice is skipped) * If not found, the default language is taken off. * \ingroup loc * \see loc_setLangDefault * * Prototype: fun [S S] I * * \param S : a base pathname (like "tutorials/lang/mylocapp") i.e. * without language name nor extension (see above) * \param S : the language * * \return I : 0 if success, nil if error. **/ fun loc_initEx (file, lang)= set locOk = 1; set locLang = lang; loc_loadloc file locLang;; /*! \brief Clear the current localisation. * It should be used before to reload a language file. * \ingroup loc * \see loc_init * \see loc_initEx * * Prototype: fun [] I * * \return I : always 0. **/ fun loc_clear ()= set locOk = 0; set loc_tabLoc = nil; 0;; /*! \brief Set the returned string when no reference is found. * By default, it is "*". * * \ingroup loc * Prototype: fun [S] S * * \param S : a string * * \return S : the same string. **/ fun loc_setUnknown (str)= set locUnknown = str;; /*! \brief Get the returned string when no reference is found. * By default, it is "*". * * \ingroup loc * Prototype: fun [] S * * \return S : this string. **/ fun loc_getUnknown ()= locUnknown;; /*! \brief Set the language default. By default, it is "english". * It should be only called before loc_init or loc_initEx and after, if * any, loc_clear. * * The default language is used when other langauage files are unknown or * invalid. Thus, the default language file should always exist. * * \ingroup loc * Prototype: fun [S] S * * \param S : a default language * * \return S : the same string or nil if error (loc_init alreday called * or the string is nil). **/ fun loc_setLangDefault (str)= if ((!loc_check) || (str != nil)) then set locDefaultlang = str else nil;; /*! \brief Get the default language. * By default, it is "english". * * \ingroup loc * Prototype: fun [] S * * \return S : this string. **/ fun loc_getDefaultLang ()= locDefaultlang;; /*! \brief Get the language used by this client. * * \ingroup loc * Prototype: fun [] S * * \return S : this language. **/ fun loc_getLang ()= locLang;; /*! \brief Return the default symbol for parameters. * * By default, it is "##". * * \ingroup loc * Prototype: fun [] S * * \return S : this symbol. **/ fun loc_getDefaultParamSymbol ()= locDefaultParam;; /*! \brief Set the default symbol for parameters. The language file should * not be loaded yet. * * By default, it is "##". * * \ingroup loc * Prototype: fun [S] S * * \return S : the same string or nil if an error occurs. **/ fun loc_setDefaultParamSymbol (str)= if ((!loc_check) || (str != nil)) then ( set locDefaultParam = str; set locDefaultParamSize = strlen locDefaultParam; str ) else nil;; /*! \brief Return if a language is currently loaded. * * \ingroup loc * Prototype: fun [] I * * \return I : 1 : loaded, 0 : not loaded **/ fun loc_isLoaded ()= loc_check;; /* For compatibilty, these following functions are set : */ fun startloc (file) = loc_init file;; fun loc (s)= loc_get s;; fun strloc (s, l)= loc_gets s l;; fun findloc (l, s)= loc_findloc l s;; fun loadloc (file, lang)= loc_loadloc file lang;;