<commit_before>defmodule Weber.Localization.LocalizationManager do

  use GenServer.Behaviour

  defrecord LocalizationConfig,
    config: nil,
    default_locale: nil

  def start_link(config) do
    :gen_server.start_link({:local, :localization_manager}, __MODULE__, [config], [])
  end

  def init([config]) do
    :gen_server.cast(:erlang.self(), :load_localization_files)
    { :ok, LocalizationConfig.new config: config}
  end

  def handle_cast(:load_localization_files, state) do
    case :lists.keyfind(:localization, 1, state.config) do
      false ->
        {:stop, :normal, state}
      {:localization, localization_config} ->
        project_path = File.cwd!()

        default_locale = Keyword.fetch!(localization_config, :default_locale)
        use_locales = Keyword.fetch!(localization_config, :use_locales)
                      |> Enum.map(fn(l) -> atom_to_binary(l) <> ".json" end)

        on_files_in_path(
                         project_path <> "/deps/weber/lib/weber/i18n/localization/locale",
                         &( :lists.member(&1, use_locales) ),
                         &( Weber.Localization.Locale.start_link(binary_to_atom(&1), &2) )
                        )

        on_files_in_path(
                         project_path <> "/lang",
                         fn (_) -> true end,
                         &( Weber.Translation.Translate.start_link(binary_to_atom(&1), &2) )
                        )

        {:noreply, LocalizationConfig.new config: state.config, default_locale: default_locale}
    end
  end

  def on_files_in_path(path, check_fun, apply_fun) do
    case File.ls(path) do
      {:ok, files} ->
        Enum.each(files, &( check_fun.(&1) && apply_on_file(path, &1, apply_fun) ) )
      _ -> false
    end
  end

  def apply_on_file(path, file, apply_fun) do
    file_data = File.read!(path <> file)
    (file_data != <<>>) && apply_fun.(file, file_data)
  end

end<commit_msg>Fix bug that weber can't find locale files.<commit_after>defmodule Weber.Localization.LocalizationManager do

  use GenServer.Behaviour

  defrecord LocalizationConfig,
    config: nil,
    default_locale: nil

  def start_link(config) do
    :gen_server.start_link({:local, :localization_manager}, __MODULE__, [config], [])
  end

  def init([config]) do
    :gen_server.cast(:erlang.self(), :load_localization_files)
    { :ok, LocalizationConfig.new config: config}
  end

  def handle_cast(:load_localization_files, state) do
    case :lists.keyfind(:localization, 1, state.config) do
      false ->
        {:stop, :normal, state}
      {:localization, localization_config} ->
        project_path = File.cwd!()

        default_locale = Keyword.fetch!(localization_config, :default_locale)
        use_locales = Keyword.fetch!(localization_config, :use_locales)
                      |> Enum.map(fn(l) -> atom_to_binary(l) <> ".json" end)

        on_files_in_path(
                         Path.join([project_path, "/deps/weber/lib/weber/i18n/localization/locale"]),
                         &( :lists.member(&1, use_locales) ),
                         &( Weber.Localization.Locale.start_link(binary_to_atom(&1), &2) )
                        )

        on_files_in_path(
                         Path.join([project_path, "/lang"]),
                         fn (_) -> true end,
                         &( Weber.Translation.Translate.start_link(binary_to_atom(&1), &2) )
                        )

        {:noreply, LocalizationConfig.new config: state.config, default_locale: default_locale}
    end
  end

  def on_files_in_path(path, check_fun, apply_fun) do
    case File.ls(path) do
      {:ok, files} ->
        Enum.each(files, &( check_fun.(&1) && apply_on_file(path, &1, apply_fun) ) )
      _ -> false
    end
  end

  def apply_on_file(path, file, apply_fun) do
    file_data = File.read!(Path.join([path, file]))
    (file_data != <<>>) && apply_fun.(file, file_data)
  end

end
{'fraction_non_alphanumeric': 0.1250317662007624, 'fraction_numerical': 0.005082592121982211, 'mean_word_length': 2.4077922077922076, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 26, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6424017', 'n_tokens_mistral': 1284, 'n_tokens_neox': 1225, 'n_words': 226}