Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@
<artifactId>jackson-databind</artifactId>
<version>2.18.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/javax.inject/javax.inject -->
<dependency>
<groupId>javax.inject</groupId>
<artifactId>javax.inject</artifactId>
<version>1</version>
</dependency>

</dependencies>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Нащо це тут? Схоже він ніде не використовуєтся.


<build>
Expand Down
23 changes: 14 additions & 9 deletions src/main/java/ua/com/javarush/gnew/m2/cli/PhoneBookCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import picocli.CommandLine;
import picocli.CommandLine.Command;
import ua.com.javarush.gnew.m2.cli.commands.*;
import ua.com.javarush.gnew.m2.service.LocalizationService;
import java.util.Locale;

@Command(
name = "phonebook",
Expand All @@ -13,21 +15,24 @@
description = "CLI для управління контактами в телефонній книзі")
public class PhoneBookCLI implements CliCommand {

@Override
LocalizationService localizationService = new LocalizationService(Locale.getDefault());

@Override
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Це не сюди, це треба зробити у класі PhoneBookContext. Також ти встановлюєш локаль з стстеми, а в нас заплановано що локаль вибирає користувач і зберігає у файлі налаштувань. отже локаль треба взяти звідти.

public Integer call() {
System.out.println("Використовуйте одну з команд: user, add, search, edit, delete, list");
System.out.println(localizationService.getMessage("app.commands").replace("{0}","user, add, search, edit, delete, list, locale"));
return 0;
}


public static int init(String[] args) {
return new CommandLine(getBean(PhoneBookCLI.class))
.addSubcommand("add", getBean(AddContact.class))
.addSubcommand("search", getBean(SearchContact.class))
.addSubcommand("edit", getBean(EditContact.class))
.addSubcommand("delete", getBean(DeleteContact.class))
.addSubcommand("list", getBean(ListContacts.class))
.addSubcommand("user", getBean(SetUser.class))
.addSubcommand("locale", getBean(SetLocale.class))
.addSubcommand("command.add", getBean(AddContact.class))
.addSubcommand("command.search", getBean(SearchContact.class))
.addSubcommand("command.edit", getBean(EditContact.class))
.addSubcommand("command.delete", getBean(DeleteContact.class))
.addSubcommand("command.list", getBean(ListContacts.class))
.addSubcommand("command.user", getBean(SetUser.class))
.addSubcommand("command.locale", getBean(SetLocale.class))
.execute(args);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Це не текст для користвача це назви команд для picocli їх не треба перекладати. Думаю так воно не заведется.

}
}
41 changes: 30 additions & 11 deletions src/main/java/ua/com/javarush/gnew/m2/cli/commands/SetLocale.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
package ua.com.javarush.gnew.m2.cli.commands;

import java.io.IOException;
import java.util.Arrays;
import java.util.Locale;

import com.fasterxml.jackson.annotation.JacksonInject;
import picocli.CommandLine;
import ua.com.javarush.gnew.m2.cli.CliCommand;
import ua.com.javarush.gnew.m2.configuration.PhoneBookContext;
import ua.com.javarush.gnew.m2.service.LocalizationService;
import ua.com.javarush.gnew.m2.service.SettingsServiceInterface;

import javax.inject.Inject;

@CommandLine.Command(
name = "locale",
aliases = {"-l", "--locale"},
Expand All @@ -14,20 +21,32 @@
public class SetLocale implements CliCommand {

private final SettingsServiceInterface settingsService =
PhoneBookContext.getBean(SettingsServiceInterface.class);
PhoneBookContext.getBean(SettingsServiceInterface.class);

LocalizationService localizationService = new LocalizationService(Locale.getDefault());

@CommandLine.Parameters(index = "0", description = "language", arity = "1")
private String newLocale;

@Override
public Integer call() {
try {
settingsService.setLocale(newLocale);
System.out.println("Локаль установлена на: " + newLocale);
return 0;
} catch (IOException e) {
System.err.println("Ошибка при установке локали: " + e.getMessage());
return 1;


@Override
public Integer call() {
try {
Locale locale = new Locale(newLocale);
if (!Arrays.asList(Locale.getAvailableLocales()).contains(locale)) {
System.err.println(localizationService.getMessage("error.dontUse.notfound").replace("{0}", newLocale));
return 1;
}
settingsService.setLocale(newLocale);
localizationService.setLocale(new Locale(newLocale));
System.out.println(localizationService.getMessage("success.locale.changed").replace("{0}", newLocale));
System.out.println(localizationService.getMessage("locale.current").replace("{0}", newLocale));
return 0;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ОК. хоча програма закінчить роботу після виконання цєї команди. Чи є потреба міняти поточну локаль?


} catch (IOException e) {
System.err.println(localizationService.getMessage("error.command.notfound").replace("{0}", newLocale));
return 1;
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ua.com.javarush.gnew.m2.service;

import java.nio.charset.StandardCharsets;
import java.util.Locale;
import java.util.ResourceBundle;

Expand All @@ -13,13 +14,15 @@ public LocalizationService(Locale locale) {


public void setLocale(Locale locale) {
this.resourceBundle = ResourceBundle.getBundle("messages", locale);
this.resourceBundle = ResourceBundle.getBundle("Localization", locale);
}



public String getMessage(String key) {
try {
return resourceBundle.getString(key);
String message = resourceBundle.getString(key);
return new String(message.getBytes(StandardCharsets.UTF_8), StandardCharsets.UTF_8);
} catch (Exception e) {
return "Message not found for key: " + key;
}
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/Localization_ru.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
RU
26 changes: 26 additions & 0 deletions src/main/resources/Localization_uk.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Загальні повідомлення
app.description=CLI для управління контактами в телефонній книзі
app.version=Телефонна книга CLI 1.0
app.commands=Використовуйте одну з команд: {0}

# Команди
command.add=додати
command.search=пошук
command.edit=редагувати
command.delete=видалити
command.list=список
command.user=користувач
command.locale=мова

# Підказки та помилки
error.command.notfound=Команду не знайдено: {0}
error.missing.argument=Відсутній обов'язковий аргумент: {0}
success.locale.changed=Мову успішно змінено на {0}
error.dontUse.notfound = невідома команда: {0}


# Мови
locale.current=Поточна мова: {0}
locale.available=Доступні мови: {0}


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ОК . Але це тільки маленький шматочек тут потрібно внести всі тексти для користувача.

Empty file.
1 change: 0 additions & 1 deletion src/main/resources/messages_ru.properties

This file was deleted.

1 change: 0 additions & 1 deletion src/main/resources/messages_ua.properties

This file was deleted.