Skip to content

Commit 9121f18

Browse files
committed
Added German and change language methods.
1 parent 68cdf3e commit 9121f18

File tree

1 file changed

+48
-10
lines changed
  • timeago/src/main/java/com/github/kevinsawicki/timeago

1 file changed

+48
-10
lines changed

timeago/src/main/java/com/github/kevinsawicki/timeago/TimeAgo.java

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011 Kevin Sawicki <kevinsawicki@gmail.com>
2+
* Copyright (c) 2015 Pedro Adame <pedro.a.1smr@gmail.com>
33
*
44
* Permission is hereby granted, free of charge, to any person obtaining a copy
55
* of this software and associated documentation files (the "Software"), to
@@ -50,23 +50,32 @@ public class TimeAgo {
5050
private String months;
5151
private String year;
5252
private String years;
53+
private String country;
54+
/**
55+
* Constructor for the TimeAgo object.
56+
* @param locale The country code for the desired language to display messages. Supported languages are 'en', 'es', 'it', 'fr' and 'de' at the time.
57+
*/
58+
public TimeAgo(String locale) throws IllegalArgumentException{
59+
init(locale);
60+
}
5361

54-
public TimeAgo(String locale){
55-
56-
if(locale.equals("en")){
62+
private void init(String locale){
63+
if(locale.equals("en"))
5764
initLanguage("EN");
58-
}else if(locale.equals("es")){
65+
else if(locale.equals("es"))
5966
initLanguage("ES");
60-
}else if(locale.equals("it")){
67+
else if(locale.equals("it"))
6168
initLanguage("IT");
62-
}else if(locale.equals("fr")){
69+
else if(locale.equals("fr"))
6370
initLanguage("FR");
64-
}else{
65-
throw new IllegalArgumentException("Locale must be equals to 'es', 'en' or 'it'.");
66-
}
71+
else if(locale.equals("de"))
72+
initLanguage("DE");
73+
else
74+
throw new IllegalArgumentException("Locale not recognised or it isn't supported yet.");
6775
}
6876

6977
private void initLanguage(String language) {
78+
country = language.toLowerCase();
7079
prefixAgo = Messages.getString("TimeAgo.PREFIX_AGO" + "_" + language); //$NON-NLS-1$
7180
suffixAgo = Messages.getString("TimeAgo.SUFFIX_AGO" + "_" + language); //$NON-NLS-1$
7281
prefixFromNow = Messages.getString("TimeAgo.PREFIX_FROM_NOW" + "_" + language); //$NON-NLS-1$
@@ -450,4 +459,33 @@ public TimeAgo setYears(String years) {
450459
this.years = years;
451460
return this;
452461
}
462+
463+
/**
464+
* Returns the country code of the actual language.
465+
* @return Used country code.
466+
*/
467+
468+
public String getCountryCode(){
469+
return country;
470+
}
471+
472+
/**
473+
* Changes the language of the expressions to one of the supported ones.
474+
* @param locale The new language. Must be equals to 'en', 'es', 'it', 'fr' or 'de'.
475+
*/
476+
public void changeLanguage(String locale){
477+
if(locale.equals("en"))
478+
initLanguage("EN");
479+
else if(locale.equals("es"))
480+
initLanguage("ES");
481+
else if(locale.equals("it"))
482+
initLanguage("IT");
483+
else if(locale.equals("fr"))
484+
initLanguage("FR");
485+
else if(locale.equals("de"))
486+
initLanguage("DE");
487+
else
488+
throw new IllegalArgumentException("Locale not recognised or it isn't supported yet.");
489+
490+
}
453491
}

0 commit comments

Comments
 (0)