Skip to content

Instantly share code, notes, and snippets.

@Adirelle
Last active November 29, 2018 21:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adirelle/c9186e8f61776c9ea2b45a64811f14fe to your computer and use it in GitHub Desktop.
Save Adirelle/c9186e8f61776c9ea2b45a64811f14fe to your computer and use it in GitHub Desktop.
Rimworld - French Language Worker
public abstract class LanguageWorker
{
public virtual string WithIndefiniteArticle(string str, Gender gender, bool plural = false, bool name = false)
{
if( str.NullOrEmpty() )
return "";
//By default names don't get articles
if( name )
return str;
if( Translator.CanTranslate( "IndefiniteForm" ) )
return "IndefiniteForm".Translate(str);
else
return "IndefiniteArticle".Translate() + " " + str;
}
public string WithIndefiniteArticle(string str, bool plural = false, bool name = false)
{
return WithIndefiniteArticle(str, LanguageDatabase.activeLanguage.ResolveGender(str), plural, name);
}
public string WithIndefiniteArticlePostProcessed(string str, Gender gender, bool plural = false, bool name = false)
{
return PostProcessed(WithIndefiniteArticle(str, gender, plural, name));
}
public string WithIndefiniteArticlePostProcessed(string str, bool plural = false, bool name = false)
{
return PostProcessed(WithIndefiniteArticle(str, plural, name));
}
public virtual string WithDefiniteArticle(string str, Gender gender, bool plural = false, bool name = false)
{
if( str.NullOrEmpty() )
return "";
//By default names don't get articles
if( name )
return str;
if( Translator.CanTranslate( "DefiniteForm" ) )
return "DefiniteForm".Translate(str);
else
return "DefiniteArticle".Translate() + " " + str;
}
public string WithDefiniteArticle(string str, bool plural = false, bool name = false)
{
return WithDefiniteArticle(str, LanguageDatabase.activeLanguage.ResolveGender(str), plural, name);
}
public string WithDefiniteArticlePostProcessed(string str, Gender gender, bool plural = false, bool name = false)
{
return PostProcessed(WithDefiniteArticle(str, gender, plural, name));
}
public string WithDefiniteArticlePostProcessed(string str, bool plural = false, bool name = false)
{
return PostProcessed(WithDefiniteArticle(str, plural, name));
}
public virtual string OrdinalNumber(int number, Gender gender = Gender.None)
{
return number.ToString();
}
public virtual string PostProcessed(string str)
{
//Fix double-spaces
str = str.MergeMultipleSpaces();
return str;
}
public virtual string ToTitleCase(string str)
{
if( str.NullOrEmpty() )
return str;
return CultureInfo.CurrentCulture.TextInfo.ToTitleCase(str);
}
public virtual string Pluralize(string str, Gender gender, int count = -1)
{
return str;
}
public string Pluralize(string str, int count = -1)
{
return Pluralize(str, LanguageDatabase.activeLanguage.ResolveGender(str), count);
}
public virtual string PostProcessedKeyedTranslation(string translation)
{
return translation;
}
}
public class LanguageWorker_French : LanguageWorker
{
public override string WithIndefiniteArticle(string str, Gender gender, bool plural = false, bool name = false)
{
//Names don't get articles
if( name )
return str;
if( plural )
return "des " + str;
return (gender == Gender.Female ? "une " : "un ") + str;
}
public override string WithDefiniteArticle(string str, Gender gender, bool plural = false, bool name = false)
{
if( str.NullOrEmpty() )
return str;
//Names don't get articles
if( name )
return str;
if( plural )
return "les " + str;
char first = str[0];
if( IsVowel(first) )
return "l'" + str;
return (gender == Gender.Female ? "la " : "le ") + str;
}
public override string OrdinalNumber(int number, Gender gender = Gender.None)
{
return number == 1 ? number + "er" : number + "e";
}
public override string Pluralize(string str, Gender gender, int count = -1)
{
if( str.NullOrEmpty() )
return str;
// TODO: should str be lowercased before the tests?
// words ending with "s", "x" or "z": do not change anything,
char last = str[str.Length - 1];
if( last == 's' || last == 'x' || last == 'z' )
return str;
// exceptions to the next rules; only test words that could possibly be found in Rimworld
switch( str ) {
// "bail", "corail", "émail", "gemmail", "soupirail", "travail", "vantail", "vitrail": replace "ail" by "aux"
case "travail":
return str.Substring(0, str.Length - 3) + "aux";
// "bleu", "émeu", "landau", "lieu", "pneu", "sarrau", "bal", "banal", "fatal", "final", "festival": append "s"
case "bleu":
case "émeu":
case "lieu":
case "banal":
case "fatal":
case "final":
return str + "s";
// "bijou", "caillou", "chou", "genou", "hibou", "joujou", "pou": append "x"
case "bijou":
case "caillou":
case "genou":
return str + "x";
}
// words ending with "al": replace "al" by "aux"
if( str.EndsWith("al") )
return str.Substring(0, str.Length - 2) + "aux";
// words ending with "au" or "eu": append "x"
if( str.EndsWith("au") | str.EndsWith("eu") )
return str + "x";
// general case: append s
return str + "s";
}
public override string PostProcessed(string str)
{
return PostProcessedInt(base.PostProcessed(str));
}
public override string PostProcessedKeyedTranslation(string translation)
{
return PostProcessedInt(base.PostProcessedKeyedTranslation(translation));
}
public bool IsVowel(char ch)
{
return "aàâäæeéèêëiîïoôöœuùüûhAÀÂÄÆEÉÈÊËIÎÏOÔÖŒUÙÜÛH".IndexOf(ch) >= 0;
}
private string PostProcessedInt(string str)
{
// Note: should spaces be added before and after to simulate "word boundaries" ?
return str
.Replace(" de le ", " du ")
.Replace(" de les ", " des ")
.Replace(" de des ", " des ")
.Replace(" à le ", " au ")
.Replace(" à les ", " aux ")
.Replace(" si il ", " s'il ")
.Replace(" si ils ", " s'ils ")
// the following rules could probably be generalized to "que/lorsque" before all vowels
// and should also be applied at the start of the string
.Replace(" que il ", " qu'il ")
.Replace(" que il ", " qu'ils ")
.Replace(" que elle ", " qu'elle ")
.Replace(" que elles ", " qu'elles ")
.Replace(" lorsque il ", " lorsqu'il ")
.Replace(" lorsque il ", " lorsqu'ils ")
.Replace(" lorsque elle ", " lorsqu'elle ")
.Replace(" lorsque elles ", " lorsqu'elles ");
// TODO: other elision rules: "le/la/de []" > "l'/l'/d'[vowel]"
}
}
@b606
Copy link

b606 commented Nov 29, 2018

Line 97 : remove "yY" and add "hH"

For LanguageWorker_French, after further testing and checks

  1. The letters "yY" should be removed from the vowels list since definite article for these is not "l'". Ex : "the yacht" -> "le yacht" not "l'yacht".

  2. Most of words starting with "h" (case of so called 'h muet') get " l' " but there are exceptions. Ex: "the man" -> "l'homme", "l'hortensia" but "la hotte". Unless someone could define the grammatical rule for that, I would treat this as a general case and process the exception differently. The idea is that in RimWorld, the words with that exception are really rare.

Exceptions for elisions can be handled one by one by using non-breaking spaces.

I already had to deal with that somewhere in the translation files. For example in sentences such as "Le but est de le capturer" instead of "Le but est du capturer", elision has been deactivated by hand since "le" here is a pronoun not an article.

@Adirelle
Copy link
Author

The letters "yY" should be removed from the vowels list since definite article for these is not "l'". Ex : "the yacht" -> "le yacht" not "l'yacht".

There are exceptions too, but most them seems to be proper nouns (l'Yonne, ).

  1. Most of words starting with "h" (case of so called 'h muet') get " l' " but there are exceptions. Ex: "the man" -> "l'homme", "l'hortensia"
    but "la hotte". Unless someone could define the grammatical rule for that, I would treat this as a general case and process the exception differently. The idea is that in RimWorld, the words with that exception are really rare.

The exceptions are words starting with an aspirated h (like "haricot"). In our case we would have to list them (or at least the ones in the files).

References:

@Adirelle
Copy link
Author

In this screenshort, we should read "Pirate mâle des Muggers"; "Les Muggers" being the name of a faction. I am not sure how to fix it.

image

@Adirelle
Copy link
Author

Fixed the list of vowels.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment