All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.ultreia.java4all.i18n.spi.I18nTemplateDefinition Maven / Gradle / Ivy

package io.ultreia.java4all.i18n.spi;

/*-
 * #%L
 * I18n :: Spi
 * %%
 * Copyright (C) 2018 Code Lutin, Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Lesser Public License for more details.
 *
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Locale;
import java.util.Objects;
import java.util.StringTokenizer;
import java.util.stream.Collectors;

/**
 * Describe a set of templates.
 * 

* Created by tchemit on 04/11/2018. * * @author Tony Chemit - [email protected] */ public class I18nTemplateDefinition extends I18nLocalizedResource { public static String TEMPLATE_SET_EXTENSION = ".template"; public I18nTemplateDefinition(String packageName, String name, Locale locale) { super(packageName, name, TEMPLATE_SET_EXTENSION, locale); } public static List detect(Path directory, String packageName) throws I18nResourceInitializationException { if (!Files.exists(directory)) { return Collections.emptyList(); } try { return Files .walk(directory, 1) .filter(p -> Files.isRegularFile(p) && p.toFile().getName().endsWith(I18nTemplateDefinition.TEMPLATE_SET_EXTENSION)) .map(f -> I18nTemplateDefinition.templateFromFilename(packageName, f)) .collect(Collectors.toList()); } catch (Exception e) { throw new I18nResourceInitializationException("Could not init from: " + directory, e); } } public static void write(I18nTemplateDefinition definition, Path directory, Charset encoding, boolean usePackage, String template, boolean override) throws IOException { if (!Files.exists(Objects.requireNonNull(directory))) { Files.createDirectories(directory); } String fileName = definition.getResourcePath(usePackage); Path target = directory.resolve(fileName); if (override || !Files.exists(target)) { Files.write(target, Collections.singletonList(template), encoding); } } public static List templateListFromCoordinate(String coordinate) { List result = new LinkedList<>(); StringTokenizer tokenizer = new StringTokenizer(coordinate, ":"); String packageName = tokenizer.nextToken(); String name = tokenizer.nextToken(); while (tokenizer.hasMoreTokens()) { String locale = tokenizer.nextToken(); result.add(new I18nTemplateDefinition(packageName, name, I18nLocaleHelper.newLocale(locale))); } return result; } public static I18nTemplateDefinition templateFromFilename(String packageName, Path coordinate) { String filename = I18nResource.removeExtension(Objects.requireNonNull(coordinate).toFile().getName(), TEMPLATE_SET_EXTENSION); int i = filename.indexOf("_"); String name = filename.substring(0, i); String localeStr = filename.substring(i + 1); return new I18nTemplateDefinition(packageName, name, I18nLocaleHelper.newLocale(localeStr)); } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy