public DefaultInvoiceItemFormatter(final TranslatorConfig config, final InvoiceItem item, final DateTimeFormatter dateFormatter, final Locale locale) { this.item = item; this.dateFormatter = dateFormatter; this.locale = locale; this.translator = new DefaultCatalogTranslator(config); }
@Override protected String getBundlePath() { return config.getCatalogBundlePath(); }
@Override protected String getBundlePath() { return config.getInvoiceTemplateBundlePath(); }
@Override public String getPhaseName() { return Strings.nullToEmpty(translator.getTranslation(locale, item.getPhaseName())); }
@Override public String getTranslation(final Locale locale, final String originalText) { final String bundlePath = getBundlePath(); ResourceBundle bundle = getBundle(locale, bundlePath); if ((bundle != null) && (bundle.containsKey(originalText))) { return bundle.getString(originalText); } else { if (config.getDefaultLocale() == null) { log.debug("No default locale configured, returning original text"); return originalText; } final Locale defaultLocale = LocaleUtils.toLocale(config.getDefaultLocale()); try { bundle = getBundle(defaultLocale, bundlePath); if ((bundle != null) && (bundle.containsKey(originalText))) { return bundle.getString(originalText); } else { return originalText; } } catch (MissingResourceException mrex) { log.warn("Missing translation bundle for locale {}", defaultLocale); return originalText; } } }
@Test(groups = "fast") public void testResourceDoesNotExist() throws Exception { final TestTranslatorBase translator = new TestTranslatorBase(Mockito.mock(TranslatorConfig.class)); final String originalText = UUID.randomUUID().toString(); Assert.assertEquals(translator.getTranslation(Locale.FRANCE, originalText), originalText); } }
public String generateInvoice(final Account account, @Nullable final Invoice invoice, final boolean manualPay) throws IOException { // Don't do anything if the invoice is null if (invoice == null) { return null; } final Map<String, Object> data = new HashMap<String, Object>(); final DefaultInvoiceTranslator invoiceTranslator = new DefaultInvoiceTranslator(config); final Locale locale = LocaleUtils.toLocale(account.getLocale()); invoiceTranslator.setLocale(locale); data.put("text", invoiceTranslator); data.put("account", account); final InvoiceFormatter formattedInvoice = factory.createInvoiceFormatter(config, invoice, locale, currencyConversionApi); data.put("invoice", formattedInvoice); if (manualPay) { return templateEngine.executeTemplate(config.getManualPayTemplateName(), data); } else { return templateEngine.executeTemplate(config.getTemplateName(), data); } } }
private ResourceBundle getBundle(final Locale locale, final String bundlePath) { try { // Try to load the bundle from the classpath first return ResourceBundle.getBundle(bundlePath, locale); } catch (MissingResourceException ignored) { } // Try to load it from a properties file final String propertiesFileNameWithCountry = bundlePath + "_" + locale.getLanguage() + "_" + locale.getCountry() + ".properties"; ResourceBundle bundle = getBundleFromPropertiesFile(propertiesFileNameWithCountry); if (bundle != null) { return bundle; } else { final String propertiesFileName = bundlePath + "_" + locale.getLanguage() + ".properties"; bundle = getBundleFromPropertiesFile(propertiesFileName); } return bundle; }
protected void installNotifiers() { bind(NextBillingDateNotifier.class).to(DefaultNextBillingDateNotifier.class).asEagerSingleton(); bind(NextBillingDatePoster.class).to(DefaultNextBillingDatePoster.class).asEagerSingleton(); final TranslatorConfig config = new ConfigurationObjectFactory(configSource).build(TranslatorConfig.class); bind(TranslatorConfig.class).toInstance(config); bind(InvoiceFormatterFactory.class).to(config.getInvoiceFormatterFactoryClass()).asEagerSingleton(); }
@Override public String getPlanName() { return Strings.nullToEmpty(translator.getTranslation(locale, item.getPlanName())); }
@Override @BeforeClass(groups = "fast") public void beforeClass() throws Exception { super.beforeClass(); final ConfigSource configSource = new ConfigSource() { private final Map<String, String> properties = ImmutableMap.<String, String>of("killbill.template.invoiceFormatterFactoryClass", "com.ning.billing.mock.MockInvoiceFormatterFactory"); @Override public String getString(final String propertyName) { return properties.get(propertyName); } }; final TranslatorConfig config = new ConfigurationObjectFactory(configSource).build(TranslatorConfig.class); translation = new DefaultCatalogTranslator(config); }
@Test(groups = "fast") public void testMissingTranslation() { // If the translation is missing from the file, return the original text final String originalText = "missing translation"; assertEquals(translation.getTranslation(Locale.US, originalText), originalText); }
@Test(groups = "fast") public void testMissingFileAndText() { // If the file is missing, and the "English" translation is missing, return the original text final String originalText = "missing translation"; assertEquals(translation.getTranslation(Locale.CHINA, originalText), originalText); } }
@Test(groups = "fast") public void testExistingTranslation() { // If the translation exists, return the translation final String originalText = "shotgun-monthly"; assertEquals(translation.getTranslation(Locale.US, originalText), "Monthly shotgun plan"); }
@Test(groups = "fast") public void testMissingTranslationFileWithEnglishText() { // If the translation file doesn't exist, return the "English" translation final String originalText = "shotgun-monthly"; assertEquals(translation.getTranslation(Locale.CHINA, originalText), "Monthly shotgun plan"); }
@Test(groups = "fast") public void testInitialization() { final String shotgunMonthly = "shotgun-monthly"; final String shotgunAnnual = "shotgun-annual"; final String badText = "Bad text"; assertEquals(translation.getTranslation(Locale.US, shotgunMonthly), "Monthly shotgun plan"); assertEquals(translation.getTranslation(Locale.US, shotgunAnnual), "Annual shotgun plan"); assertEquals(translation.getTranslation(Locale.US, badText), badText); assertEquals(translation.getTranslation(Locale.CANADA_FRENCH, shotgunMonthly), "Fusil de chasse mensuel"); assertEquals(translation.getTranslation(Locale.CANADA_FRENCH, shotgunAnnual), "Fusil de chasse annuel"); assertEquals(translation.getTranslation(Locale.CANADA_FRENCH, badText), badText); assertEquals(translation.getTranslation(Locale.CHINA, shotgunMonthly), "Monthly shotgun plan"); assertEquals(translation.getTranslation(Locale.CHINA, shotgunAnnual), "Annual shotgun plan"); assertEquals(translation.getTranslation(Locale.CHINA, badText), badText); }