package com.printhub.printhub.service;

import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.*;

@Service
public class I18nService {

    public Map<String, String> getTranslations(String lang) {
        Map<String, String> translations = new HashMap<>();

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        try {
            var resources = resolver.getResources("classpath:i18n/" + lang + "/*.properties");

            for (var resource : resources) {
                Properties props = new Properties();
                try (var reader = new InputStreamReader(resource.getInputStream(), StandardCharsets.UTF_8)) {
                    props.load(reader);
                    for (String key : props.stringPropertyNames()) {
                        translations.put(key, props.getProperty(key));
                    }
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return translations;
    }
}
