Skip to content

Commit d6e3eb5

Browse files
authored
Refactor Gettext.Macros to generate less code (#393)
1 parent 0cdb06a commit d6e3eb5

File tree

5 files changed

+594
-562
lines changed

5 files changed

+594
-562
lines changed

lib/gettext.ex

+21-6
Original file line numberDiff line numberDiff line change
@@ -598,6 +598,16 @@ defmodule Gettext do
598598
@type backend :: module
599599
@type bindings :: map() | Keyword.t()
600600

601+
@typedoc """
602+
A Gettext domain.
603+
604+
See [*Domains*](#module-domains) in the module documentation for more information.
605+
"""
606+
@typedoc since: "0.26.0"
607+
@type domain() :: :default | binary()
608+
609+
defguardp is_domain(domain) when domain == :default or is_binary(domain)
610+
601611
@doc false
602612
defmacro __using__(opts) do
603613
opts =
@@ -775,15 +785,16 @@ defmodule Gettext do
775785
776786
"""
777787
@doc section: :translation
778-
@spec dpgettext(module, binary, binary | nil, binary, bindings) :: binary
788+
@spec dpgettext(module, domain, binary | nil, binary, bindings) :: binary
779789
def dpgettext(backend, domain, msgctxt, msgid, bindings \\ %{})
780790

781791
def dpgettext(backend, domain, msgctxt, msgid, bindings) when is_list(bindings) do
782792
dpgettext(backend, domain, msgctxt, msgid, Map.new(bindings))
783793
end
784794

785795
def dpgettext(backend, domain, msgctxt, msgid, bindings)
786-
when is_atom(backend) and is_binary(domain) and is_binary(msgid) and is_map(bindings) do
796+
when is_atom(backend) and is_domain(domain) and is_binary(msgid) and is_map(bindings) do
797+
domain = domain_or_default(backend, domain)
787798
locale = get_locale(backend)
788799
result = backend.lgettext(locale, domain, msgctxt, msgid, bindings)
789800
handle_backend_result(result, backend, locale, domain, msgctxt, msgid)
@@ -820,7 +831,7 @@ defmodule Gettext do
820831
821832
"""
822833
@doc section: :translation
823-
@spec dgettext(module, binary, binary, bindings) :: binary
834+
@spec dgettext(module, domain, binary, bindings) :: binary
824835
def dgettext(backend, domain, msgid, bindings \\ %{}) do
825836
dpgettext(backend, domain, nil, msgid, bindings)
826837
end
@@ -900,7 +911,7 @@ defmodule Gettext do
900911
901912
"""
902913
@doc section: :translation
903-
@spec dpngettext(module, binary, binary | nil, binary, binary, non_neg_integer, bindings) ::
914+
@spec dpngettext(module, domain, binary | nil, binary, binary, non_neg_integer, bindings) ::
904915
binary
905916
def dpngettext(backend, domain, msgctxt, msgid, msgid_plural, n, bindings \\ %{})
906917

@@ -910,8 +921,9 @@ defmodule Gettext do
910921
end
911922

912923
def dpngettext(backend, domain, msgctxt, msgid, msgid_plural, n, bindings)
913-
when is_atom(backend) and is_binary(domain) and is_binary(msgid) and is_binary(msgid_plural) and
924+
when is_atom(backend) and is_domain(domain) and is_binary(msgid) and is_binary(msgid_plural) and
914925
is_integer(n) and n >= 0 and is_map(bindings) do
926+
domain = domain_or_default(backend, domain)
915927
locale = get_locale(backend)
916928
result = backend.lngettext(locale, domain, msgctxt, msgid, msgid_plural, n, bindings)
917929
handle_backend_result(result, backend, locale, domain, msgctxt, msgid)
@@ -943,7 +955,7 @@ defmodule Gettext do
943955
944956
"""
945957
@doc section: :translation
946-
@spec dngettext(module, binary, binary, binary, non_neg_integer, bindings) :: binary
958+
@spec dngettext(module, domain, binary, binary, non_neg_integer, bindings) :: binary
947959
def dngettext(backend, domain, msgid, msgid_plural, n, bindings \\ %{}),
948960
do: dpngettext(backend, domain, nil, msgid, msgid_plural, n, bindings)
949961

@@ -1125,4 +1137,7 @@ defmodule Gettext do
11251137

11261138
backend.handle_missing_bindings(exception, incomplete)
11271139
end
1140+
1141+
defp domain_or_default(backend, :default), do: backend.__gettext__(:default_domain)
1142+
defp domain_or_default(_backend, domain) when is_binary(domain), do: domain
11281143
end

0 commit comments

Comments
 (0)