I'm developing a [webapp](https://cxmanager.live) with Nim as the backend. I 
need to support multiple languages with an easy and quick way to add further 
language support. Besides supporting multiple languages, I also need the return 
to be based on the host.

I have found a way which works quite well, but I don't think it's an optimal 
way, if my languagefile scales too much. So I'm looking for performance 
suggestions or another approach, if this isn't the perfect solution.

**languagefile.cfg**
    
    
    title_ENh = Hubanize
    title_DKh = Hubanize
    title_ENc = Cx Manager
    title_DKc = Cx Manager
    help_ENh = Help
    help_DKh = Hjælp
    help_ENc = Help
    help_DKc = Hjælp
    

**languageparser.nim**
    
    
    proc langInit(cfgFilename: string): Table[string, string] =
      ## Parse the languagefile
      let inputString = readFile(cfgFilename)
      result = initTable[string, string]()
      for line in inputString.splitLines:
        result[chunks[0]] = chunks[1]
    
    
    
    let langGet* = langInit("languagefile.cfg")
    
    
    
    proc langGen*(identifier, pageID: string, lang = "EN"): string =
      ## Retrive language item
      ## identifier => The item to retrieve
      ## pageID => c or h based on the host address. Accessed per user with 
c.pageID
      ## lang => Language. Accessed per user with c.lang - test users can have 
empty lang, therefor standard "EN"
      try:
        return langGet[identifier &  "_" & lang & pageID]
      except:
        return ""
    
    
    
    echo langGen("title", "c", "EN") # = Cx Manager
    echo langGen("help", "h", "DK")  # = Hjælp
    

Reply via email to