How to change LSP config for a single buffer

I’m using the ltex.lua configuration from lsp-config. My configuration works as expected:

      lspconfig.ltex.setup{
        on_attach = on_attach,
        autostart = false,
        settings = {
          ltex = {
            disabledRules = {
              ["en"]    = { "MORFOLOGIK_RULE_EN"    },
              ["en-AU"] = { "MORFOLOGIK_RULE_EN_AU" },
              ["en-CA"] = { "MORFOLOGIK_RULE_EN_CA" },
              ["en-GB"] = { "MORFOLOGIK_RULE_EN_GB" },
              ["en-NZ"] = { "MORFOLOGIK_RULE_EN_NZ" },
              ["en-US"] = { "MORFOLOGIK_RULE_EN_US" },
              ["en-ZA"] = { "MORFOLOGIK_RULE_EN_ZA" },
              ["es"]    = { "MORFOLOGIK_RULE_ES"    },
              ["it"]    = { "MORFOLOGIK_RULE_IT_IT" },
              ["de"]    = { "MORFOLOGIK_RULE_DE_DE" },
            },
          },
        },
      }

However, when working on a non-English text file I would like to change the settings only for a single buffer by modifying the setup table above settings = { ltex = { language = "pt-BR" } } and then starting the LSP server with the modified configuration.

Is that possible? How could I do that?

It’s not really possible in the protocol, unless you launch a single language server per buffer (unless the server has some off-spec way of defining settings per URI.

Yes, the plan would be to launch one single language server per buffer. Doesn’t the language server support single file mode anyway?

I’m thinking of creating configs on the fly by calling a function with the required specs. Would it that be my best shot?

Yes but as an optimization I made “single file mode” share the language server for a given directory

In most cases, slightly different specs are only needed on a project basis. So, I’ll try to see how far I get with the factory method described above.