...

Source file src/github.com/chai2010/gettext-go/gettext.go

Documentation: github.com/chai2010/gettext-go

     1  // Copyright 2013 ChaiShushan <chaishushan{AT}gmail.com>. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package gettext
     6  
     7  var (
     8  	DefaultLanguage string = getDefaultLanguage() // use $(LC_MESSAGES) or $(LANG) or "default"
     9  )
    10  
    11  type Gettexter interface {
    12  	FileSystem() FileSystem
    13  
    14  	GetDomain() string
    15  	SetDomain(domain string) Gettexter
    16  
    17  	GetLanguage() string
    18  	SetLanguage(lang string) Gettexter
    19  
    20  	Gettext(msgid string) string
    21  	PGettext(msgctxt, msgid string) string
    22  
    23  	NGettext(msgid, msgidPlural string, n int) string
    24  	PNGettext(msgctxt, msgid, msgidPlural string, n int) string
    25  
    26  	DGettext(domain, msgid string) string
    27  	DPGettext(domain, msgctxt, msgid string) string
    28  	DNGettext(domain, msgid, msgidPlural string, n int) string
    29  	DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string
    30  
    31  	Getdata(name string) []byte
    32  	DGetdata(domain, name string) []byte
    33  }
    34  
    35  // New create Interface use default language.
    36  func New(domain, path string, data ...interface{}) Gettexter {
    37  	return newLocale(domain, path, data...)
    38  }
    39  
    40  var defaultGettexter struct {
    41  	lang   string
    42  	domain string
    43  	Gettexter
    44  }
    45  
    46  func init() {
    47  	defaultGettexter.lang = getDefaultLanguage()
    48  	defaultGettexter.domain = "default"
    49  	defaultGettexter.Gettexter = newLocale("", "")
    50  }
    51  
    52  // BindLocale sets and queries program's domains.
    53  //
    54  // Examples:
    55  //	BindLocale(New("poedit", "locale")) // bind "poedit" domain
    56  //
    57  // Use zip file:
    58  //	BindLocale(New("poedit", "locale.zip"))          // bind "poedit" domain
    59  //	BindLocale(New("poedit", "locale.zip", zipData)) // bind "poedit" domain
    60  //
    61  // Use FileSystem:
    62  //	BindLocale(New("poedit", "name", OS("path/to/dir"))) // bind "poedit" domain
    63  //	BindLocale(New("poedit", "name", OS("path/to.zip"))) // bind "poedit" domain
    64  //
    65  func BindLocale(g Gettexter) {
    66  	if g != nil {
    67  		defaultGettexter.Gettexter = g
    68  		defaultGettexter.SetLanguage(defaultGettexter.lang)
    69  	} else {
    70  		defaultGettexter.Gettexter = newLocale("", "")
    71  		defaultGettexter.SetLanguage(defaultGettexter.lang)
    72  	}
    73  }
    74  
    75  // SetLanguage sets and queries the program's current lang.
    76  //
    77  // If the lang is not empty string, set the new locale.
    78  //
    79  // If the lang is empty string, don't change anything.
    80  //
    81  // Returns is the current locale.
    82  //
    83  // Examples:
    84  //	SetLanguage("")      // get locale: return DefaultLocale
    85  //	SetLanguage("zh_CN") // set locale: return zh_CN
    86  //	SetLanguage("")      // get locale: return zh_CN
    87  func SetLanguage(lang string) string {
    88  	defaultGettexter.SetLanguage(lang)
    89  	return defaultGettexter.GetLanguage()
    90  }
    91  
    92  // SetDomain sets and retrieves the current message domain.
    93  //
    94  // If the domain is not empty string, set the new domains.
    95  //
    96  // If the domain is empty string, don't change anything.
    97  //
    98  // Returns is the all used domains.
    99  //
   100  // Examples:
   101  //	SetDomain("poedit") // set domain: poedit
   102  //	SetDomain("")       // get domain: return poedit
   103  func SetDomain(domain string) string {
   104  	defaultGettexter.SetDomain(domain)
   105  	return defaultGettexter.GetDomain()
   106  }
   107  
   108  // Gettext attempt to translate a text string into the user's native language,
   109  // by looking up the translation in a message catalog.
   110  //
   111  // It use the caller's function name as the msgctxt.
   112  //
   113  // Examples:
   114  //	func Foo() {
   115  //		msg := gettext.Gettext("Hello") // msgctxt is ""
   116  //	}
   117  func Gettext(msgid string) string {
   118  	return defaultGettexter.Gettext(msgid)
   119  }
   120  
   121  // Getdata attempt to translate a resource file into the user's native language,
   122  // by looking up the translation in a message catalog.
   123  //
   124  // Examples:
   125  //	func Foo() {
   126  //		Textdomain("hello")
   127  //		BindLocale("hello", "locale.zip", nilOrZipData)
   128  //		poems := gettext.Getdata("poems.txt")
   129  //	}
   130  func Getdata(name string) []byte {
   131  	return defaultGettexter.Getdata(name)
   132  }
   133  
   134  // NGettext attempt to translate a text string into the user's native language,
   135  // by looking up the appropriate plural form of the translation in a message
   136  // catalog.
   137  //
   138  // It use the caller's function name as the msgctxt.
   139  //
   140  // Examples:
   141  //	func Foo() {
   142  //		msg := gettext.NGettext("%d people", "%d peoples", 2)
   143  //	}
   144  func NGettext(msgid, msgidPlural string, n int) string {
   145  	return defaultGettexter.NGettext(msgid, msgidPlural, n)
   146  }
   147  
   148  // PGettext attempt to translate a text string into the user's native language,
   149  // by looking up the translation in a message catalog.
   150  //
   151  // Examples:
   152  //	func Foo() {
   153  //		msg := gettext.PGettext("gettext-go.example", "Hello") // msgctxt is "gettext-go.example"
   154  //	}
   155  func PGettext(msgctxt, msgid string) string {
   156  	return defaultGettexter.PGettext(msgctxt, msgid)
   157  }
   158  
   159  // PNGettext attempt to translate a text string into the user's native language,
   160  // by looking up the appropriate plural form of the translation in a message
   161  // catalog.
   162  //
   163  // Examples:
   164  //	func Foo() {
   165  //		msg := gettext.PNGettext("gettext-go.example", "%d people", "%d peoples", 2)
   166  //	}
   167  func PNGettext(msgctxt, msgid, msgidPlural string, n int) string {
   168  	return defaultGettexter.PNGettext(msgctxt, msgid, msgidPlural, n)
   169  }
   170  
   171  // DGettext like Gettext(), but looking up the message in the specified domain.
   172  //
   173  // Examples:
   174  //	func Foo() {
   175  //		msg := gettext.DGettext("poedit", "Hello")
   176  //	}
   177  func DGettext(domain, msgid string) string {
   178  	return defaultGettexter.DGettext(domain, msgid)
   179  }
   180  
   181  // DNGettext like NGettext(), but looking up the message in the specified domain.
   182  //
   183  // Examples:
   184  //	func Foo() {
   185  //		msg := gettext.PNGettext("poedit", "gettext-go.example", "%d people", "%d peoples", 2)
   186  //	}
   187  func DNGettext(domain, msgid, msgidPlural string, n int) string {
   188  	return defaultGettexter.DNGettext(domain, msgid, msgidPlural, n)
   189  }
   190  
   191  // DPGettext like PGettext(), but looking up the message in the specified domain.
   192  //
   193  // Examples:
   194  //	func Foo() {
   195  //		msg := gettext.DPGettext("poedit", "gettext-go.example", "Hello")
   196  //	}
   197  func DPGettext(domain, msgctxt, msgid string) string {
   198  	return defaultGettexter.DPGettext(domain, msgctxt, msgid)
   199  }
   200  
   201  // DPNGettext like PNGettext(), but looking up the message in the specified domain.
   202  //
   203  // Examples:
   204  //	func Foo() {
   205  //		msg := gettext.DPNGettext("poedit", "gettext-go.example", "%d people", "%d peoples", 2)
   206  //	}
   207  func DPNGettext(domain, msgctxt, msgid, msgidPlural string, n int) string {
   208  	return defaultGettexter.DPNGettext(domain, msgctxt, msgid, msgidPlural, n)
   209  }
   210  
   211  // DGetdata like Getdata(), but looking up the resource in the specified domain.
   212  //
   213  // Examples:
   214  //	func Foo() {
   215  //		msg := gettext.DGetdata("hello", "poems.txt")
   216  //	}
   217  func DGetdata(domain, name string) []byte {
   218  	return defaultGettexter.DGetdata(domain, name)
   219  }
   220  

View as plain text